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;
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");
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.
(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>
usingnamespace 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.
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.
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.