Question

Average and Standard Deviation of 4 numbers

**c++**

Write an input function to input 4 doubles. then write a function that computes the average of 4 doubles and returns the average and the standard deviation. Write a output routine that shows the 6 numbers. Please feel free to use sub-functions to break up your code.   

you can name the four variables a1, a2, a3, a4, or something equally simple as long as you are consistent.

Average (or mean) is the sum of the four variables divided by 4.

Standard Deviation is shown below.


do not use vectors or arrays

(x - mean) x is a set of numbers mean is the average of the set of numbers n is the size of the set o is the standard deviati
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<cmath>

using namespace std;

void getInput(double & d1,double & d2,double & d3,double & d4){
  
   cout<<"Enter 1st number: ";
   cin>>d1;
   cout<<"Enter 2nd number: ";
   cin>>d2;
   cout<<"Enter 3rd number: ";
   cin>>d3;
   cout<<"Enter 4th number: ";
   cin>>d4;
  
}

void getStats(double d1,double d2, double d3, double d4, double &avg, double &std ){
   double sum = (d1+d2+d3+d4)*1.0;
   avg = sum/4.0;
   double term;
   term = pow((d1-avg),2)+pow((d2-avg),2)+pow((d3-avg),2)+pow((d4-avg),2);
   std = pow(term/4.0,.5);
}

void display(double d1,double d2, double d3, double d4, double avg, double std ){
   cout<<"\nNumber 1: "<<d1;
   cout<<"\nNumber 2: "<<d2;
   cout<<"\nNumber 3: "<<d3;
   cout<<"\nNumber 4: "<<d4;
   cout<<"\nAverage: "<<avg;
   cout<<"\nStandard deviation: "<<std;
}

int main(){
   double d1,d2,d3,d4,avg,std;
   getInput(d1,d2,d3,d4);
   getStats(d1,d2,d3,d4,avg,std);
   display(d1,d2,d3,d4,avg,std);
   return 0;
  
  
}

Enter 1st number: 45.3 Enter 2nd number: 54.3 Enter 3rd number: 56.4 Enter 4th number: 64.3 Number 1: 45.3 Number 2: 54.3 Num

Add a comment
Know the answer?
Add Answer to:
Average and Standard Deviation of 4 numbers **c++** Write an input function to input 4 doubles....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a complete C++ program that is made of functions main() and rShift(). The rShift() function...

    Write a complete C++ program that is made of functions main() and rShift(). The rShift() function must be a function without return with 6 parameters. rShift() should do following. Shift the first 4 formal parameters' value one place to right circularly and send the updated values out to the caller function, i.e. main. Furthermore, after calling this function with first 4 actual parameters, say a1, a2, a3, a4, and these actual parameters should shift their value one place to right...

  • 4. Write a C++ program Write a program that takes as input five numbers and outputs...

    4. Write a C++ program Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are xi, Xa, x3, Xa, and xs, then the mean is x = (Xi + X2 + X3+ X4 + Xs)/5 and the standard deviation is: Your program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation. 30...

  • Write a program that computes the average and standard deviation of four scores. The standard deviation...

    Write a program 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. Use two functions. One function to calculate the average, this function should be with Return Type "double". Second Function should be to calculate standard deviation, this function should be with Return Type "void". Allow the program to continue until the user tells the program he/she is finished. This is for...

  • Write a program that takes as input five numbers and outputs the mean (average) and standard...

    Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are X_1, X_2, . X_3, X_4, X_5 then the mean is equal to X = (X_1 + X_2 + X_3+ X_4 + X_5)/5 and the standard deviation is: squareroot (Xl - X)^2 + (X2 - X)^2 + (X3 - X)^2 + (X4 - X)^2 + (X5 - X)^2 Your program must contain at least the following functions:...

  • (b) Write a Ruby function sigma that uses your function mean and computes the standard deviation of an arbitrary number of arguments. Function calls sigma (1,2,1,2) , sigma (1) and sigma should r...

    (b) Write a Ruby function sigma that uses your function mean and computes the standard deviation of an arbitrary number of arguments. Function calls sigma (1,2,1,2) , sigma (1) and sigma should respectively return 1 ,0,and "No arguments". (Hint: the standard deviation is the square root o variance, and variance is the mean value of squares minus the square of the mean value) (c) Write a Ruby function stat that computes and returns the mean value, standard deviation, and the...

  • 1. Write a program that reads in two arrays (a1 and a2) of numbers and creates...

    1. Write a program that reads in two arrays (a1 and a2) of numbers and creates a new array (a3) of numbers such that the new array contains all the unique numbers of a1 and a2. Assume the input array elements are unique elements. In the main function, ask the user to enter the length of each array, declare and reads in the numbers for each array, and calculate and display the output array. Example input/output #1: Enter the length...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • Write a program in C++ to calculate the average (mean) and the standard deviation of a...

    Write a program in C++ to calculate the average (mean) and the standard deviation of a list of numbers using vectors. Your program should... Prompt for the number of values the user wishes to enter Prompt the user to enter the values one at a time for the number of requested values into a vector Calculate and display the Mean and Standard Deviation The flow of your code should be similar to: int main() { /** Prompt the user and...

  • Please help! We use Java JGrasp. Write a Java program to take the input of 5...

    Please help! We use Java JGrasp. Write a Java program to take the input of 5 numbers and output the mean (average) and standard deviation of the numbers. If the numbers are x1, x2, x3, x4, and x5, the formula for mean is X = (x1 + x2 + x3 + x4 + x5)/5 And the formula for standard deviation is You can also break standard deviation down like this: 1. Work out the Mean (the simple average of the...

  • Create a mathlab code. Create MATLAB/Scilab code that calculates an average and a standard deviation of...

    Create a mathlab code. Create MATLAB/Scilab code that calculates an average and a standard deviation of 8 numbers. Use 2,0,3,0,6,9,4,0 as eight 1 digit numbers to be used for your calculation. Use the following formula for calculating the standard deviation. Use MATLAB function "std"/Scilab function "stdev" to calculate the standard deviation. Compare your result with the result produced by the function. Hypothesize the reason why they are different. (If you do not see a pattern, try different set of numbers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT