HELP PLEASE

can anyone help me with this question please ?

write a program to compute the average of the ten numbers 1,4,9,...,81,100, that is, the average of the squares of the numbers from 1 to 10. (Hint: instead of printing each squares as it is computed, add it in to a variable sum which keeps track of the sum of all the squares, and then at the end, divide the sum variable by the number of numbers summed.)
if you keep track of the sum in a variable of type int, and divide by the integer 10, you'll get an integer-only approximation of the average (the answer should be 38). If you keep track of the sum in a variable of type float or double, on the other hand, you will get the answer as a floating-point number.

HERE IS WHAT I HAVE SO FAR AND I DON'T KNOW WHAT ELSE TO DO , ANY HELP WILL BE VERY MUCH APPRECIATE. "I'm new to coding"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
int main()
{
int i;
float sum = 0;
int n = 0;

for (i = 1; i <=10; i = i + 1)
{
	sum = sum + i * i;
	n = n + 1;
}

printf("The average is %f\n", sum/n);
return 0;

}
Last edited on
If you use C++ <iostream>. change line 14 to
 
    std::cout << "The average is " << sum/n << std::endl;


printf() is valid but you need #include <cstdio>
Cool , This is what I have now but I still not able to run the code and I don't know what is wrong :-(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
int main()
{
int i;
float sum = 0;
int n = 0;

for (i = 1; i <=10; i = i + 1)
{
	sum = sum + i * i;
	n = n + 1;

}

std::cout << "the average is " << sum/n << std::endl;

return 0;
I still not able to run the code


What do you mean by this? Please explain the problem in more detail.
Just needs a closing } at the end after the return 0; (unless that just didn't copy in to the forum post).

Othewise it runs ok for me here. http://ideone.com/X7H4Z6
Last edited on
Well, there's a closing brace missing right at the end. Other than that it works for me, see demo here https://ideone.com/Knzzvk

I still not able to run the code
could you state what problem or error message you get.

like , when I press the button to start debugging the code, the black window opens and closes very quickly.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
int main()
{
int i;
float sum = 0;
int n = 0;

for (i = 1; i <=10; i = i + 1)
{
	sum = sum + i * i;
	n = n + 1;

}

std::cout << "the average is " << sum/n << std::endl;

return 0;

}
Last edited on
closed account (j3Rz8vqX)
Create an exit prompt before return 0:
1
2
3
    //Exit prompt:
    std::cout << "Press <enter> to exit console: ";
    std::cin.get();
Wow, everything is working now. :-)

Thanks everyone for all the help , I do appreciate it a lot.

Topic archived. No new replies allowed.