Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Determine the largest and the smallest number in a list and print the difference between them.

Example: with a list of [13,2,9,19,54,4,7,12] (use a list of these numbers for testing) the minimum is 2, and the maximum is 54 so it should print 52.

What I have tried:

I'm just so confused about this. I'm not sure how to start or how to code it really.
Posted
Updated 25-May-21 6:43am

Work it out on paper, step by step.

What are you going to have to track? Well, that's easy. you're going to have to look at each number in the list, one at a time. Now, is it higher than the highest number? What do you do if it is? Is it lower than the lowest number you've seen? What do you do if it is? Keep going through the list. Now that you have the highest and lowest number, how do you determine the difference between them?

This is an easy assignment if you put some thought into it.
 
Share this answer
 
Comments
j a c o b t h e J O K E R 25-May-21 12:12pm    
I know it's easy. That why I'm mad right now cause I cant figure it out lol. But, all I'm trying to do is print the smallest and highest number (2, 54). And then print the difference between them (52).
Dave Kreskowiak 25-May-21 12:19pm    
I already told you how to do it. My description maps to 1 to 3 instructions and even implies you have to keep track of certain values, all two of them.

Now, what would you have me say, without writing your code for you?
Get a big piece of paper, and ten small ones. Draw two boxes on the big piece, one labelled "minimum", and one labelled "maximum". Now write all the numbers 13,2,9,19,54,4,7,12 on one each of the small pieces. You should have two left, write -infinity on one and put it in the maximum box. Write +infinity on the other and put it in the minimum box. Now pick each of the other numbered pieces one at a time. Compare the number on it with the minimum box. If it is smaller then replace the piece in the minimum box with the numbered piece. Do the same with the maximum box, but check if it is larger. Repeat with all the numbered pieces. once you have used up all the pieces see what you have in the two boxes.

And that is all there is to it, so now you can write those steps in code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900