-6

I'm trying to figure this one out on my own and I can't. The professor is asking for this. User will define the length of the sequence. User will then enter numbers from any range being both neg and pos. The program: Find the two largest numbers and display in descending order. Find the two smallest numbers and display in ascending order.

Can't use sort or arrays. I have an outline but, my head just wants to use arrays..... I don't see this simple without an array. Confused. I'm a beginner. I just want help. Advice? Tutor?

learning
  • 1
  • 1
  • Have one variable for each the 2 max and the 2 min elements. Then as you read in the data compare and set the values appropriately. No need for an array at all. – NathanOliver Feb 07 '17 at 19:31
  • 2
    Downvoted as we're not chegg to do your homework for you. Consider re-posting with your proposed outline you have and try asking for suggested modifications to achieve your goal. – sudo_coffee Feb 07 '17 at 19:31
  • Just define 4 variables (or an array of 4 `int`s :D) and check every time if the number is the bigger and so on... – Paolo M Feb 07 '17 at 19:31
  • 3
    Imagine how *you* would perform this task if you weren't able to memorize the entire list of numbers. – Drew Dormann Feb 07 '17 at 19:36

2 Answers2

2

Start with a solution to an easier problem, and grow it into a solution to the actual problem that you are solving:

  • Write a program that finds and prints the largest number entered by the user. This is easy to do with a single variable keeping track of "high watermark"
  • Modify your program to keep track of the smallest number as well. You can do it by adding another variable, and keeping track of the "low watermark".

The challenge in both tasks above is the initial value of the high/low watermark. This is a common source of errors; there are multiple Q&As on SO explaining the fix.

Now for the fun part:

  • Modify your program to keep track of the second-largest number by "demoting" the number that was previously considered largest to second-largest each time that you find a number that is larger than the largest one, and by replacing the second-largest when you find a value above it that does not exceed the largest value.

This would require you to write a couple of if statements.

  • Finally, modify your program to keep track of the second-smallest number by applying the "mirror image" of the algorithm above.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you. Breaking it down into smaller problems will help. I didn't think about making small simple programs to display just a part of the whole then progressing from there. Thanks for the idea! Too much stuff on my mind I couldn't think straight. Thanks again :) – learning Feb 07 '17 at 22:04
0

Algorithm ("Find the two largest numbers in a sequence without storing the sequence")

(I decided to undelete this answer. It might be helpful to OP to get away from the array frame of mind)

Input: A sequence of values S.
Output: Values Max and SecondMax.

The consumed, current value of S is E
1 : Set Max to E
2 : Set SecondMax to E (Second value in S)
3 : If Max < SecondMax -> Swap Max and SecondMax.
4 : while ( e:= E exists)
4.1 : If ( e > Max ) SecondMax := Max, Max := e
4.1.1 : Else If ( e > SecondMax ) SecondMax := e

This is trivially extended to the minimums.

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67