Output the Correct Standard Deviation

The program does compile the average and the mean correctly. I can't understand why the standard_deviation member function isn't applying the sqrt properly:

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

const int N = 10;

class Standard_Deviation
{
private:
double in[N];
double mean_avg , sum;
//double standard_deviation(int num);
double answer;

public:
void input(int num), mean(int num), E_sigma(int num);
double standard_deviation(int num);
int count;
};

void Standard_Deviation::input(int num)
{
for (int i = 0; i < num; i++)
{
cout << "Enter value " << i + 1 << " : ";
//Array
cin >> in[i];
}
cout << in[0] << " " << in[1] << " " << in[2] << " " << in[3]
<< " " << in[4] << " " << in[5] << " " << in[6] << " "
<< in[7] << " " << in[8] << " " << in[9] << " " << endl;
}

void Standard_Deviation::mean(int num)
{
double total = 0;
for (int i = 0; i < num; i++)

{ //total = total + in[i]
total += in[i];
}
//Output the mean
mean_avg = total / num;

//Display on screen
cout << "Total (LOC) is: ";
cout << in[0] + in[1] + in[2] + in[3] + in[4] + in[5] +
in[6] + in[7] + in[8] + in[9];
cout << "\nMean Average is: " << mean_avg << endl;
}

void Standard_Deviation::E_sigma(int num)
{
double sum = 0;
//double avg = mean_avg;
for (int i = 0; i < num; i++)
{

sum += pow((in[i] - mean_avg), 2); //This access the data field from private data fields
}
//display calculate the square distance (x_ xave)
cout << "The ten values minus Average squared is : " << setprecision(1) << fixed << sum << endl;

}
double Standard_Deviation::standard_deviation(int num)
{
answer = sum;
cout << "\nThe standard deviation is: " << endl;
return sqrt(((1 / 9) * sum));
}
int main()
{
Standard_Deviation SD;
int count;
cout << "Please enter number 10: ";
cin >> count;
cout << endl;
SD.input(count); SD.mean(count); SD.E_sigma(count); SD.standard_deviation(count); //calculating standard deviation.sigma count; standard deviation.stand_devn(count);
system("pause");

}
1
2
3
4
5
6
double Standard_Deviation::standard_deviation(int num)
{
	answer = sum;
	cout << "\nThe standard deviation is: " << endl;
	return sqrt(((1 / 9) * sum));
}
you return the result, you don't show it.
also, integer divisions returns an integer, so you return 0
I've been trying to figure out how to return the result, and I still can't figure it out. I'm using my C++ book as a reference and it doesn't have any good examples on that subject. I can do it without using a class interface, but I want to improve my C++ skills by defining member functions. I modified the code and it still doesn't work. I'm out of ideas.

double Standard_Deviation::standard_deviation(int num)
{
double answer;
cout << "\nThe standard deviation is: " << endl;
answer = sqrt(((1 / 9) * sum));
return 0;
}
I have 2 suggestions for you:

(1) what do you mean by "to return the result"? To return it to the code which has called the function Standard_Deviation()? Then you need to add the value you want to return after the "return" command. In your above example, you always return the constant "0"

If you want to "show" the result, you would have to feed it to the cout stream, see below.

(2) ne555 also mentioned you have a problem in your calculation. "1/9" is an integer division and the result is an integer. Since INT(0.1111...) =0, you calculate the SQRT(0*sum) which is also 0.

You would need to force the division to be floating numbers, have a look at the code below ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double answer;
    double sum=99.9;
    cout << endl;
    cout << "The answer is: " << endl;
    answer = sqrt(((1 / 9) * sum));
    cout << answer << " = result with Integer division 1/9" << endl;
    answer = sqrt(((1.0 / 9) * sum));
    cout << answer << " = result with float division 1.0/9" << endl;
    return 0;
}
I modified my code and it did not work. I got one error message "uninitialized local variable `answer` used". It doesn't like line: cout << answer << "\nThe standard deviation is: " << endl;. I've already initialized double answer in the class public interface and that didn't work. I then declared double answer in the private class field because it can store the variable object, but it computed the same error message. By the why olaf, your code snippet was helpful and informative, thank you for taking the time and showing me that code.
The code below is what I've changed so far. It looks good, don't know why it's not working.

double Standard_Deviation::standard_deviation(int num)
{
double answer;
cout << answer << "\nThe standard deviation is: " << endl;
answer = sqrt(((1.0 / 9) * sum));
return 0;
}
You are re-declaring answer above the cout line; this uninitialized variable (different from the member of the class with the same name) is what you are printing.
Shouldn't it be

1
2
3
4
5
6
double Standard_Deviation::standard_deviation(int num)
{
answer = sqrt(((1.0 / 9) * sum));
cout << answer << "\nThe standard deviation is: " << endl;
return 0;
}

... you need to calculate "answer" before you send it to cout.
You guys are awesome!! it worked. My program is outputting the correct mean and standard deviation. I would like to thank olaf, Zhuge, and ne555. Thanks for taking the time to help me with this program. I learned a lot while coding this program thanks to you guys.
Topic archived. No new replies allowed.