////////////////////////////////////////////////////////////////////////// // name.c // // // // Purpose // // ======= // // The program obtains a finite set of readings from the user // // and calculates the largest and smallest reading. // // The readings are temporarily stored in an array. // // A C++ built-in array is used // // // // Author Creation Date // // ====== ============= // // AY 13 November 2003 // // // // 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 the largest (double) and smallest reading (double) // // as well as their corresponding reading numbers // // // ////////////////////////////////////////////////////////////////////////// #include using namespace std; // recursive function declaration double recMax(int start, int n, double array []); int main() { const int MAXSIZE=100; // absolute max size of the array double readings[MAXSIZE]; // array declaration int noOfReadings; // actual number of readings int carryon = 1; // initialise flag to continue calculations while (carryon) { cout << "Enter actual number of temperature readings (0> 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; // find max and min //initialise current max and min double maxRead = readings[0]; double minRead = readings[0]; int maxNum = 0; int minNum = 0; for (int i = 1; i < noOfReadings; i++) { if (maxRead < readings[i]) { maxRead = readings[i]; maxNum = i; } if (minRead > readings[i]) { minRead = readings[i]; minNum = i; } } // output the results cout << "The largest reading was " << maxRead << " - it was " << " reading no. " << maxNum+1 << endl; cout << "The smallest reading was " << minRead << " - it was " << " reading no. " << minNum+1 << endl; // recursive function call cout << "The largest reading found by recursive process is: " << recMax(0,noOfReadings,readings) << endl; cout << endl << "Do you want to continue? (type 1 if yes; 0 if not) "; cin >> carryon; } cout << "End of Readings" << endl; return 0; } // end main() // recursive function definition double recMax(int start, int n, double array []) { double curMax; if (start < n-1) { // recursive call case //cout << "recursive call case" << "curMax=" << curMax << endl; curMax=recMax(start+1,n,array); cout << "recursive call case" << "curMax=" << curMax << endl; if (array[start] > curMax) { curMax=array[start]; } } else // termination case { curMax=array[start]; cout << "termination case!" << "curMax=" << curMax << endl; } return curMax; }