Reputation: 7
After finding the standard deviation I turn the int into a floating point value, yet I'm not sure if I did it right because it is not being printed out. I'm wondering if the problem is in the conversion
or in the cout
itself.
float standardDev(int num[], int count, int average)
{
int i,standardDev, sum =0;
for(i=0;count-1;i++)
{
sum = sum + (num[i] - average)<<2;
standardDev = sqrt((1.0/count)*sum);
}
std::cout << std::fixed;
float x = (float)standardDev; //int into floating point
cout << "The standard deviation is " ;
std::cout<< x;
cout << ".";
return x;
}
Currently nothing is being displayed from this function. After trying to display standardDev before turning it into a float, it seems that the main problem is in the math I am trying to do.
Upvotes: 0
Views: 126
Reputation: 12789
After finding the standard deviation (...)
I'm afraid that the posted code doesn't do that, considering how the loop is written:
for(i=0;count-1;i++)
{ // ^^^^^^^ This should be a condition like: i < count
// As written, the loop is either skipped (if count == 1, count-1 results in 0,
// which is interpreted as false) or endless (count != 1 -> count-1 is true)
sum = sum + (num[i] - average)<<2;
// ^^^ Nope. We'll see later.
standardDev = sqrt((1.0/count)*sum); // That's not how it is calculated and repeating
// it at every iteration is pointless
}
Note that an endless loop without side effects has undefined behavior, so that the compilers are allowed to ignore it.
On top of that, standardDev
(and average
) is an int
, which is later casted to a float
to be printed, while the result of std::sqrt
has already a floating point type.
<< 2 is basically supposed to be the shorter version of pow.
There are two problems, here. Given an integer x
, a bitwise left shift by two bits will effectively multiply it by 4 (assuming that it doesn't overflow), while you need to square the value, in your formula.
Also, due to operator precedence, the sum is performed before the bitshift.
Consider using a function, instead, something like
double square(double x)
{
return x * x;
}
Which would lead to the following
double standard_deviation(int num[], int count, double average)
{ // ^^^^^^
double sum = 0;
for(int i = 0; i < count; ++i)
{ // ^^^^^^^^^
sum += square(num[i] - average);
}
return std::sqrt(sum / (count - 1));
}
The next steps of improvements may be to use a standard container like std::vector
and some of the functions defined in <algorithm>
.
Upvotes: 1
Reputation: 21
The main problem with your code is in the for loop. You never specify i <= count - 1
Otherwise, it will print something, but not the right result. Here is your code. Now I recommend the following suggestions: turn the num array to floats, and average to float. Then compute it. But unless your nums need to be int and average is int, then you might have to do some type conversion. Here is my recommended code with not much change to your code.
// your code with correction
float standardDev(int num[], int count, int average)
{
int i,standardDev, sum =0;
for(i=0;i<=count-1;i++)
{
sum = sum + (num[i] - average)<<2;
standardDev = sqrt((1.0/count)*sum);
}
std::cout << std::fixed;
float x = (float)standardDev; //int into floating point
cout << "The standard deviation is " ;
std::cout<< x;
cout << ".";
return x;
}
// my code below
float standardDev(float num[], int count, float average){
int i = 0;
float sum, standardDev = 0;
for(i = 0; i <= count-1; i++){
sum += ((num[i] - average)*(num[i] - average));
standardDev = sqrt((1.0/count)*sum);
}
//standardDev = sqrt((1.0/((float) count))*sum);
std::cout << std::fixed;
cout << "The standard deviation is ";
std::cout<<standardDev;
cout<< ".";
return standardDev;
}
Hope I could help.
Upvotes: 0