Hi, I need help asap. The problem I am having is trying to search an array of 2,000,000 long Numbers. I have populated the array with random numbers and sorted the numbers in ascending order.
That I know how to do. What I don't know how to do is how to ask the user to enter a number, search the array for that number, and if the number is not found, re-populate the array with random numbers and search again. Do this until the number is found and output number to screen.
I have been trying to figure out how to do a search for 4 hours now, all I have come up with is a headache and 4 hours wasted. Here is the code that I have:
void swap(long &val1, long &val2)
long splitList(long longArray, long start, long stop)
long midValue, midPoint, mid, x;
for(x=start+1; x<=stop; ++x)
void sort(long longArray, int start, int stop)
midPoint=splitList(longArray, start, stop);
sort(longArray, start, midPoint-1);
sort(longArray, midPoint+1, stop);
const long arraySize = 2000000; //define array size
long longArray[arraySize]; //declare array
int x =0;
int rr = rand();
srand(time(NULL)); //creates a random seed using system clock
for (int i = 0; i < arraySize; i++)
longArray[i]=(rand()/rr*rand()/rr); //populates array with random numbers
cout<<"Which number do you want to search for?"<<endl;
while ((scan<size) && (longArray[scan]!=value))
I know the code is a mess at the moment, I will be cleaning it up, but this is what I have so far. The search that I have implemented is not producing the correct results. Can someone "fix" my search code and tell me why it is not producing the correct results? As you can see in the "COUT" statements in the main, I was outputting elements 1 and 2 so that I could search for those exact numbers. The thing is. I don't think my random number generator is working correctly either, since elements 1 and 2 are negative, and the rest of the elements are zero. I would REALLY appreciate some help.
THanks in advance