#include #include #include using namespace std; bool isSorted(int* ar, int len); void initArray(int* ar, int len); void displayArray(int* ar, int len); void swap(int* ar, int pos1, int pos2); int main(int argc, char** argv) { srand(time(NULL)); // initialize random number generator const int NUM_TESTS = 1703; // number of tests to run on sort int errorCount = 0; // keeps track of how many tests fail int arraySize = 0; // keeps track of length of array int* nums = NULL; // pointer to array int sum = 0; // used to make sure array has not been corrupted // Run the code for the number of tests specified for (int test = 1; test <= NUM_TESTS; test++) { // Display the test number every 100 tests and at end if (test % 100 == 0 || test == NUM_TESTS) { cout << "\rTest number " << test; cout.flush(); } // Create an array of random size from 1 - 1000 arraySize = rand() % 1000 + 1; nums = new int[arraySize]; if (nums == NULL) { cout << "Error allocating space for array\n"; exit(EXIT_FAILURE); } // Fill array with random numbers initArray(nums, arraySize); // Get sum for array to check if it is same after sorting sum = sumArray(nums, arraySize); // Sort array sort(nums, arraySize); // Make sure array is sorted and sum is consistent if (sum != sumArray(nums, arraySize)) { cout << "Error: Array corrupted.\n"; errorCount++; } else if (!isSorted(nums, arraySize)) { cout << "Error: Array not sorted\n"; errorCount++; } // Free memory used by array delete[] nums; } cout << "\nNumber of tests: " << NUM_TESTS << '\n'; cout << "Number of errors: " << errorCount << '\n'; return 0; } /* Name: isSorted Input: ar: a pointer to an array of integers len: the length of the array Output: true if the array is sorted, otherwise false */ bool isSorted(int* ar, int len) { bool sorted = true; for (int i=0; i *(ar+i+1)) { sorted = false; break; } } return sorted; } /* Name: initArray Input: ar: a pointer to an array of integers len: the length of the array Output: None Side effect: The input array is sorted in ascending order. */ void initArray(int* ar, int len) { for (int i=0; i