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

JAVA I need to write a code that calculates mean and standard deviation using arrays and...
JAVA I need to write a code that calculates mean and standard deviation using arrays and OOP. This is what I have so far. I'm close but my deviation calculator method is throwing errors. Thanks so much :) import java.util.Scanner; import java.util.Arrays; public class STDMeanArray { private double[] tenUserNums = new double[10];//Initialize an array with ten index' private double mean; private double deviation; Scanner input = new Scanner(System.in);//create new scanner object /*//constructor public STDMeanArray(double tenUserNum [], double mean, double deviation){...
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
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 multithreaded program that calculates various statistical values for a list of numbers. This program...
Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the third will determine the minimum value. For example, suppose your program is passed the integers 90 81 78 95 79 72 85 The program will report The...
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...
Write a java program that calculates a speeding fine. The user is prompted for the speed...
Write a java program that calculates a speeding fine. The user is prompted for the speed of the vehicle, the posted speed limit and if the offence took place in the construction zone. The fine is calculated at $75 + 3$/ km over the speed limit for the first 20km over + $6 / km for the next 20 and $9/km after that. If the posted limit is 40km, the fine is doubled. If the offence took place in a...
*****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.
*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 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;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT