I have been assigned a homework assignment to prompt the user for 3 positive integers then compare and print them in order of largest, median and smallest.
Prompting and writing a while loop to check if the numbers are positive is fine. I can also figure out how to print the largest and smallest integer.
(Something like this?)
if (a >= b)
if (a >= c) { max= a; if (b >= c) min= c; else min= b; }
else { max= c; min= b; }
else if (b >= c)
{ max= b; if (a >= c) min= c; else min= a; }
else { max= c; if (a >= b) min= b; else min= a; }
How would I calculate the middle integer using this same schema? I would preferably not use an array yet as the professor has not yet explained them.
Any help is appreciated.
Thank you!
Store the three numbers in three variables a b c
, then use your branching logic to determine the order. You have everything you need to solve this problem here.
For example
if (a > b && a > c) {
//Here you determine second biggest, but you know that a is largest
}
if (b > a && b > c) {
//Here you determine second biggest, but you know that b is largest
}
if (c > b && c > a) {
//Here you determine second biggest, but you know that c is largest
}
Inside the comments above is where you would determine the median
and the smallest
number. The code is wordy, but since you said not to use an array, it's the most straightforward way to understand the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With