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
|
#include<iostream>
#include<cmath>
using namespace std;
double calcavg(double, double);
double variance(double, double, double);
int main()
{
double testvals[14]{ 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73 };
double size = 14.0;
double sum = 0.0;
double a = calcavg(sum, size);
cout << "Average of array elements is " << calcavg(sum, size) << endl;
cout << "Variance of array elements is " << variance(sum, size, a) << endl;
system("pause");
return 0;
}
double calcavg(double sum, double size)
{
double testvals[14]{ 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73 };
for (int i = 0; i<14; i++) //Loop which inputs arrays data and calculates its sum
{
sum = sum + testvals[i];
}
return sum / size;
}
double variance(double sum, double size, double a)
{
double testvals[14]{ 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73 };
for (int i = 0; i<14; i++) //Loop which inputs arrays data and calculates its sum
{
sum = sum + ((testvals[i] - a)*(testvals[i] - a));
}
return sum / size;
}
|