Thread: compute arithmetic mean without knowing the number of items

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    compute arithmetic mean without knowing the number of items

    Hello.

    I am working on a program that will compute arithmetic mean of certain number of real numbers (in C double) without knowing the number of items.

    I can think of two solutions.

    1) I can declare an array of doubles of huge length and then ask user to start entering its values. After writing the first and every next term then before continuing I can ask the user whether they want to continue adding new values, or it is the end already. That would work.

    2) Is there a way how to enter all values at the same time separated by space? Let's say that the user input could be:
    Code:
    2 5 8 10
    or
    Code:
    10 15 20.4 80 100
    or
    Code:
    1 2 3
    Maybe in the 2) solution I can consider the user's input as a string and work out from that the numbers to compute their mean.
    Is there a better way how to code the 2nd solution?
    Last edited by nerio; 02-11-2016 at 05:25 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    You might be able to read the values one by one, adding them to a running total, then in the end, divide by the number of values. This way, you do not need an array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    @laselight : reading one by one requires to know when do you stop. That requires to know number of items, which is not given. And asking when to stop after every entry is the 1) solution.

    Or a special value, let's say a negative one e.g. -2.00001, can be entered to signify that this is the end of entering.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    Quote Originally Posted by nerio
    reading one by one requires to know when do you stop. That requires to know number of items, which is not given.
    Then you simply stop when there are no more items to read, i.e., when the next read results in a read error due to end of file.

    If this is interactive input, then you could either require the user to trigger EOF, or designate a special value as the sentinel, e.g., a non-positive value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Thank you for replies. The entering of negative value I have already mentioned in post #3. Thank you for helping me to get to these thoughts.

    Original problem is solved now.

    I am also interested in knowing how to solve it using the 2). Is there a smart way how to proceed from the numbers input that user inputs at the same time? I am sure I saw it somewhere how to do it in C. I think it was without strings.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    Quote Originally Posted by nerio
    I am also interested in knowing how to solve it using the 2). Is there a smart way how to proceed from the numbers input that user inputs at the same time?
    Once you have the input in a string, you can parse the string with sscanf or strtod in a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arithmetic word number conversion problem
    By anonymous123 in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2012, 05:19 AM
  2. Counting number of items in an [][]array
    By A_User in forum C Programming
    Replies: 5
    Last Post: 11-20-2011, 07:54 PM
  3. How dynamically assign the number of items
    By zcrself in forum C Programming
    Replies: 2
    Last Post: 08-19-2009, 06:37 AM
  4. is knowing C enough !!
    By Meshal in forum Windows Programming
    Replies: 5
    Last Post: 11-30-2006, 09:52 AM
  5. complex number arithmetic
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2003, 09:04 AM