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++. Below is the format:
//
// Name
// 1 October 2020
// Program to calculate the mean and standard
deviation
//
// 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);
int main(void) {
// Splash Screen
printHeader("1 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
// Prompt for entering data - you must
enter four values
// Pass the four data variables to a function that
returns the mean
// Pass the four data variables and the
mean to a function that returns
// the standard deviation
// 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;
}
// Function Definitions
void printHeader(string dueDate, string description) {
// PRINTHEADER void printHeader(string, string) prints
the splash screen
// using the two strings that are passed to the
function from the driver.
//
// Name:
// Date:
//
// Print the splash screen
cout << endl;
cout << "Your Name Here" << endl;
cout << "CMPSC 101" << endl;
cout << dueDate << endl;
cout << description << endl;
cout << endl;
return;
}
Here is the code:
// Name
// 1 October 2020
// Program to calculate the mean and standard deviation
// Pound includes - will need the string and cmath libraries
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
// Function Prototypes
double mean(double arr[], int n);
double stdev(double arr[], int n);
void printHeader(string, string);
void printResults(double arr[], int n, double mean, double sd);
int main ()
{
// Splash Screen
printHeader ("1 October 2020", "Calculating Statistics");
// variables initialization
int n;
cout << endl << "Enter the no. of elements: ";
cin >> n;
// double array
double arr[n];
// Getting elements for array
for (int i = 0; i < n; i++) {
cout << endl << "Enter element " << i+1 << ": ";
cin >> arr[i];
}
// mean
double cMean = mean(arr, n);
// standard deviation
double sd = stdev(arr, n);
printResults(arr, n, cMean, sd);
return 0;
}
// Function Definitions
void printHeader (string dueDate, string description)
{
// Print the splash screen
cout << endl;
cout << "Your Name Here" << endl;
cout << "CMPSC 101" << endl;
cout << dueDate << endl;
cout << description << endl;
cout << endl;
return;
}
// Calculates the mean of given array
double mean(double arr[], int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return (sum / n);
}
// Calculates standard deviation of array and return it
double stdev(double arr[], int n) {
// calculating mean
double calculatedMean = mean(arr, n);
double temp = 0;
// Calculating variance
for(int i = 0; i < n; i++)
{
temp += pow(arr[i] - calculatedMean, 2) ;
}
double var = temp / n;
return sqrt(var); // Standard deviation
}
// Print the results (mean, standard deviation) with array
void printResults(double arr[], int n, double mean, double sd) {
// Printing array
cout << endl << "X: ";
for (int i = 0; i < n; i++) {
cout << arr[i];
// Printing commas accordingly
if (i < n-1) {
cout << ", ";
}
}
// Printing mean and standard deviation
cout << endl << "Mean: " << mean;
cout << endl << "Standard Deviation: " << sd;
}
Output:
Your Name Here
CMPSC 101
1 October 2020
Calculating Statistics
Enter the no. of elements: 4
Enter element 1: 4
Enter element 2: 7
Enter element 3: 1
Enter element 4: 10
X: 4, 7, 1, 10
Mean: 5.5
Standard Deviation: 3.3541
Screenshot: