C++ User defined sequence integers without using arrays [closed]

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.

Leave a Comment