Sum of odd numbers not correct in output - Help please!

Hi everyone,

I really need help with a programming assignment im doing. The assignment was to output a table of four columns: The first column lists all odd number from 11 to 25 (inclusive), the second column is the square of these numbers, the third is the cube of the original numbers, and the final column is the square root of the original numbers.

After doing all of this, we are asked to output the sum of all numbers in the first column, along with other summary stats.

Now, the table itself comes out absolutely correct. However, when I compile and run, the output shows the sum of the numbers as 160, when it should be 144. I've been trying for at least two days now to figure out what the problem is, but cannot figure it out.

I hope someone can help me with this.

By the way, the compiler im using is codeblocks. Below is my program.

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
38
39
40
41
42
43
#include <iostream>
#include <cmath>


using namespace std;

int main()
{
    int cnt = 11;
    int count = 0;
    double average;
    double average2;
    int sum = 0;
    double sum2 = 0;
    cout << "NUMBER" << "\tNSQ" << "\tNCUBE" << "\tSQROOT" << endl << endl;
    while (cnt <= 17)
        {cout << cnt;
        cout << "\t" << cnt * cnt;
        cout << "\t" << cnt * cnt * cnt;
        cout << "\t" << sqrt(cnt) << endl;
        cnt = cnt + 2;


count = count + 1;

sum = sum + cnt;
sum2 = sum2 + sqrt(cnt);
}

average = 1.0*sum/count;
average2 = 1.0*sum2/count;


        cout << "\n\n\nNumber of numbers is: " << count << endl;
        cout << "\nSum of numbers is: " << sum << endl;
        cout << "Sum of square roots is: " << sum2 << endl;
        cout << "\nAverage of numbers is: " << average << endl;
        cout << "Average of the square roots is: " << average2 << endl;

    cout << "\n\n\nFINISHED!\n\n\n\n";

}


Please keep it simple, im a beginner.

Thanks in advance for any help.
Sorry, you misunderstood my question. Im talking about the sum of all ODD numbers from 11 to 25. This equal to 144.

The first column of numbers is all odd numbers from 11 to 25. I need the sum of that entire column of numbers to be 144. But for some reason its coming out to 160.

Please help.
closed account (D80DSL3A)
Where does it go to 25? I see 17 in your code.

Regardless, get ready for a DUH moment!
Line 21 appears too soon. You increment cnt before using the number to find sum. The numbers you're adding therefore range from 13-27, not 11-25.
Sorry, that 17 was a typo. It supposed to be 25.

Regardless, I still dont understand what the problem is. Can you dumb it down a little more? Where do I put line 21 now?

Thank you.
closed account (D80DSL3A)
Regardless, I still dont understand what the problem is.

Well, I guess that's why you can't see the problem :)

Move line 21 to follow line 27, but before line 28. ie. put it just before the closing brace }.
Last edited on
Well, I guess that's why you can't see the problem :)

Move line 21 to follow line 27, but before line 28. ie. put it just before the closing brace }.


Thank you so much! That worked.

Topic archived. No new replies allowed.