////////////////////////////////////////////////////////////////////////// // 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 16 November 2003 // // // // Input/Output // // ============ // // The input is the number of readings (int) and the readings (double). // // The input is expected to be from a file - the name of the file is // // entered by the user // // The output is the largest (double) and smallest reading (double) // // and their corresponding reading numbers // // // ////////////////////////////////////////////////////////////////////////// #include #include #include using namespace std; int main() { const int MAXSIZE=100; // absolute max size of the array double readings[MAXSIZE]; // array declaration double maxRead; double minRead; int maxNum; int minNum; string fileName; // create a filename storage for a string of characters int noOfReadings; // actual number of readings int carryon = 1; // initialise flag to continue calculations while (carryon) { // enter name of file with input data cout << "Enter the name of the file with readings data: "; cin >> fileName; cout << fileName << endl; ifstream myDataFile; myDataFile.open(fileName.c_str()); // input data from the file myDataFile >> noOfReadings; // input actual readings from file for (int i=0; i< noOfReadings; i++) { myDataFile >> readings[i]; } // find max and min //initialise current max and min maxRead = readings[0]; minRead = readings[0]; maxNum = 0; 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; 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()