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.
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?
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.
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.
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;