////////////////////////////////////////////////////////////////////////// // name.c // // // // Purpose // // ======= // // The program obtains a finite set of readings from the user // // and sorts the array of readings in ascending order using // // the method based on finding the smallest reading. // // The readings are temporarily stored in an array // // // // Author Creation Date // // ====== ============= // // AY 3 December 2002 // // // // Input/Output // // ============ // // The input is the number of readings (int) and the readings (double). // // The input is expected to be from the keyboard // // The output is array of readings sorted in ascending order // // // ////////////////////////////////////////////////////////////////////////// #include using namespace std; #include "Array.h" // function declaration void ascendSort(int n, Array & array); int main() { // declare the array Array readings; int maxSIZE; // number of readings cout << "Enter the largest possible number of readings: "; cin >> maxSIZE; // set the maxSize readings.setSize(maxSIZE); cout << "The largest possible number of readings is: " << readings.size() << " elements" << endl; int carryon = 1; // initialise flag to continue calculations while (carryon) { // initialise the array with zero's for (int i = 0; i> noOfReadings; cout << endl; // enter actual readings from keyboard cout << "Enter your readings, one at a time, following the prompt\n"; for (int i = 0; i < noOfReadings; i++) { cout << "reading(" << i+1 << ") = "; cin >> readings(i); } cout << endl; // calling the sorter ascendSort(noOfReadings, readings); // print the sorted array cout << "The readings sorted in ascending order: " << endl; for (int i = 0; i < noOfReadings; i++) { cout << "reading(" << i+1 << ") = "; cout << readings(i) << endl; } cout << endl << "Do you want to continue? (type 1 if yes; 0 if not) "; cin >> carryon; } // end of while cout << "End of Readings" << endl; return 0; } // end main() // function definition for sorting an array of n elements in // ascending order // Note the parameter passing method // the actual number of elements is passed as a value // the array is passed as a reference. Therefore // the actual array 'readings' is being sorted // under its formal parameter alias called 'array' void ascendSort(int n, Array & array) { double temp; // temp is used for swapping // the outer loop steps through each position - the // position is given by j - // inside the outer loop there is a call to the inner loop // which is used to find the next min value. // then this value is swapped with the element standing // on the j position in the array // // j acts as the counter for the START of finding the min for (int j=0; j < n-1; j++) { double minElem = array(j); int minNum = j; // the inner loop finds the min value in the // remaining (unprocessed) part of the array from j onwards for (int i = j+1; i < n; i++) { if (minElem > array(i)) { minElem = array(i); minNum = i; } } // end of inner for // swapping element on j with element on minNum temp= array(j); array(j) = array(minNum); array(minNum) = temp; } // end of outer for return; }