////////////////////////////////////////////////////////////////////////// // 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 // // // // Author Creation Date // // ====== ============= // // AY 30 November 1999 // // // // 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 using namespace std; #include #include "Array.h" #include "Text.h" const int MAXSIZE = 100; int main() { // declare and set Max size of the array of readings Array readings; readings.setSize(MAXSIZE); // declare and set Max size of the array of averages Array averages; averages.setSize(MAXSIZE); // initialise both arrays with zero's for (int i = 0; i> fileName1; cin.ignore(80, '\n'); ifstream myDataFile; myDataFile.open(fileName1); // 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: "; Text fileName2; cin >> fileName2; cin.ignore(80, '\n'); ofstream myResultsFile; myResultsFile.open(fileName2); // input actual readings from file for (int i=0; i< noOfReadings; i++) { myResultsFile << averages(i) << endl; } cout << "End of Readings"; myDataFile.close(); myResultsFile.close(); return 0; } // end main()