SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Function Basics
Exercise:
Programming Projects
Question:6 | ISBN:9780132846813 | Edition: 5

Question

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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.