# Thread: Shortening long if statements

1. ## 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. Use an array. Walk through the array picking out the biggest and smallest.

Quzah.

3. 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. 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.

5. 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. 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.

7. Thanks, sounds tricky. Any hints?
regards,
Extro

8. 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?

9. 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. So this IS an array I need?

11. 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. 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. You are allowed to ask for help. But you can't get other people to do the work for you.

14. >>> 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.

Popular pages Recent additions