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 !!