Getting incorrect sum of square roots

Hi everyone,

Im working on an assignment which involves listing all the EVEN numbers from 20 to 60 (inclusive). I then have to calculate several summary stats, including the sum of the numbers, and the sum of the square roots of the numbers.

I've gotten the correct sum of numbers (20 + 22 + 24 etc...), but I am getting an incorrect answer for the sum of the square roots of these numbers.

Im including the code below.

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
int main()
{
int countdiv5 = 0, countpersq = 0, sumnumb = 0, sumsqrt = 0;
double avg53, avgsqrt;

cout << "NUMBER" << "\tDIVIS5" << "\tSQROOT" << "\tPRFCTSQ" << "\tFRTHROOT\n\n\n";

for(int numb = 20; numb <= 60; numb += 2){
        cout << numb;
            sumnumb += numb;

    if (numb%5==0){
        cout << "\t   *";
            countdiv5++;}

        cout << "\t\t" << sqrt(numb);
            sumsqrt =+ sqrt(numb);


    if(sqrt(numb) * sqrt(numb) == numb){
        cout << "\t\t\t*";
            countpersq++;}


        cout << "\t\t\t\t" << sqrt(sqrt(numb)) << endl;

}

cout << "\n\n\n\n";

cout << "The number of numbers that are divisible by five:   " << countdiv5 << endl;
cout << "The number of numbers that are perfect squares:    " << countpersq << endl;
cout << "Average of the numbers that are divisible by both 5 and 3:  " << avg53 << endl;
cout << "Average of the squre roots of all numbers :   " << avgsqrt << endl;
cout << "The sum of the numbers in the NUMBER column:   " << sumnumb << endl;
cout << "The sum of the numbers in the SQROOT column:   " << sumsqrt << endl;

cout << "\n\n\n\nFINSIHED!\n\n\n\n";
  


I keep getting 7 as the sum of the square roots of these numbers, which is not correct. It should be greater. What exactly am I doing wrong?

The compiler im using is codeblocks.

Please go easy on me, im a beginner.

Thank you in advance for any help
Last edited on
For convenience sake, the relevant part of the code is lines 16-17.

Notice, I do the exact same thing in lines 16-17 as I do in line 9-10. Yet, in the first case I get the correct answer, but in the second case (lines 16-17) I get an incorrect answer.

Am I missing something here?
closed account (D80DSL3A)
Look carefully at line 17. =+ ?
Ah, I see.

By the way, I messed up in my last post. The relevant lines ares actually 26-27. Its just that the format changes when I past it here from my file in codeblocks. Anyway, thank you for pointing out the silly mistake. I'll fix it.
closed account (D80DSL3A)
The code I see shows line 26 = blank line, line 27 = }. I see no problem on either of those lines.

Two problems I do see are:
Line 21. The 3 tabs won't print in most of the cases, so the output values will be misalligned. ie. sqrt(sqrt(numb) will appear under PRFCTSQ most of the time.

Line 17. sumsqrt is declared as an int, so most values won't be summed to it properly. Maybe it should be type double.
The code I see shows line 26 = blank line, line 27 = }. I see no problem on either of those lines.


This is weird...

On my phone, the line formatting completely changes, which is why you were seeing a black line and I was seeing something else on lines 26-27. Anyway, im on desktop now and I see what you are saying.

I fixed the problem, code is running fine now. However, I'd like to make a few tweaks using set width and set precision. I was hoping you could help me with that :)

I included the library (#include <iomanip>) as weel ass the statement: cout.setf(ios::fixed,ios::floatfield);

However, when I cout a setw(5), it doesn't do what its supposed to. From what I've learned so far, setw is supposed to move your output left or right from the edges. Its not doing that for me and I can't figure out why. Instead its mesing with my decimal places.

Any clue what im doing wrong? Here's the part of the code below:

1
2
3
4
5
6
cout << setw(2) << "The number of numbers that are divisible by five:   " << countdiv5 << endl;
cout << "The number of numbers that are perfect squares:    " << countpersq << endl;
cout << "Average of the numbers that are divisible by both 5 and 3:  " << avg53 << endl;
cout << "Average of the squre roots of all numbers :   " << avgsqrt << endl;
cout << "The sum of the numbers in the NUMBER column:   " << sumnumb << endl;
cout << "The sum of the numbers in the SQROOT column:   " << sumsqrt << endl;


closed account (D80DSL3A)
Someone else will have to help you with the output formatting.
I don't know it well at all. Too tedious.
I'm glad you got the rest worked out.
Someone else will have to help you with the output formatting.
I don't know it well at all. Too tedious.
I'm glad you got the rest worked out.


Thank you anyway for all your help!


Anyone else wanna take a crack?
Topic archived. No new replies allowed.