Question

In: Computer Science

Write a program that calculates mean and standard deviation for four user entered values. The most...

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;
}

Solutions

Expert Solution

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:


Related Solutions

Write a program named MakeChange that calculates and displays the conversion of an entered number of...
Write a program named MakeChange that calculates and displays the conversion of an entered number of dollars into currency denominations—twenties, tens, fives, and ones. For example, if 113 dollars is entered, the output would be twenties: 5 tens: 1 fives: 0 ones: 3. Answer in C# (P.S i'm posting the incomplete code that i have so far below.) using System; using static System.Console; class MakeChange { static void Main() { int twenties, tens, fives, ones; WriteLine("Enter the number of dollars:");...
Write a program that asks the user for an angle, entered in radians. The program should...
Write a program that asks the user for an angle, entered in radians. The program should then display the sine, cosine, and tangent of the angle. (Use the sin, cos, and tan library functions to determine these values.) The output should be displayed in fixed-point notation, rounded to four decimal places of precision Take your previous Angle Calculator program and modify it to do a table of trig values. The columns will be: Degrees, Sine, Cosine, Tangent,. And the rows...
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
*****Using Java Write a program that finds the standard deviation while also using a graphical user...
*****Using Java Write a program that finds the standard deviation while also using a graphical user interface.
Write a program that calculates the average of upto 100 English distances input by the user....
Write a program that calculates the average of upto 100 English distances input by the user. Create an array of objects of the Distance class, as in the ENGLARAY example in this chapter. To calculate the average, you can borrow the add_dist() member function from the ENGLCON example in Chapter 6. You’ll also need a member function that divides a Distance value by an integer. Here’s one possibility: void Distance::div_dist(Distance d2, int divisor) { float fltfeet = d2.feet + d2.inches/12.0;...
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
Write a program that calculates the mean, median and mode of a given array in the...
Write a program that calculates the mean, median and mode of a given array in the following manner: i) Write three functions mean (), median () and mode () that calculates the mean, median and mode of an array. ii) Using these functions write a program that calculates all the three above mentioned values for two different arrays A and B where A is an array that contains the number of hours spent by a person each day for playing...
A distribution of values is normal with a mean of 190 and a standard deviation of...
A distribution of values is normal with a mean of 190 and a standard deviation of 15. From this distribution, you are drawing samples of size 35. Find the interval containing the middle-most 82% of sample means: Enter your answer using interval notation. In this context, either inclusive or exclusive intervals would be acceptable. Your numbers should be accurate to 1 decimal places. Answers obtained using exact z-scores or z-scores rounded to 3 decimal places are accepted.
A distribution of values is normal with a mean of 114.8 and a standard deviation of...
A distribution of values is normal with a mean of 114.8 and a standard deviation of 98.5. Find the probability that a randomly selected value is between 16.3 and 26.2
A distribution of values is normal with a mean of 80 and a standard deviation of...
A distribution of values is normal with a mean of 80 and a standard deviation of 18. From this distribution, you are drawing samples of size 12. Find the interval containing the middle-most 88% of sample means: Enter your answer using interval notation. In this context, either inclusive or exclusive intervals would be acceptable. Your numbers should be accurate to 1 decimal places. Answers obtained using exact z-scores or z-scores rounded to 3 decimal places are accepted.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT