Shortening long if statements

This is a discussion on Shortening long if statements within the C Programming forums, part of the General Programming Boards category; I have to keep track of smallest and largest numbers of a set of five. I wondered if instead of ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    Shortening long if statements

    I have to keep track of smallest and largest numbers of a set of five. I wondered if instead of something like (psudocode):

    if num1 > num2
    largest number is 1
    if num2 > num3
    largest number is 2

    ... and so on comparing all 5 numbers all to each I wondered if there is a quicker way, I'm sure there must be. Any help is appreciated, it really is.
    Thanks,
    Extro.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use an array. Walk through the array picking out the biggest and smallest.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    68
    The teacher says
    " You need a variable that stores the currently smallest and largest and we will need to compare the incoming digit to these values and replace them if necessary. Don't forget to initialize the smallest to highest value possible and largest to lowest value possible value"

    You think he is reffering to an array?

    thanks,
    Extro

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Where are these numbers coming from? Are you reading them from a file? From the keyboard? Setting them up manually at compile-time with an array? Are you storing them some place? It depends on what you're trying to do.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    68
    Ya they are coming from the keyboard. I'll read the assignment istructions word for word so maybe you can see why I'm hoping to keep it short. here it is :

    "Write a program that sums a sequence of integers. Assume that the first integer read with scanf specifie the number of values remaining to be entered. You program should read only one value each time scanf is executed.

    You will use a for structure to read the numbers. Inside the structure you will add the number to a total and keep track of the smallest and largest numbers..

    Once the loop is completed insert a blank line and print a report that will show the total, the average, the smallest number and the largest number.

    You need a variable that stores the currently smallest and largest and we will need to compare the incoming digit to these values and replace them if necessary. Don't forget to initialize the smallest to highest value possible and largest to lowest value possible value.

    So, this problem will have a for structure and several ifs."

    I think I got the program figured out I'm just trying to avoid a lot of excess programming comparing all the numbers.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so each time through the loop, check to see if what you are entering is:
    a) Highest.
    b) Lowest.
    Update as needed. Then add this to the total you're making.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    68
    Thanks, sounds tricky. Any hints?
    regards,
    Extro

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    68
    Ok Im not following the logic. Where are the numbers coming from?
    It says a typical input sequence should look like this:

    5 100 200 300 400 500
    where 5 indicates that the five subsequent values are to be summed.

    How do I set this up?
    Last edited by Extropian; 08-05-2005 at 12:31 AM.

  9. #9
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Fairly simple. The first number is the size of the array you are to create and the rest is the data. Read the first value and create an array of that size and then read the rest into the array. Then do whatever you want to do with them.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    68
    So this IS an array I need?

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Quote Originally Posted by Extropian
    I'll read the assignment istructions word for word so maybe you can see why I'm hoping to keep it short.

    I was under the impression that you can't post homework or assignment on the message board.... !!!

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    68
    Quote Originally Posted by Machoscorpion
    I was under the impression that you can't post homework or assignment on the message board.... !!!
    Dont sweat it, I'm not asking anyone to do it for me, just asking for help with it, nobody will help me if they dont want to.

  13. #13
    ---
    Join Date
    May 2004
    Posts
    1,379
    You are allowed to ask for help. But you can't get other people to do the work for you.

  14. #14
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> can't post homework or assignment

    Whilst that is true...

    >>> You are allowed to ask for help.

    ... this is as well. Pushing people in the right direction so they can learn from the experience is very valid. Serving up the answer is, of course, not going to help anyone, least of all the questioner.

    To the OP, consider, when you get the first number, it is, by virtue of the fact that you only have a single number, both the highest and the lowest. So you set "High" and "Low" to the first value.

    Now, the next number, is it lower then "Low", if so, update "Low", is it higher then "High" if so update "High". Once you have read all 5 number and processed then in that way, you have the highest and lowest in "High" and "Low".

    No all you have to do is code it up.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 03:46 PM
  2. Problem in Converting Unsigned long to String
    By cprogrammer_18 in forum C Programming
    Replies: 8
    Last Post: 01-14-2006, 07:57 AM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  4. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM

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