/* Loops while( condition ) { statements } file: 3ex1.cpp (or used as 3ex1.appended) FALL 1998 ___________________________________ Jacob Y. Kazakia jyk0 September 12, 1998 Programming example 1 of unit #3 Recitation Instructor: J.Y.Kazakia Recitation Section 01 ___________________________________ Purpose: This program reads a sequence of test scores a, b, c, etc.... and calculates the average of them. The sequence is terminated when -1 is inserted as a test score. The scores entered as well as the average are outputted to a file called 3ex1report.txt with an appropriate heading. Algorithm: The score entered is stored in the variable float score. Then the variable sum ( which is float and initialized at the begining to zero ) is increased by the score: sum = sum + score or equivalently sum += score As the scores are entered an integer named count is increased so that the number of entries is registered. Finally we calculate average = sum / count and it is outputted. */ #include // header which contains the functions // needed for default input and output #include // header needed for file input output void main() { ofstream report ( "3ex1report.txt", ios :: out); float score ; // the test score entered float sum = 0; float average; // the average score int count = 0; // counter of scores entered . It starts as zero, // ends as the total number of scores. cout<<" \n\n Enter the first score (-1 to terminate) " ; cin >> score; report << "\n\n This is the list of scores entered \n\n"; while ( score >= 0) // read the scores and add them up { count = count + 1; report << " score # " << count << " is " << score << endl; sum = sum + score; cout << " \n Enter the next score ( -1 terminates) " ; cin >> score; } if ( count > 0 ) { average = sum / count; report << " \n\n number of scores entered : "<< count ; report << ". The average score is "<< average ; } cout << " \n \n Done! " ; cout << " \n\n Enter e(exit) to exit ..."; char hold; cin >> hold; } /* THIS IS THE OUTPUT TO THE DEFAULT WINDOW: Enter the first score (-1 to terminate) 97 Enter the next score ( -1 terminates) 83 Enter the next score ( -1 terminates) 76 Enter the next score ( -1 terminates) 95 Enter the next score ( -1 terminates) 88 Enter the next score ( -1 terminates) 34 Enter the next score ( -1 terminates) 56 Enter the next score ( -1 terminates) -1 Done! Enter e(exit) to exit ... THE CORRESPONDING FILE IS : This is the list of scores entered score # 1 is 97 score # 2 is 83 score # 3 is 76 score # 4 is 95 score # 5 is 88 score # 6 is 34 score # 7 is 56 number of scores entered : 7. The average score is 75.5714 */