#include #include #include using namespace std; const unsigned short MAX_SCORES = 100; float average( float [], unsigned short ); int main() { char choice, filename[81]; float scores[MAX_SCORES]; unsigned short n = 0; ifstream infile; cout << "Are scores to be entered from the keyboard (k) " << "or read from a file (f)?\nEnter your choice. "; cin.get( choice ); cout << endl; switch ( choice ) { case 'k': case 'K': cout << "Enter the scores. " << "Enter Crtl+Z on a line by itself when finshed.\n"; cin >> scores[n]; while ( !cin.eof() ) { n++; cin >> scores[n]; } break; case 'f': case 'F': cout << "Enter the filename.\n"; cin >> filename; cout << endl; infile.open( filename ); if ( infile.fail() ) { cout << "\n** File cannot be opened **\n"; system( "PAUSE" ); exit( EXIT_FAILURE ); } infile >> scores[n]; while ( !infile.eof() ) { n++; infile >> scores[n]; } break; infile.close(); default: cout << "You've entered an invalid choice. Goodbye.\n"; system( "PAUSE" ); exit( EXIT_FAILURE ); } cout << "Number of scores: " << n << "\n"; cout << "Average score: " << average( scores, n ); cout << "\n\n"; system( "PAUSE" ); return( EXIT_SUCCESS ); } float average( float scores[], unsigned short num_scores ) { float sum = 0.0F; for ( unsigned short i = 0; i < num_scores; i++ ) { sum += scores[i]; } return( sum / float( num_scores ) ); }