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.