// FILE: CmpSmAvN.cpp // COMPUTES AND PRINTS THE SUM AND AVERAGE OF A COLLECTION OF DATA // ITEMS #include #include // Functions used ... // COMPUTES SUM OF DATA double compute_sum (int); // IN: number of data items // COMPUTES AVERAGE OF DATA double compute_ave (int, // IN: number of data items double); // IN: sum of data items // PRINTS NUMBER OF ITEMS, SUM, AND AVERAGE void print_sum_ave (int, // IN: number of data items double, // IN: sum of the data double); // IN: average of the data int main () { // Local data ... int num_items; // input: number of items to be added double sum; // output: accumulated sum of data double average; // output: average of data being processed // Read the number of items to process. cout << "Enter the number of items to process:"; cin >> num_items; // Compute the sum of the data. sum = compute_sum (num_items); // Compute the average of the data. average = compute_ave (num_items, sum); // Print the sum and the average. print_sum_ave (num_items, sum, average); return 0; } // Insert definitions for functions compute_sum, compute_ave, // and print_ave here. // COMPUTES SUM OF DATA double compute_sum (int num_items) // IN: number of data items // Pre: num_items is assigned a value. // Post: num_items data items read; their sum is stored in sum. // Returns: Sum of all data items read if num_items >= 1; //ÊÊÊÊotherwise 0.0. { // Local data ... double item; // input: contains current item being added double sum; // output: used to accumulate sum of data // ÊÊÊÊÊÊÊÊitems read // Read each data item and accumulate it in sum. sum = 0.0; for (int count = 0; count < num_items; count++) { cout << "Enter a number to be added: "; cin >> item; sum += item; } // end for return sum; } // end compute_sum // COMPUTES AVERAGE OF DATA double compute_ave (int num_items, // IN: number of data items double sum) // IN: sum of data // Pre: num_items and sum are defined; num_items must be // greater than 0. // Post: If num_items is positive, the average is computed as // sum / num_items; // Returns: The average if num_items is positive; otherwise, 0. { // Compute the average of the data. if (num_items < 1) // test for invalid input { cout << "Invalid value for num_items = " << num_items << endl; cout << "Average not computed." << endl; return 0.0; // return for invalid input } // end if return sum / double (num_items); // recast operand num_items } // end compute_ave // PRINTS NUMBER OF ITEMS, SUM, AND AVERAGE OF DATA void print_sum_ave (int num_items, // IN: number of data items double sum, // IN: sum of the data double average) // IN: average of the data // Pre: num_items, sum, and average are defined. // Post: Displays num_items, sum and average if num_items > 0. { // Display results if num_items is valid. if (num_items > 0) { cout.precision (2); cout.setf (ios::fixed); cout << "The number of items is " << num_items << endl; cout << "The sum of the data is " << sum << endl; cout << "The average of the data is " << average << endl; } else { cout << "Invalid number of items = " << num_items << endl; cout << "Sum and average are not defined." << endl; cout << "No printing done. Execution terminated." << endl; } // end if } // end print_sum_ave