/* Filename: msorttst.cpp Author: Br. David Carlson Date: April 26, 1998 Last Revised: November 28, 2001 This program asks the user for the number of items to place into an array, fills the array with that many random ints, prints the array, sorts it with a mergesort, and then prints the sorted array. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET g++ under Linux */ #include #include // needed for setw #include // needed for srand, rand functions #include // needed for time #include "msort.h" using namespace std; // Function prototypes: void PrintArray(IntArray NumArray, int Count); void FillArray(IntArray NumArray, int Count); int main(void) { int Count; IntArray NumArray; cout << "Enter the number of items to place in the array (1 to " << MAX << "): "; cin >> Count; while ((Count < 1) || (Count > MAX)) { cout << "Reenter. Use a number from 1 to " << MAX << "." << endl; cin >> Count; } FillArray(NumArray, Count); cout << "The original array of random numbers:" << endl; PrintArray(NumArray, Count); MergeSort(NumArray, 0, Count - 1); cout << endl << "The sorted array:" << endl; PrintArray(NumArray, Count); return 0; } /* Given: NumArray Array of ints, from index 0 to Count - 1 Count Number of ints in array NumArray. Task: To print all the ints in NumArray on the screen, 10 per line. Return: Nothing. */ void PrintArray(IntArray NumArray, int Count) { int k; for (k = 0; k < Count; k++) { cout << setw(5) << NumArray[k]; if ((k % 10 == 9) || (k == Count - 1)) cout << endl; } } /* Given: Count Number of ints to place into array NumArray. Task: To place into NumArray, at indices 0 to Count - 1, random ints between 0 and 500. Return: NumArray Array filled with Count data items. */ void FillArray(IntArray NumArray, int Count) { int k; srand(time(NULL)); // seed the random number generator // Fill NumArray with random numbers between 0 and 500 for (k = 0; k < Count; k++) NumArray[k] = (500.0 * rand()) / RAND_MAX; }