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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
// This program prompts the user to enter a quantity of values
// then prints the values the user entered, the average of the values,
// and the standard deviation of the values.
#include <iostream>
#include <cmath>
using namespace std;
const int VALSIZE = 100;
void checkVals(int &numVals);
void displayVals(int vals[], int numVals, int sum);
void getVals(int vals[], int numVals);
double average(int vals[], int numVals, int &sum);
double stanDev(int vals[], int numVals, int sum);
int main()
{
int numVals;
int sum = 0;
int vals[VALSIZE];
cout << "Please enter the number of values you would like(1-100): ";
cin >> numVals;
checkVals(numVals);
getVals(vals, numVals);
cout << endl;
displayVals(vals, numVals, sum);
cout << endl;
}
void getVals(int vals[], int numVals)
{
int index;
for(index=0; index<numVals; index++) {
cout << "Please enter value #" << index+1 << ": ";
cin >> vals[index];
}
}
void displayVals(int vals[], int numVals, int sum)
{
int index;
for(index=0; index<numVals; index++) {
cout << "Here is value #" << index+1 << ": ";
cout << vals[index] << ".\n";
}
cout << endl;
cout << "The average of your values is: ";
cout << average(vals, numVals, sum) << ".\n";
cout << endl;
cout << "The standard deviation of your values is: ";
cout << stanDev(vals, numVals, sum) << ".\n";
}
void checkVals(int &numVals)
{
while ((numVals < 0 ) || (numVals > 100 )) {
cout << "That is an invalid value entered!!!\n";
cout << "A valid value is between 0 and 100 inclusive!!!\n";
cout << "Please reenter the number of values that you would like!\n";
cin >> numVals;
}
}
double average(int vals[], int numVals, int &sum)
{
int i;
for(i=0; i<numVals; i++) {
sum+= vals[i];
}
return (double)sum/numVals;
}
double stanDev(int vals[], int numVals, int sum)
{
double sigmaSquared = 0.0;
int i;
double ave = average(vals, numVals, sum);
for(i=0; i<numVals; i++) {
sigmaSquared += pow((vals[i] - ave),2.0);
}
return sqrt(sigmaSquared/(numVals-1));
}
|