In this assignment, you shall create a complete C++ program that
prompts the user for three integer values, reads the values from
the keyboard, and then uses a series of decisions to find the
middle value (in ascending order) of the three integers. The three
integers can each be different amounts, or two or more of them can
be the same amount.
Below is an example execution of the program. Your program
should use the same wording for prompting the user and printing the
results.
Enter three integers separated by one or more spaces: 12 530
-8
The sorted values are -8 12 530
The middle value is 12
A First Approach to the Problem
You may think that this is a rather simple program to write
because given only two integer values, you can easily compare two
values and tell which one is greater or lesser than the other.
Therefore, finding the middle of three integer values is probably
just as simple, although it may take a few more lines of source
code.
So, you start the program by trying to write a separate if
statement to handle every possible combination of the three
integers. This can get rather involved very quickly because the
three integers do not all have to be different numbers. This
approach most likely will involve many if statements and else
statements, with if statements and else statements nested inside of
each of those statements. Then when you think you have finally
finished the program, you need to test each possible path through
your source code.
Take some scratch paper and try writing this "first approach"
algorithm in C++. Don't spend too much time attempting to complete
it, though, because there is a general, straightforward way to
solve the problem. This is described in the next section.
A Second Approach to the Problem
Instead of focusing on all the possible combinations of three
integers, we can first convert the data into a standard format that
is easier to work with no matter what values are entered. In other
words, we can first sort the three integers in ascending order.
Then the task of finding the middle value is rather simple. In
addition, we get the byproduct of having sorted the integers. That
is the approach we will take in this assignment. The sorting
algorithm that is used here is called a bubble sort.