Usenet post to alt.comp.lang.learn.c-c++ on 17.2.2009 > > Hello, > > I have written a program (from exercise 13 in You can Program in > > C++). The program is supposed to compute the 'Root Mean Square' of > > a set of values. Unfortunately , i dont know a lot about maths at > > the moment so i thought i better post my program here to check its > > giving me the correct output. > > > > This is the logic i used to write the program. > > 1. Identify the Number of values that are to be input > > 2. Make a total of the Square roots > > 3. Then identify the average of the total (ie the Arithmentic > > mean) 4. Identify the 'root mean square' - the square root of the > > arithmetic mean. Richard Heathfield replied: Not quite. I'll use your test values to indicate the procedure for calculating RMS. > > I tested my program with these three values: 5 , 8 and 12. Step 1: Square each value, and sum the squares: n = 0 ---- number of data points, used later Sum = 0.0 x = 5.0 ++n ---- n is now 1 x = x * x ---- x is now 25.0 Sum += x ---- Sum is now 25.0 x = 8.0 ++n ---- n is now 2 x = x * x ---- x is now 64.0 Sum += x ---- Sum is now 89.0 x = 12.0 ++n ---- n is now 3 x = x * x ---- x is now 144.0 Sum += x ---- Sum is now 233.0 Step 2: Divide by the number of data points: Mean = Sum / n ---- Mean is now 233.0 / 3 = 77.6667 (ish) Step 3: Take the square root: rms = sqrt(77.6667) ---- rms is now 8.81287 _________________________ In writing this program, I encountered the concept of 'Root Mean Square' for the first time. My initial attempt to write the program was flawed as I was not able to clearly and correctly identify the steps needed to calculate the value. Instead of simply producing a sum of the squares of all values entered, I attempted to generate a sum of all the square roots of the values entered. Once the error was pointed out, I corrected the program code.