Program Always Returning Zero

This program is giving me a major headache, while it should be very simple. It will only return 0 each time I run it. I have pulled the program apart and run each function separately in a driver program. Each function appears to work fine on its own, however for some confounding reason, it invariably returns the value of zero each time I run the program as a whole. I thank anyone in advance for pointing out what I must surly be missing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cmath>
void deviation(double& ave2);
void average(double& first, double& second, double& third, double& fourth, double& ave1);
void average2(double& first, double& second, double& third, double& fourth, double& ave1, double& ave2);
using namespace std;
int main()
{
    double first, second, third, fourth;
    double ave1, ave2;
    cout << "Enter four numbers." << endl;
    cin >> first;
    cin >> second;
    cin >> third;
    cin >> fourth;
    average(first, second, third, fourth, ave1);
    average2(first, second, third, fourth, ave1, ave2);
    deviation(ave2);
    cout << ave2;
    return 0;
}

void average(double& first, double& second, double& third, double& fourth, double& ave1)
{
    ave1 = (first + second + third + fourth) / 4;
}

void average2(double& first, double& second, double& third, double& fourth, double& ave1, double& ave2)
{
    ave2 = (first - ave1) + (second - ave1) + (third - ave1) + (fourth - ave1);
}

void deviation(double& ave2)
{
    cout << "The standard deviation of this set is " << sqrt(ave2) << ".";
}
void functions return nothing. You want them to return ave1, so they should be double functionname and should end with returning average.
That doesn't seem to change anything. The program still returns 0. Also, I learned in class that one can in fact put cout in a void function, and it would work perfectly fine. It is in the book examples. Here is my revised (but still non-functioning) code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cmath>
double deviation(double& ave2);
void average(double& first, double& second, double& third, double& fourth, double& ave1);
void average2(double& first, double& second, double& third, double& fourth, double& ave1, double& ave2);
using namespace std;
int main()
{
    double first, second, third, fourth;
    double ave1, ave2;
    cout << "Enter four numbers." << endl;
    cin >> first;
    cin >> second;
    cin >> third;
    cin >> fourth;
    average(first, second, third, fourth, ave1);
    average2(first, second, third, fourth, ave1, ave2);
    cout << "The standard deviation is " << deviation(ave2);
    return 0;
}

void average(double& first, double& second, double& third, double& fourth, double& ave1)
{
    ave1 = (first + second + third + fourth) / 4;
}

void average2(double& first, double& second, double& third, double& fourth, double& ave1, double& ave2)
{
    ave2 = (first - ave1) + (second - ave1) + (third - ave1) + (fourth - ave1);
}

double deviation(double& ave2)
{
    return sqrt(ave2);
}
I tried simplifying the program down some and it seems to work. Somethign got broke when you added more in.

or wait.. hmm.. I never changed the divided by 4. when i change it to divided by 2 it goes back to zero.

hmmm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cmath>
void deviation(double& ave2);
void average(double& first, double& second, double& ave1);
void average2(double& first, double& second, double& ave1, double& ave2);
using namespace std;
int main()
{
    double first, second;
    double ave1, ave2;
    cout << "Enter two numbers." << endl;
    cin >> first;
    cin >> second;
    average(first, second, ave1);
    average2(first, second, ave1, ave2);
    deviation(ave2);
    cout << ave2;
    return 0;
}

void average(double& first, double& second, double& ave1)
{
    ave1 = (first + second) / 4;
}

void average2(double& first, double& second, double& ave1, double& ave2)
{
    ave2 = (first - ave1) + (second - ave1);
}

void deviation(double& ave2)
{
    cout << "The standard deviation of this set is " << sqrt(ave2) << ".";
}
Last edited on
What compiler are you using? Because when I change the / 4 in the average function to / 2 to comply with the fact that there would be two numbers, it goes back to 0. I am starting to think that there must be something wrong with my complier. It seems that when a simple program should be perfectly correct, it either won't compile or won't compile properly.
Last edited on
Try writing it out by hand without code. I think the way you have it written will always return zero.

I mean you have

 
ave2 = (first - ave1) + (second - ave1);


try to fill in some values on that

say we input 2 and 4 making the average 3


(2 - 3) + (4 - 3 ) = 0


lets say we had 3 values using your code above


say we input 2, 4, and 6 making the average 4

(2 - 4) + (4 - 4) + ( 6 - 4) = 0


You are doomed to always have it at zero with the way it's written. The program is functioning fine. Just that formula you have written is wrong some how.


I tried googling standard deviation and I found this on wikipedia.

http://en.wikipedia.org/wiki/Standard_deviation


so using the 2, 4, 6 example with an average of 4

it should be

(2-4)squared + (4 - 4 ) squared + (6-4) squared giving you a result of 8

then the standard deviation would be the square root of (8 / 3)

at least I think thats how it should function

basically std deviation would = square root of (the sum of the square of the elements deviations divided by the number of elements)

not sure how to write that in mathy form.
Last edited on
I think you are right. I have adjusted the formula, and the program finally seems to be outputting sensible information. Thank you.
I think it should look like this. for standard deviation.

I doubled checked with a calculator. I probably should get a better one lol.

This works fine. You should be able to extend this to as many as you need.

oh, and if anybody knows how to express "squared" in c++ please educate me :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cmath>
void deviation(double& step2_result);
void average(double& first, double& second, double& third, double& ave1);
void step2(double& first, double& second, double& third, double& ave1, double& step2_result);
using namespace std;
int main()
{
    double first, second, third;
    double ave1,step2_result;
    cout << "Enter three numbers." << endl;
    cin >> first;
    cin >> second;
    cin >> third;
    average(first, second, third, ave1);
    step2(first, second, third, ave1, step2_result);
    deviation(step2_result);
    cout << step2_result;
    return 0;
}

void average(double& first, double& second, double& third, double& ave1)
{
    ave1 = (first + second + third) / 3;
    cout << ave1 << "\n";
}

void step2(double& first, double& second, double& third, double& ave1, double& step2_result)
{
    step2_result = ((first - ave1)*(first - ave1)) + ((second - ave1)*(second - ave1)) + ((third - ave1)*(third - ave1)); //adds up the value of deviation squared for each input
}

void deviation(double& step2_result)
{
    cout << "The standard deviation of this set is " << sqrt(step2_result/3) << "."; //make sure to divide by the number of inputs
}
Last edited on
Yeah, that's essentially what I did. Thanks.
You guys are both doing integer division for a double...
1
2
 ave1 = (first + second + third + fourth) / 4;
ave1 = (first + second + third) / 3;

10 / 3 = 3
10 / 3.0 = 3.3333

Always either
a) cast to a double/float
B) add '.' or ".0" after the number
c)add ".0f" to end of number
d) Add ".f" after the number.
What do you mean by returning 0 as a whole program?
Do you mean that the termination code of the program is always 0? (if so this is just because you've got return 0; at the end of you main function).

Otherwise what is your actual problem?
Topic archived. No new replies allowed.