/* ============================================================================ Name : DynamicArrays.c Author : Matt Boutell, David Mutchler and YOUR-NAME-HERE Description : An exercise to introduce dynamic arrays. ============================================================================ */ #include #include #include "DynamicArrays.h" int main(void) { // Declare a dynamic array of floats, along with other variables as needed. float arr[3]; int arrayLength = 3; while (1) { // Run until user enters a negative number for the array size. // Call and test getArrayLengthFromUser1 // Call and test getArrayLengthFromUser2 if (arrayLength < 0) { return EXIT_SUCCESS; } fillArrayWithRandomNumbers(arr, arrayLength); selectionSort(arr, arrayLength); printArray(arr, arrayLength); } } // Asks the user for the length of the array and returns the response. // Does NOT do any error-checking for valid input. XXX getArrayLengthFromUser1(XXX) { } // Asks the user for the length of the array and returns the response, // via the argument pointer. // Does NOT do any error-checking for valid input. XXX getArrayLengthFromUser2(XXX) { } // Fills the array with random numbers between 0 and 1. void fillArrayWithRandomNumbers(float* arr, int arrayLength) { int k; for (k = 0; k < arrayLength; ++k) { arr[k] = (float) rand() / (float) RAND_MAX; } } // Sorts the given array. void selectionSort(float* arr, int arrayLength) { int j; int k; int minJ; float temp; for (j = 0; j < arrayLength - 1; ++j) { minJ = j; for (k = j + 1; k < arrayLength; ++k) { if (arr[k] < arr[minJ]) { minJ = k; } } temp = arr[j]; arr[j] = arr[minJ]; arr[minJ] = temp; } } // Prints the elements of the array, each on a line by itself. void printArray(float* arr, int arrayLength) { int k; for (k = 0; k < arrayLength; ++k) { printf("%f\n", arr[k]); } fflush(stdout); }