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
|
#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 sum, int numVals);
double stanDev(int vals[], double mean, int numVals);
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 sum, int numVals)
{
double dsum = (double)sum;
double dnumVals = (double)numVals;
return dsum / dnumVals;
}
double stanDev(int vals[], double mean, int numVals)
{
double sum = 0, dVals = 0, value = 0, variance = 0;
for (int i = 0; i<numVals; i++)
{
dVals = (double)vals[i];
value = (dVals - mean)*(dVals - mean);
sum += value;
variance = sum / (numVals);
}
return sqrt(variance);
}
|