Thread: help with c++

  1. #16
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by kbpsu View Post
    i know how to figure out the average and range if the numbers are known. but for this problem, i am not sure how many numbers the user is going to input, so i dont know what number to divide by. and for the range, i dont know how to get the program to recognize which number was the max/min.
    yes, that's why i linked you the wiki page on how to calculate a moving average.

    the key to your assignment is to just keep track of things as you go along.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    so i dont know what number to divide by
    Count the loop iterations?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    The divide by will be different each iteration, which is why you need a simple counter which increases by one each time the user enters a number. The first number entered will just BE the average of course, but the second one will only get a 1/2 share. To add the fifth number into your ongoing average, you might use the following calculation:

    mean=mean*(4/5) + number*(1/5)

    If a sixth number is added, you'll need to update your average:

    mean=mean*(5/6) + number*(1/6)

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by rossipoo View Post
    The divide by will be different each iteration, which is why you need a simple counter which increases by one each time the user enters a number. The first number entered will just BE the average of course, but the second one will only get a 1/2 share. To add the fifth number into your ongoing average, you might use the following calculation:

    mean=mean*(4/5) + number*(1/5)

    If a sixth number is added, you'll need to update your average:

    mean=mean*(5/6) + number*(1/6)
    What you're saying is correct, but the exact implementation isn't -- (1/6) == 0. Since the counter is almost certainly an integer, you'll need a cast to double before doing the division.

    But I wouldn't do it that way anyway -- I'd just count the iterations and divide once at the end. All those intermediate divisions and additions would make me nervous about floating point noise.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #20
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    Oh yeah, duh, you can just divide the total at the end. You will need to go to a float though, unless the user happens to enter numbers that divide evenly, which isn't likely.

Popular pages Recent additions subscribe to a feed