Determining and displaying a range?

This is a discussion on Determining and displaying a range? within the C Programming forums, part of the General Programming Boards category; I think this is a relatively simply question but I can't seem to figure it out. Assume a program in ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Determining and displaying a range?

    I think this is a relatively simply question but I can't seem to figure it out. Assume a program in which the user enters a collection of numbers. Using a while loop, the program calculates the sum and average for the collection of numbers. How do I go about getting the program to identify the smallest value within the collection, the largest value, and the subsequent range?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    While reading in the numbers, read in the first number into two variables (highestVal, lowestVal), then as you read in the rest of the values, check to see if the last value you read in is higher than what's in highestVal or lower than what's in lowestVal. If it is, replace it.

    Then in the end, just subtract lowestVal from highestVal to get your range.
    Sent from my iPadŽ

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    min=max=first input
    while input
    if input<min
    min=input
    else if input>max
    max =input


    range is min to max
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Yeah, basically what cbastard said. He just made it a little cluttered.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    I think the clutter was what I needed . The only issue I have now is that the minimum value is being registered as the value entered to end the program. This value isn't taken into account when calculating the average or the sum of the numbers however. Any ideas?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    I don't understand what you're saying in your second sentence, but why would the minimum value be taken account in calculating the average?
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    In the program, I am using a value that is outside the conceivable realm of the data entry to signal to the program to cease receiving input. So when I plug this number in, the program should take the previous numbers entered to determine the average, the range, etc. For some reason, the value to end the data entry is registering as the minimum value for the group. It is not, however, being used to determine the average, as it shouldn't be. Does that help?

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Read the input number into a variable called something clever like inputnum. Right after receiving that number check to see if it is one of the special end-the-input numbers. If it is, skip to the code that spits out the results. Otherwise, do your minval/maxval stuff on the input number and repeat the input process.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21