1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include<iostream>
#include<cstdlib>
#include<iomanip>
using namespace std;
int sumAverage(int times, int value, double sum, double average);
void displaySumAverage(int displaySum, double displayAverage);
int main()
{
int times, value, loop;
double sum, average;
while(loop != 0)
{
loop = sumAverage(times, value, sum, average);
if (loop == 0)
break;
displaySumAverage(sum, average);
}
system("pause");
return 0;
}
int sumAverage(int times, int value, double sum, double average)
{
sum = 0;
cout << "How many numbers would you like to average? (Enter \"0\" to terminate) ";
cin >> times;
for(int count = 0; count < times; count++)
{
cout << " Enter a value to be added to the sum ";
cin >> value;
sum += value;
cout << endl ;
}
if (times!=0)
average = sum / times;
else
cout << "Program Terminated ";
return times;
}
void displaySumAverage(int displaySum, double displayAverage)
{
cout << " The sum of the entered values is: " << displaySum;
cout << " The average of the entered values is: " << setprecision(1) << showpoint << fixed << displayAverage <<endl;
return;
}
|