Write a function that computes the average and standard deviation of four scores. The standard deviation is defined to be the square root of the average of the four values: (si– a)2 , where a is the average of the four scores s1, s2, s3, and s4 . The function will have six parameters and will call two other functions. Embed the function in a program that allows you to test the function again and again until you tell the program you are finished.
C++ program code:
#include <iostream>
#include <cmath>
using namespace std;
// Function to compute the average of four scores
double computeAverage(double score1, double score2, double score3, double score4)
{
return (score1 + score2 + score3 + score4) / 4.0;
}
// Function to compute the standard deviation of four scores
double computeStandardDeviation(double score1, double score2, double score3, double score4, double average)
{
double deviationSum = pow(score1 - average, 2) + pow(score2 - average, 2) +
pow(score3 - average, 2) + pow(score4 - average, 2);
double variance = deviationSum / 4.0;
return sqrt(variance);
}
//main method
int main()
{
//Declare variables
double score1, score2, score3, score4;
char choice;
do
{
// Input four scores from the user
cout << "Enter four scores: ";
cin >> score1 >> score2 >> score3 >> score4;
// Compute the average of the four scores
double average = computeAverage(score1, score2, score3, score4);
// Compute the standard deviation of the four scores
double standardDeviation = computeStandardDeviation(score1, score2, score3, score4, average);
// Output the average and standard deviation
cout << "Average: " << average <<endl;
cout << "Standard Deviation: " << standardDeviation <<endl;
// Ask the user if they want to test again
cout << "Do you want to test again? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
return 0;
}
Output of the program code:
Enter four scores: 10
20
30
50
Average: 27.5
Standard Deviation: 14.7902
Do you want to test again? (y/n): y
Enter four scores: 1
2
4
6
Average: 3.25
Standard Deviation: 1.92029
Do you want to test again? (y/n): n