Question #4220

If X[0], X[1],....., X[N-1] is a list of N numbers and the mean (average) of these numbers is M, then we define their standard deviation to be the square root of the number:
((X [0] - M) ^ 2 + (X [1] - M) ^ 2 + ... + (X [N] - M) ^ 2 / (N - 1).
Use a loop to allow a user to input up to 10 numbers in an array. The user should indicate that he is done by entering 0. Find the mean (average) of these numbers. Then use the mean to find the standard deviation. Your output should display both the mean and the standard deviation.
Name your variables as follows:
* the array of numbers should be X [10]
* the first sum should be named Sum1
* the average (mean) should be Mean
* the second sum should be named Sum2
* the standard deviation should be StandardDeviation
* the number to be input by the user should be Num
* counters an be named as desired; I like Count, K or J

Expert's answer

#include <iostream>

#include <math.h>

using namespace std;

int main() {

int x[10];

int num;

for (int i = 0; i<10; i++) {

cin>>x[i];

if (x[i] == 0) {

num = i;

break;

}

}

int sum1 = 0;

for (int i = 0; i<num; i++)

sum1 += x[i];

float mean = sum1/num;

float sum2;

for (int i = 0; i<num; i++)

sum2 += pow((x[i] - mean),2);

float standarddeviation = pow(sum2,0.5);

cout<<"average = "<<mean<<endl;

cout<<"standard deviation = "<<standarddeviation<<endl;

return 0;

}

#include <math.h>

using namespace std;

int main() {

int x[10];

int num;

for (int i = 0; i<10; i++) {

cin>>x[i];

if (x[i] == 0) {

num = i;

break;

}

}

int sum1 = 0;

for (int i = 0; i<num; i++)

sum1 += x[i];

float mean = sum1/num;

float sum2;

for (int i = 0; i<num; i++)

sum2 += pow((x[i] - mean),2);

float standarddeviation = pow(sum2,0.5);

cout<<"average = "<<mean<<endl;

cout<<"standard deviation = "<<standarddeviation<<endl;

return 0;

}

## Comments

## Leave a comment