Calculating mean and standard deviation

I have to make a program that calculates the mean and standard deviation of 5 numbers but I keep getting a ridiculously big number for the mean and -2 for the standard deviation, I need to use two functions to do this, I feel like I am on the right track but Im not to sure whats wrong here

#include <iostream>
#include <cmath>
using namespace std;

int average (int x1, int x2, int x3, int x4, int x5);
int deviation (int x1, int x2, int x3, int x4, int x5);
int main()
{
int x1, x2, x3, x4, x5, x, y;
cout << "Enter 5 integers" << endl;
cin >> x1 >> x2 >> x3 >> x4 >> x5;

average(x1, x2, x3, x4, x5);
cout << "The average is: " << x << endl;

deviation(x1, x2, x3, x4, x5);
cout << "The standard deviation is: " << y << endl;
}

int average(int x1, int x2, int x3, int x4, int x5)
{
int x = (x1+x2+x3+x4+x5)/5;
return x;
}

int deviation(int x1, int x2, int x3, int x4, int x5)
{
int x;
int y = sqrt(pow ((x1-x),2)+pow((x2-x),2)+pow((x3-x),2)+pow((x4-x),2)+pow((x5-x),2))/5;
return y;
}
Hi,

Please always use code tags : http://www.cplusplus.com/articles/z13hAqkS/

Integer division doesn't work well here, and nor does it for sqrt and pow, make your types double.
The sqrt and pow functions have overloads that take ints as arguments, but it returns a double. But then this is implicitly cast back to int again in your functions.

You call your functions but don't assign the return value to anything.

In the deviation function the local variable x is un-initialised. That's a golden rule: Always make sure variables are initialised :+) The x needs to be sent to the function as an argument, not as a local variable. Would Mean be a better name for this variable?

I always put number before and after the decimal point for a double literal, it has the effect of forcing that part of the expression to be of type double. You should avoid having magic numbers like 5 in your code, make them a const variable instead:

const double size = 5.0;

Send that variable to any function which needs it, don't make it global.

Your function parameters should be const, it means the compiler can enforce the idea you won't be changing them inside the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
double deviation(const double Mean,
                const double size,
                const double x1, 
                const double x2, 
                const double x3, 
                const double x4, 
                const double x5)
{
int x;
double StdDeviation = sqrt(  ( (x1-Mean) * (x1-Mean) )+( (x2-Mean) * (x2-Mean) )+( (x3-Mean) * 
(x3-Mean) )+( (x4-Mean) * (x4-Mean) )+( (x5-Mean) * (x5-Mean) )  ) / size;
return StdDeviation;
} 


It 's probably more tidy to work out the squares of the residuals individually first, then calc the deviation.

The pow function probably uses a binomial series to calculate it's answer, so it's terribly inefficient for squaring a number, so its better to just multiply, that's what I have done in the example above:

It won't make any difference for this small program, but there you go - an idea for the future !

Have you learnt about arrays yet? That would make this a lot easier.

Good Luck !!
Last edited on
Its telling me now I cant use mean as a function in line 12 and I dont get why

#include <iostream>
#include <cmath>
using namespace std;

double mean (double x1, double x2, double x3, double x4, double x5);
double deviation (double x1, double x2, double x3, double x4, double x5);
int main()
{
double x1, x2, x3, x4, x5, mean, stdDeviation;
cout << "Enter 5 numbers" << endl;
cin >> x1 >> x2 >> x3 >> x4 >> x5;
mean(x1, x2, x3, x4, x5);
cout << "The average is: " << mean << endl;

deviation(x1, x2, x3, x4, x5);
cout << "The standard deviation is: " << stdDeviation << endl;
}

double mean(double x1, double x2, double x3, double x4, double x5)
{
double size = 5.0;
double mean = (x1+x2+x3+x4+x5)/size;
return mean;
}

double deviation(const double mean, double x1, double x2, double x3, double x4, double x5)
{
double size = 5.0;
double stdDeviation = sqrt(((x1-mean)*(x1-mean))+((x2-mean)*(x2-mean))+((x3-mean)*(x3-mean))+((x4-mean)*(x4-mean))+((x5-mean)*(x5-mean)))/size;
return stdDeviation;
}
Last edited on
Please always use code tags : http://www.cplusplus.com/articles/z13hAqkS/

If you have compiler errors, then post them here. If you had code tags, I could compile it with cpp.sh.

Again:
TheIdeasMan wrote:
You call your functions but don't assign the return value to anything.
Topic archived. No new replies allowed.