Hey everyone out there please help me with this code

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

int main()
{
      long n;
      cout<<"Enter no. of random numbers to be generated\n";
      cin>>n;
      int arr1[n];
      srand(time(NULL));
      for(int i=0;i<n;i++)
      {
                      arr1[i]=rand();
                     
      }
int x=0;
int pos=0;
double k=time(NULL);
for(int i=0;i<n-1;i++)
{
                x=arr1[i];
                for(int j=i+1;j<n;j++)
                {
                                if(x>arr1[j])
                                {x=arr1[j];
                                pos=j;}
                }
                arr1[pos]=arr1[i];
                arr1[i]=x;
                pos=n+1;  
}
double l=time(NULL);
for(int i=0;i<n;i++)
{
                cout<<arr1[i]<<"\t";
}
      system("pause");
      system("cls");
      int j=n/2;
      cout<<"Median ="<<arr1[j]<<"\n";
      cout<<"Time Taken in Processing="<<l-k;
      system("pause");
      return 0;
}

When I enter a 5 digit number in n the program works perfectly fine
But when I enter a 6 digit number it hangs

Be patient. It's still processing. It doesn't print until all the random numbers have been generated. Those sometimes take a while. Especially when there are a lot of them.

Compiled code with G++. Ran with CPU under moderate load. Finished in 14 seconds.

I'm not seeing any problems.

Yeah, you have to consider how your algorithm scales: [tex]O(n^2)[/tex], in this case. This means that a number 10 times as big will take 100 times as long to process. So, if you enter 100000 and it takes 5 seconds, then for 1000000 you would expect to wait 500 seconds before it prints out the result.

I have compiled it with dev c++ and the program hangs and the not responding error comes when i eneter 6 digits
Is there any faster method of sorting????????????????

I made my own algorithm for sorting
but after the process it just displays half random numbers and half 0s

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
long n;
      cout<<"Enter no. of random numbers to be generated\n";
      cin>>n;
      int arr[n];
      srand(time(NULL));
      for(int i=0;i<n;i++)
      {
                      arr[i]=rand();
                     
      }

double m=time(NULL);
int arr1[n];
int arr2[n];
int arr3[n/2];
int arr4[n/2];
int arr5[n/2];
int arr6[n/2];
int arr7[n/4];
int arr8[n/4];
int arr9[n/4];
int arr10[n/4];
int arr11[n/4];
int arr12[n/4];
int arr13[n/4];
int arr14[n/4];
int j=0,k=0;

int pivot = arr[n/2];
for(int i=0;i<n;i++)
{
if(arr[i]<=pivot)
{arr1[j]=arr[i];j++;}
if(arr[i]>pivot)
{arr2[k]=arr[i];k++;}
}
j=0,k=0;

pivot = arr1[n/4];
for(int i=0;i<n/2;i++)
{
if(arr1[i]<=pivot)
{arr3[j]=arr1[i];j++;}
if(arr1[i]>pivot)
{arr4[k]=arr1[i];k++;}
}
j=0,k=0;

pivot = arr2[n/4];
for(int i=0;i<n/2;i++)
{
if(arr2[i]<=pivot)
{arr5[j]=arr2[i];j++;}
if(arr2[i]>pivot)
{arr6[k]=arr2[i];k++;}
}
j=0,k=0;

pivot = arr3[n/8];
for(int i=0;i<n/4;i++)
{
if(arr3[i]<=pivot)
{arr7[j]=arr3[i];j++;}
if(arr3[i]>pivot)
{arr8[k]=arr3[i];k++;}
}
int aa=j;
int ab=aa+k;
j=0,k=0;

pivot = arr4[n/8];
for(int i=0;i<n/4;i++)
{
if(arr4[i]<=pivot)
{arr9[j]=arr4[i];j++;}
if(arr4[i]>pivot)
{arr10[k]=arr4[i];k++;}
}
int ac=ab+j;
int ad=ac+k;
j=0,k=0;

pivot = arr5[n/8];
for(int i=0;i<n/4;i++)
{
if(arr5[i]<=pivot)
{arr11[j]=arr5[i];j++;}
if(arr5[i]>pivot)
{arr12[k]=arr5[i];k++;}
}
int ae=ad+j;
int af=ae+k;
j=0,k=0;

pivot = arr6[n/8];
for(int i=0;i<n/4;i++)
{
if(arr6[i]<=pivot)
{arr13[j]=arr6[i];j++;}
if(arr6[i]>pivot)
{arr14[k]=arr6[i];k++;}
}
int ag=af+j;
int ah=ag+k;
j=0,k=0;

int x=0;
int pos=0;
for(int i=0;i<n/4;i++)
{
                x=arr7[i];
                for(int j=i+1;j<n;j++)
                {
                                if(x>arr7[j])
                                {x=arr7[j];
                                pos=j;}
                }
                arr7[pos]=arr7[i];
                arr7[i]=x;
                pos=n+1;  
}
x=0;
pos=0;
for(int i=0;i<n/4;i++)
{
                x=arr8[i];
                for(int j=i+1;j<n;j++)
                {
                                if(x>arr8[j])
                                {x=arr8[j];
                                pos=j;}
                }
                arr8[pos]=arr8[i];
                arr8[i]=x;
                pos=n+1;  
}
x=0;
pos=0;
for(int i=0;i<n/4;i++)
{
                x=arr9[i];
                for(int j=i+1;j<n;j++)
                {
                                if(x>arr9[j])
                                {x=arr9[j];
                                pos=j;}
                }
                arr9[pos]=arr9[i];
                arr9[i]=x;
                pos=n+1;  
}
x=0;
pos=0;
for(int i=0;i<n/4;i++)
{
                x=arr10[i];
                for(int j=i+1;j<n;j++)
                {
                                if(x>arr10[j])
                                {x=arr10[j];
                                pos=j;}
                }
                arr10[pos]=arr10[i];
                arr10[i]=x;
                pos=n+1;  
}
x=0;
pos=0;
for(int i=0;i<n/4;i++)
{
                x=arr11[i];
                for(int j=i+1;j<n;j++)
                {
                                if(x>arr11[j])
                                {x=arr11[j];
                                pos=j;}
                }
                arr11[pos]=arr11[i];
                arr11[i]=x;
                pos=n+1;  
}
x=0;
pos=0;
for(int i=0;i<n/4;i++)
{
                x=arr12[i];
                for(int j=i+1;j<n;j++)
                {
                                if(x>arr12[j])
                                {x=arr12[j];
                                pos=j;}
                }
                arr12[pos]=arr12[i];
                arr12[i]=x;
                pos=n+1;  
}
x=0;
pos=0;
for(int i=0;i<n/4;i++)
{
                x=arr13[i];
                for(int j=i+1;j<n;j++)
                {
                                if(x>arr13[j])
                                {x=arr13[j];
                                pos=j;}
                }
                arr13[pos]=arr13[i];
                arr13[i]=x;
                pos=n+1;  
}
x=0;
pos=0;
for(int i=0;i<n/4;i++)
{
                x=arr14[i];
                for(int j=i+1;j<n;j++)
                {
                                if(x>arr14[j])
                                {x=arr14[j];
                                pos=j;}
                }
                arr14[pos]=arr14[i];
                arr14[i]=x;
                pos=n+1;  
}
int display[n];
for(int i=0;i<aa;i++)
{
display[i]=arr7[i];
}
for(int i=aa;i<ab;i++)
{
display[i]=arr8[i];
}
for(int i=ab;i<ac;i++)
{
display[i]=arr9[i];
}
for(int i=ac;i<ad;i++)
{
display[i]=arr10[i];
}
for(int i=ad;i<ae;i++)
{
display[i]=arr11[i];
}
for(int i=ae;i<af;i++)
{
display[i]=arr12[i];
}
for(int i=af;i<ag;i++)
{
display[i]=arr13[i];
}
for(int i=ag;i<ah;i++)
{
display[i]=arr14[i];
}
double p=time(NULL);
for(int i=0;i<n;i++)
{
                cout<<display[i]<<"\t";
}
      system("pause");
      system("cls");
      int median=n/2;
      cout<<"Median ="<<display[median]<<"\n";
      cout<<"Time Taken in Processing="<<p-m;
      system("pause");
      return 0;
}

You shouldn't put all your arrays in the stack, your program might cause stack overflow, especially when 'n' is chosen to be a large number. You may know what your program is doing, but did you consider what happens if someone else runs it and picks 'n' to be a huge number or 4, or less than 4? Another thing time_t is expected to be an integer type value, so there is no reason to store it in a double.

What to do instead

As Dean already said allocate the space in the heap. You can use pointers or containers from the stl. Still your arrays can be very large, so if you decide to use pointers, and manage the memory by yourself you need to ensure there is enough free space in the memory for your arrays at runtime, and if not, your program needs to handle the exception accordingly. I would also rethink the implementation of the algorithm, and I would try to simplify it as much as possible. The less the variables the easier to debug, and probably less resource heavy as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.