Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that includes two functions named calcAverage() and variance().

ID: 3626550 • Letter: W

Question

Write a program that includes two functions named calcAverage() and variance(). The calcAverage() function should calculate and return the average of the values stored in an array named testvals. The array should be declared in main90 and should include the values 89,95,72,83,99,54,86,75,92,73,79,75,82,73. 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 values returned from calcAverage() and variance() should be displayed using cout statements activated from within main().

Explanation / Answer

#include<iostream>
using namespace std;
double calcAverage(int testvals[],int size)
{
double sum=0;
for(int i=0; i<size; i++)
sum = sum+testvals[i];
return sum/size;
}
double variance(int testvals[],int size)
{
double avg = calcAverage(testvals,size);
double sum =0;
for(int i=0; i<size; i++)
sum = sum + (static_cast<double> (testvals[i])-avg)*(static_cast<double> (testvals[i])-avg);
return sum/size;
}
int main()
{
int a[14]= {89,95,72,83,99,54,86,75,92,73,79,75,82,73};
cout << "Average given by " << calcAverage(a,14) << endl;
cout << "Variance given by " << variance(a,14) << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote