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

User Benson Lin
by
5.3k points

1 Answer

1 vote

Answer:

Following are the program in the C++ Programming Language.

#include<iostream> //set header file

using namespace std; //namespace

//set method for sum and average

double calcavg(int a[])

{

double t=0;

double average ;

cout<<"Arraylist is : "<<endl;

for(int i=0;i<14;i++) //set for loop

{ cout<<a[i]<<" ";

t = t+a[i];

}

cout<<endl<<endl;

average = t/14;

cout<<"total : "<<t<<endl; //print message with output

return average; //return the average

}

//set method for the variance

double variance(int a[],double average)

{

double t=0;

for(int i=0;i<14;i++) //set the for loop

{

a[i] = (a[i]-average) * (a[i]-average);

t = t+a[i];

}

double variance = t/14;

return variance; // return variance

}

int main() //define main method

{

double average;

double variances ;

//set array data type declaration

int testvals[]={89,95,72,83,99,54,86,75,92,73,79,75,82,73};

average = calcavg(testvals); //call the methods

variances = variance(testvals,average); //call the methods

cout<<"average is : "<<average<<endl; //print output

cout<<"variance is : "<<variances<<endl;//print output

return 0;

}

Output:

Arraylist is :

89 95 72 83 99 54 86 75 92 73 79 75 82 73

total : 1127

average is : 80.5

variance is : 124.429

Step-by-step explanation:

Here, we set the function "calcavg()" in which we pass integer data type array argument "a[]" in its parameter.

  • inside it, we set two double type variable "t" and assign its value to 0 and "average"
  • we set the variable "t" to find the total sum of the array
  • we set the variable "average" to find the average of the array.

Then, we set the function "variance()" in which we pass two argument, integer array type argument "a[]" and double type argument "average" in its parameter.

  • inside it, we find the variance of the array.

Finally, we define the "main()" function in which we call both the functions and pass values in its argument and then print the output.

User Rodius
by
5.9k points