////////////////////////////////////////////////////////////////////////// // name.c // // // // Purpose // // ======= // // The program obtains a finite set of readings from the user // // and calculates the "running averages" (which is a set // // of values where each value is the average amonst the // // readings read up to the current reading) // // The readings are temporarily stored in an array. // // The averages are stored in an array. // // C++ built-in arrays are used. // // // // Author Creation Date // // ====== ============= // // AY 20 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 set of averages (doubles) where the averages(i) is // // equal to the average value among all readings // // These averages are stored in another file, whose name is also // // entered by the user // // // ////////////////////////////////////////////////////////////////////////// #include #include #include using namespace std; int main() { const int MAXSIZE=100; // absolute max size of the array double readings[MAXSIZE]; // input data array declaration double averages[MAXSIZE]; // averages array declaration string fileName1,fileName2; // create storage for two file names (input out output files) 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 >> fileName1; cin.ignore(80, '\n'); ifstream myDataFile; // declare an input stream from a file myDataFile.open(fileName1.c_str()); // open the input stream from the file // input data from the file myDataFile >> noOfReadings; // input actual readings from file for (int i=0; i< noOfReadings; i++) { myDataFile >> readings[i]; } // find averages // initialise running total double total = 0; for (int i = 0; i < noOfReadings; i++) { total = total + readings[i]; averages[i] = total/(i+1); } // output the results cout << "Enter the name of the file where to store the averages (output) data: "; cin >> fileName2; cin.ignore(80, '\n'); ofstream myResultsFile; // declare an output sream to a file myResultsFile.open(fileName2.c_str()); //open the output stream to the file myResultsFile << noOfReadings << endl; // input actual readings from file for (int i=0; i< noOfReadings; i++) { myResultsFile << averages[i] << endl; } cout << endl << "Do you want to continue? (type 1 if yes; 0 if not) "; cin >> carryon; // close the files myDataFile.close(); myResultsFile.close(); } // end of while() cout << "End of Readings" << endl; return 0; } // end main()