I'm trying to create a program using Merge Sort for an assignment, using rand and srand to place random numbers in the array. The goal is to sort a large number of values entered manually by the user and count how long it takes the program in seconds to process it.
Everything works so far, but the one problem I'm getting is that a random value is only being assigned to the first position and the rest are given 0
This is the body of my merge function (main program) where I suspect the issue might be:
//Function that begins the Merge Sort option and acts as int main()
void mergeSortAlg(){
srand(time(NULL));
int n= 0;
cout << "\n\nPlease make a selection by typing one of the following:\n";
cout << "1000 \n10000 \n50000 \n\n";
cin >> n;
cout << "You have selected: " << n << "\n";
cout << "Stopwatch has started...\n\n";
//statement that begins counting the execution time for the program
auto start = chrono::steady_clock::now();
int arr[n]= {rand() % 100}; //generating random values between 1-99 in the array
int arr_size= sizeof(arr) / sizeof(arr[0]);
cout << "The array list before sorting is: \n";
displayMergeArray(arr, arr_size);
mergeSorting(arr, 0, arr_size-1);
cout << "\n\n\nThe array list after sorting is: \n";
displayMergeArray(arr, arr_size);
auto end = chrono::steady_clock::now(); //stopping the timer at the end of the merge sort
//printing the amount of time (in seconds) the program took to sort the values
cout << "\n\n\nTotal processing time elapsed:\n" << chrono::duration_cast<chrono::seconds>(end - start).count() << " seconds\n\n\n";
}
Note that the mergeSortAlg() function just acts as int main() since the main program currently is being used to call a menu function for 3 sorting options
Still kind of new to merging and C++ in general, so I'm not sure how to use rand to assign the number of random values needed based on how many positions are entered by the user.