//Shows the difference between each of a list of golf scores and their average. #include const int MAX_NUMBER_SCORES = 10; void fill_array(int a[], int size, int& number_used); //Precondition: size is the declared size of the array a. //Postcondition: number_used is the number of values stored in a. //a[0] through a[number_used-1] have been filled with //nonnegative integers read from the keyboard. double compute_average(const int a[], int number_used); //Precondition: a[0] through a[number_used-1] have values; number_used > 0. //Returns the average of numbers a[0] through a[number_used-1]. void show_difference(const int a[], int number_used); //Precondition: The first number_used indexed variables of a have values. //Postcondition: Gives screen output showing how much each of //the first number_used elements of a differ from their average. int main( ) { int score[MAX_NUMBER_SCORES], number_used; cout << "This program reads golf scores and shows\n" << "how much each differs from the average.\n"; cout << "Enter golf scores:\n"; fill_array(score, MAX_NUMBER_SCORES, number_used); show_difference(score, number_used); return 0; } //Uses iostream.h: void fill_array(int a[], int size, int& number_used) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } number_used = index; } double compute_average(const int a[], int number_used) { double total = 0; for (int index = 0; index < number_used; index++) total = total + a[index]; if (number_used > 0) { return (total/number_used); } else { cout << "ERROR: number of elements is 0 in compute_average.\n" << "compute_average returns 0.\n"; return 0; } } void show_difference(const int a[], int number_used) { double average = compute_average(a, number_used); cout << "Average of the " << number_used << " scores = " << average << endl << "The scores are:\n"; for (int index = 0; index < number_used; index++) cout << a[index] << " differs from average by " << (a[index] - average) << endl; }