Write a program that includes two functions named calcavg( )and variance (). The
ID: 3764138 • Letter: W
Question
Write a program that includes two functions named calcavg( )and variance (). The calcavg() function should calculate and return the average of the values stored in an one-dimensional array named testvals. This array with 14 elements should be declared in main ( ) and the values of its elements should be entered by the user. The variance () function should calculate and return the variance of the data. The variance is obtained by subtracting the average from each value in testvals, squaring the values obtained, adding them, and dividing by the number of elements in testvals. The array testvalsas well as the number of the elements in testvalsshould be passed to each of the functions calcavg ()and variance ()as arguments. The values returned from calcavg () and variance () should be displayed using coutstatements in main ().Explanation / Answer
#include <iostream>
using namespace std;
double calcavg(int *testvals,int size)
{
int sum = 0;
for(int i=0;i<size;i++)
sum = sum+testvals[i];
return sum*1.0/size;
}
double variance(int *testvals,int size)
{
double avg = calcavg(testvals, size);
double sum = 0;
for(int i=0;i<size;i++)
sum = sum+((avg-testvals[i])*(avg-testvals[i]));
return sum/size;
}
int main(void) {
int testvals[14];
for(int i=0;i<14;i++)
{
cout << "Input value number " << i+1 << " : ";
cin >> testvals[i];
}
cout << "Average is : " << calcavg(testvals, 14) << endl;
cout << "Variance is : " << variance(testvals, 14);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.