In: Computer Science
Write a program that calculates mean and standard deviation for four user entered values. The most common measures of a sample are the mean and the standard deviation. the mean is the sum of the values divided by the number of elements in the data set. The dispersion - or spread in the values - is measured by the standard deviation The equation for the mean is x¯ = x1 + x2 + · · · + xn The equation for the standard deviation is sx = √ (x1 − x¯) 2 + (x2 − x¯) 2 + · · · + (xn − x¯) 2 n − 1 Your program will have the user enter four values (all doubles), You might call them x1, x2,x3,and x4. The program will then pass these four values to a function called mean that will calculate and return the mean of the four values. The mean and the four values will then be passed to a second function called stdev which will calculate and return the standard deviation of the four values. You program should be using functions. There will be a function printHeader that prints the splash screen, a function mean that calculates the mean, a function stdev that calculates the standard deviation, and a function printResults that prints the four values, their mean, and their standard deviation Your program must also include comments with your name, the date, and a short description of the project. It must also print this information as a splash screen at the beginning of the program run. It should print like:
X: 4.00, 7.00, 1.00, 10.00
Mean: 5.50
Std Dev: 3.87
This needs to be done in C++. This is what I have so far.
// Pound includes - will need the string and cmath
libraries
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
// Function Prototypes - Need to add three more. One
for mean, one for standard deviation, and one for the
printResults
void printHeader(string, string);
double mean(double, double, double, double);
double stdev(double, double, double, double, double);
int main(void) {
// Splash Screen
printHeader("20 October 2020", "Calculating
Statistics");
// Declare and Initialize all of the
variables. Will need doubles for the four data points, as well as a
double for the mean // and one for the standard deviation
double x1, x2, x3, x4;
double mean;
double std;
// Prompt for entering data - you must
enter four values
cout << "Enter the first value: ";
cin >> x1;
cout << "Enter the second value: ";
cin >> x2;
cout << "Enter the third value: ";
cin >> x3;
cout << "Enter the fourth value: ";
cin >> x4;
// Pass the four data variables to a function that
returns the mean
double m = mean (x1,x2,x3,x4);
// Pass the four data variables and the
mean to a function that returns the standard deviation
double std = stdev(m, x1, x2, x3, x4);
// Print the results
// Include the original values and the
mean and the standard
// DO NOT PRINT IT HERE - pass the variables to a printResults
function
return 0;
}
void printResults(double m, double s, double x1, double x2, double
x3, double x4){
cout << setprecision(2) << fixed <<
showpoint;
cout << "Values: " << setw (10) <<
x1 << ", " << x2 << ", " << x3 << ",
" << x4 << endl;
cout << "Mean: " << setw (10) << m
<< endl;
cout << "Standard Deviation: " << setw(10)
<< s << endl;
}
// Name:
// Date: 20 October 2020
// Function Definitions
void printHeader(string dueDate, string description) {
// Print the splash screen
cout << endl;
cout << "Name" << endl;
cout << "Class" << endl;
cout << dueDate << endl;
cout << description << endl;
cout << endl;
return;
}
// Function definition for mean
double mean (double x1, double x2, double x3, double x4) {
return (x1 + x2 + x3 + x4) / 4;
}
// Function definition for standard deviation
double stdev(double m, double x1, double x2, double x3, double
x4){
std = (m-x1)*(m-x1);
std + = (m-x2)*(m-x2);
std + = (m-x3)*(m-x3);
std + = (m-x4)*(m-x4);
std = std/3;
std = sqrt(std);
return std;
}
CODE -
// Pound includes - will need the string and cmath libraries
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
// Function Prototypes - Need to add three more. One for mean, one for standard deviation, and one for the printResults
void printHeader(string, string);
double mean(double, double, double, double);
double stdev(double, double, double, double, double);
void printResults(double, double, double, double, double, double);
int main(void) {
// Call function to print Splash Screen
printHeader("20 October 2020", "Calculating Statistics");
// Declare and Initialize all of the variables. Will need doubles for the four data points,
// as well as a double for the mean and one for the standard deviation.
double x1, x2, x3, x4;
double mean_value;
double std_dev;
// Prompt for entering data - you must enter four values
cout << "Enter the first value: ";
cin >> x1;
cout << "Enter the second value: ";
cin >> x2;
cout << "Enter the third value: ";
cin >> x3;
cout << "Enter the fourth value: ";
cin >> x4;
// Pass the four data variables to a function that returns the mean
mean_value = mean(x1,x2,x3,x4);
// Pass the four data variables and the mean to a function that returns the standard deviation
std_dev = stdev(mean_value, x1, x2, x3, x4);
// Pass the four data variables, the mean, and the standard deviation that prints the results.
printResults(mean_value, std_dev, x1, x2, x3, x4);
return 0;
}
// Name:
// Date: 20 October 2020
// Function Definitions
// Function definition for printing the splash screen
void printHeader(string dueDate, string description)
{
// Print the splash screen
cout << endl;
cout << "Name: " << endl;
cout << "Class: " << endl;
cout << dueDate << endl;
cout << description << endl;
cout << endl;
}
// Function definition for mean
double mean (double x1, double x2, double x3, double x4)
{
return (x1 + x2 + x3 + x4) / 4;
}
// Function definition for standard deviation
double stdev(double m, double x1, double x2, double x3, double x4)
{
return sqrt( ( (m-x1)*(m-x1) + (m-x2)*(m-x2) + (m-x3)*(m-x3) + (m-x4)*(m-x4) ) / 3);
}
// Function definition for printing results.
void printResults(double m, double s, double x1, double x2, double x3, double x4)
{
cout << setprecision(2) << fixed << showpoint;
cout << "X: " << setw (10) << x1 << ", " << x2 << ", " << x3 << ", " << x4 << endl;
cout << "Mean: " << setw (25) << m << endl;
cout << "Standard Deviation: " << setw(10) << s << endl;
}
SCREENSHOTS -
CODE -
OUTPUT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.