-1

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.

  • 1
    `int arr[n]= {rand() % 100}; ` only sets the first value. Also `int arr[n]` is not even legal `c++` – drescherjm Apr 27 '22 at 19:29
  • You probably wanted to reduce your code to a [mcve] instead of posting a large chunk of your code. Remember that the main purpose of your question is to help future readers solve a specific problem you have identified in your code. – drescherjm Apr 27 '22 at 19:33
  • Unrelated: Don't seed your random number generator more than once. You now seed it every time you call `mergeSortAlg`. It's better to move the seeding out to `main` and do it once there (even though you say it _"just acts as int main"_). – Ted Lyngmo Apr 27 '22 at 19:35
  • Regarding the code size, yeah I went ahead and deleted the extra functions and left just the main function being called. I wondered if there could be something in the functions that were also affecting it hence putting it in the first time. – Jewel Wildmoon Apr 27 '22 at 20:29
  • Side note: [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once). With `srand` being called from `mergeSortAlg` the code is set up to repeat the random sequence if called too quickly. – user4581301 Apr 27 '22 at 21:36

1 Answers1

2

You expect array initialization to work in a way it does not work. This

int arr[n]= {rand() % 100};

Initializes the first element with rand()%100 and the remaining elements are zero-initialized.

Moreover int arr[n] is not valid c++. See here: Why aren't variable-length arrays part of the C++ standard?.

You can use a std::vector when the size is only known at runtime and you can use a loop to roll n random numbers instead of just one:

std::vector<int> arr(n);
for (auto& e : arr) e = rand() % 100;

PS: Note that rand() is not the best way to generate random numbers. rand() is only loosely specificed and implementations are known to have issues. And on top of that rand() % N is not uniformly distributed (only if RAND_MAX % N == 0). You can find better random generators in <random>.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thank you! I used the vector with the rand and added the #include library since it gave an error for that. Though now I'm getting "cannot convert 'std::vector' to 'int' for argument '1' to 'void displayMergeArray(int*, int)" and 'cannot convert std::vector' to 'int' for argument '1' to 'void mergeSorting(int*, int, int)' for the function calls – Jewel Wildmoon Apr 27 '22 at 20:24
  • The types must match. `std::vector` isn't a pointer to `int`, but it does contain one that you can get access to with the `std::vector::data` method. – user4581301 Apr 27 '22 at 21:38
  • That said, you're probably better off changing the functions to `void displayMergeArray(vector & )`. `vector` knows how big it is, so there is no need to pass in the length. – user4581301 Apr 27 '22 at 21:40