Root Mean Square

I have asked to find the Root means Square of the given series of numbers. -99 to exit from the program.
But when I execute the program every time I get unbelievable answers like 57, 70.
This is the program.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int no;
int count=0;
int sum=0;
int Sqno=0;
while(no != -99)
{
cout<<"Enter no\n";
cin>>no;

count++;
Sqno=pow(no,2);
sum=sum+Sqno;
}
RMS=sqrt(sum/count);
cout<<RMS;

return 0;



}
You're using no while(no != -99) before you define it.

BTW, if you select your code and then click the <> button on the right it will format it making it easier to read. Indenting properly is nice too. It's just cosmetic but it makes it easier to get help when you have more complex code. ;-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int no;
int count=0;
int sum=0;
int Sqno=0;
while(no != -99)
{
cout<<"Enter no\n";
cin>>no;

count++;
Sqno=pow(no,2);
sum=sum+Sqno;
}
RMS=sqrt(sum/count);
cout<<RMS;

return 0;

}


EDIT: Now that I see it better I think you may have other issues...
Last edited on
You get the wrong answer because you include -99 in your calculation.

You forgot to define the RMS variable.

You forgot to initialize the no variable.
sum and count are both ints, so your calculation of RMS will be based on integer division. You should cast one of these values to a double.

RMS=sqrt(double(sum)/count);

And though using the pow() function is cool, I would probably just go ahead and assign:

Sqno = no * no;

One more thing. When posting code, please use the code tags. They show up as "<>" in the Format box.
Last edited on
Topic archived. No new replies allowed.