# how to turn this into c++ code.

• 04-28-2004
InvariantLoop
how to turn this into c++ code.
1st part:
we have an array thats n=3

index element
x[0] = 2
x[1] = 4
x[2] = 6

Sum = 12
average = sum /n = 12/3=4
average =4

2nd part:
now this is kinda the tricky part
(x[0] - average)^2 ... => (2- 4)^2 =4
(x[1] - average)^2 ... => (4- 4)^2 =0
(x[2] - average)^2 ... => (6- 4)^2 =4

3rd part:
SumOfSquares = 4+0+4=8
result =SumOfSquares / (n-1) ..... 8 /(3-1)=4
ResultSquared= sqrt(4) =2

i know for the 1st part i need a loop to sum the values in the indeces then devide by n to get the average. the 2rd part is the most confusing for me as u have to subtract the average, square it and then u have to get the SumOfSquares for the 3rd part.

any idea how i could code this?
• 04-28-2004
jlou
The best way to go about this is one step at a time. First, right the shell of the program that starts with the array already set up but does nothing. Make sure it compiles. Then, add a loop that goes through each value in the array. Maybe call cout to print the value to the screen. Make sure it compiles and runs as expected. If it doesn't, fix it. Then move on to changing the loop to get the sum. Compile, run, and fix until it is right. Next, get the average and output it. Compile, run, and fix until it is right.

Then, add another loop for the second part. Since you will need the average from part 1 inside the loop, maybe just print out the value and the previously calculated average inside this new loop (the average will be the same every time). Compile, run, and fix until it is right. At that point, you will have all the information you need to do the calculation, so just do it. You should be able to figure it out by then. If you are having lots of trouble, at least you will have a compilable program to post and ask specific questions about.

Good luck!
• 04-28-2004
Zach L.
One thing to note... I'm not sure what the program is you're working on, but the ResultSquared seems a little fishy. The reason is, is you take that quantity (the sum of the squares of the difference of each element and the average), and then take the square root (incidentally, this is the sqrt( ) function in <cmath>/<math.h>) of it, then you have the standard deviation. If you square it instead, I'm not sure you have anything of much statistical significance. I'm not sure if this is a concern, but you might consider looking at the requirements again.

Cheers
• 04-29-2004
InvariantLoop
Zach, i dont understand what you mean? yes that suppose to give u the standard deviation

you are right though, the formula would b more readable if i turned it into
result =sqrt(SumOfSquares / (n-1)) ..... sqrt(8 /(3-1))=2
• 04-29-2004
jlou
> ResultSquared= sqrt(4) =16

should be:

> ResultSquared= sqrt(4) = 2

since of course the square root of 4 is 2, not 16.
• 04-29-2004
InvariantLoop
your probly right, excuse me for my ignorance as i dont really know how the sqrt actually works. what i meant is i need to take the square root of 4 which gives u 16 u know that symbols that looks like this
... /-----
\/ 4 =16 right? ignore the dots lol
• 04-29-2004
jlou
No, the square root of 4 is 2. Four squared is 16, but square root of four is 2. They are different (opposites actually).
Code:

```  ___ \/ 4  =  2  (square root of four)  2 4      = 16  (four squared)```
You want the first one (square root), so your answer would be 2.
• 04-29-2004
InvariantLoop
yes ur right heeh