Thread: how to turn this into c++ code.

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    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?
    Last edited by InvariantLoop; 04-29-2004 at 01:48 PM.
    When no one helps you out. Call google();

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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!

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    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
    Last edited by InvariantLoop; 04-29-2004 at 01:48 PM.
    When no one helps you out. Call google();

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    > ResultSquared= sqrt(4) =16

    should be:

    > ResultSquared= sqrt(4) = 2

    since of course the square root of 4 is 2, not 16.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    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
    When no one helps you out. Call google();

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    yes ur right heeh
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM