Asked by:
Displaying the Largest and Smallest Integers
Question

Here's the problem...
"Write an application that inputs three integers from the user and displays the sum, average, product, and smallest and largest of the numbers. [ Note: The calculation of the average in this exercise should result in an integer representation of the average. So if the sum of the values is 7, the average should be 2, not 2.3333....]"
OK... I have everything working fine except displaying the largest and smallest integers.
My Professor said not to use the "&" symbol, arrays, or anything fancy. Just relational operators and basic if statements.
Here is a hint my Prof. gave us...
"OK. How would you solve the problem by hand? Let's say that I'd given you a long list of numbers on a piece of paper. Say 100 or more numbers. How would you find the smallest one yourself? Answering this may give you a big clue."
Any ideas? Edited by mballen07 Tuesday, September 23, 2008 4:08 AM
Tuesday, September 23, 2008 4:04 AM
All replies

So... how would you solve it by hand? :) One way is to sort all the numbers, but since you're not allowed to use an array then that option has been ruled out. Also I wouldn't want to waste time sorting 100 numbers by hand to find the min/max. Instead I would write "min" and "max" somewhere on my piece of paper and start comparing all the numbers.
Here are the steps: Is number1 greater than number2? Yes? Then I'll write down number1 under "max" and number2 under "min." No? Then do the opposite. Next I'll move on to number3  is it larger than my current max? Yes? Then replace my max with number3. No? Ok is it smaller than my current min? Yes? Then replace my min with number3. No? Then keep my current min/max, number3 is possibly equal to my current min or max values so nothing needs to get replaced.
The Yes/No considerations above can be translated to if/then statements. You only have 3 inputs so it won't be too hard.
So declare an int minNumber, maxNumber;
Then start your if/else logic... this should get your started:
if (input1 > input2)
{
maxNumber = input1;
minNumber = input2;
}
else
// switch the max/min with input2/input1
At this point you have the first round of min/max. Now move on to input3 following the logic I gave earlier.
Since this is homework I'll let you give it a try. If you get stuck come back and show us what you have so far.
Document my code? Why do you think it's called "code"? Proposed as answer by Jan Van der Haegen Tuesday, September 23, 2008 2:50 PM
 Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, September 25, 2008 7:28 AM
 Unmarked as answer by mballen07 Friday, September 26, 2008 4:32 AM
Tuesday, September 23, 2008 6:05 AM 
Actually, I find the professors "hint" quite misleading: if I was doing this on paper, I would glance for the narrowest and widest width, and only look at lines with roughly those widths. But with a computer, it is simpler and more efficient to just compare each value in turn (assuming you don't know them to be presorted). Ahmed already covered the "how" of this...
MarcTuesday, September 23, 2008 9:07 AM 
I've already considered ifelse statements and whatnot, but I don't think we can use them, as we haven't covered them in class.
Originally I had:
if ( number1 > number2 & number1 > number3 )
Console.WriteLine( "{0} is the largest number", number1 );
And that worked just fine.... only we aren't supposed to use '&' because we haven't covered them. So far in class we've only done BASIC if statements (not ifthen, ifelse, etc) and simple arithmetic operations and relational operators.
Tuesday, September 23, 2008 1:50 PM 
You haven't covered if/else? Just if? I would ask your professor if they really want you to just use if statements and not else... seems rather basic not to do so.
Anyway, you could pull it off with just if statements, although that would be a poor choice. Using your code as an example, you can break all && into a separate if statement:
if ( number1 > number2 )
{
if ( number1 > number3 )
Console.WriteLine( "{0} is the largest number", number1 );
}
That is essentially equivalent to what you had with the &&.
Document my code? Why do you think it's called "code"? Proposed as answer by Jan Van der Haegen Tuesday, September 23, 2008 2:50 PM
 Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, September 25, 2008 7:28 AM
 Unmarked as answer by mballen07 Friday, September 26, 2008 4:32 AM
Tuesday, September 23, 2008 2:10 PM 
Marc Gravell said:
Actually, I find the professors "hint" quite misleading: if I was doing this on paper, I would glance for the narrowest and widest width, and only look at lines with roughly those widths. But with a computer, it is simpler and more efficient to just compare each value in turn (assuming you don't know them to be presorted). Ahmed already covered the "how" of this...
Marc
So true... Wich of the following is the smallest number?
354354354354
35435434354354354
545247876464
5446357437357
4
544354354354
Lol!
Anyways, if you can only use if statement, check out ahmeds latest addition to your thread. For max this would lead to this scheme...
if(1>2)
{
if(1>3)
{1}
if(1<3)
{3}
}
if(1<2)
{
if(2<3)
{3}
if(2>3)
{2}
}
"The improbable we do, the impossible just takes a little longer." (Steven Parker)Tuesday, September 23, 2008 2:23 PM 
I agree that it's a poor choice, but the problem wants us to only use techniques found in the chapter we just covered.Tuesday, September 23, 2008 2:23 PM

Thanks for the help guys. I think some of this will be on the right track. I'm going to email the prof about this.Tuesday, September 23, 2008 2:27 PM

if you want to be a smart ____ :
static int GetMin (int one, int two, int three) { return Math.Min( one, Math.Min( two, three) ); } static int GetMax (int one, int two, int three) { return Math.Max( one, Math.Max( two, three) ); }
But I think your teacher wants something like this :static int GetMin (int one, int two, int three) { int min = one; if (two < min) { min = two; } if (three < min) { min = three; } return min; } static int GetMax (int one, int two, int three) { int max = one; if (two > max) { max = two; } if (three > max) { max = three; } return max; }
if a problem looks too big, break it into smaller objects Edited by M0nkeyMaster Tuesday, September 23, 2008 3:20 PM smart ____ is censored ?
 Proposed as answer by M0nkeyMaster Tuesday, September 23, 2008 3:20 PM
 Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, September 25, 2008 7:28 AM
 Unmarked as answer by mballen07 Friday, September 26, 2008 4:32 AM
Tuesday, September 23, 2008 3:19 PM 
i took a course that used the same book this example is from. heres how i solved it:
int largest = 0;
int smallest = 0;
Console.Write("Enter first number yo: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number yo: ");
int num2 = Convert.ToInt32(Console.ReadLine());
if (num1 > num2)
{
largest = num1;
smallest = num2;
}
if (num1 < num2)
{
largest = num2;
smallest = num1;
}
Console.Write("Enter third number yo: ");
int num3 = Convert.ToInt32(Console.ReadLine());
if (num3 > largest)
{
largest = num3;
}
if (num3 < num2)
{
smallest = num3;
}
Console.WriteLine("the sum of the numbers is: " + (num1 + num2 + num3));
Console.WriteLine("the product of the numbers is: " + (num1 * num2 * num3));
Console.WriteLine("the average of the numbers is: " + ((num1 + num2 + num3) / 3));
Console.WriteLine("the smallest of the numbers is: " + smallest);
Console.WriteLine("the largest of the numbers is: " + largest);Wednesday, August 3, 2011 3:30 PM 
i took a course that used the same book this example is from. heres how i solved it:
<snip>
Unfortunately, that doesn't seem to work for the input: 1, 3, 2:
Enter first number yo: 1
Enter second number yo: 3
Enter third number yo: 2
the sum of the numbers is: 6
the product of the numbers is: 6
the average of the numbers is: 2
the smallest of the numbers is: 2
the largest of the numbers is: 3Thursday, August 4, 2011 12:07 AM 
We can solve this in a maximum of 3 runtime comparisons, because there are only 6 different combinations of the input data.
Let the 6 different possible combinations be "abc acb bac bca cab cba".
With one comparison, a < b, we can partition the sorted output into two possible sets: "abc acb cab" or "bac bca cba".
Then for the first set "abc acb cab" a further comparison, c < a, partitions it into "abc" and "acb cab".
One last comparison for the "acb cab" case, c < a, will determine which of those possibilities is true.
We can apply similar logic to the second set. Putting it all together in a modified version of catchynick's sample code, we get the following:
using System; namespace Demo { class Program { static void Main(string[] args) { Console.Write("Enter first number yo: "); int a = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter second number yo: "); int b = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter third number yo: "); int c = Convert.ToInt32(Console.ReadLine()); int x, y, z; // Possible orders: abc acb bac bca cab cba if (a < b) // > abc acb cab if (b < c) // > abc { x = a; y = b; z = c; } else if (c < a) // acb cab > cab { x = c; y = a; z = b; } else // acb cab > acb { x = a; y = c; z = b; } else // > bac bca cba if (a < c) // > bac { x = b; y = a; z = c; } else if (b < c) // bca cba > bca { x = b; y = c; z = a; } else // bca cba > cba { x = c; y = b; z = a; } // x y z are now in sorted order. Console.WriteLine("the sum of the numbers is: " + (x + y + z)); Console.WriteLine("the product of the numbers is: " + (x * y * z)); Console.WriteLine("the average of the numbers is: " + ((x + y + z) / 3)); Console.WriteLine("the smallest of the numbers is: " + x); Console.WriteLine("the largest of the numbers is: " + z); } } }
However, this uses "else" statements, which might not be allowed according to the OP. Proposed as answer by Sankarsan Parida Thursday, August 4, 2011 6:01 AM
Thursday, August 4, 2011 12:32 AM 
If we want to avoid using the "else" statement at all, you can do it like this (but this is always making at least 5 comparisons, and usually more):
using System; namespace Demo { class Program { static void Main(string[] args) { Console.Write("Enter first number yo: "); int a = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter second number yo: "); int b = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter third number yo: "); int c = Convert.ToInt32(Console.ReadLine()); int x = c, y = b, z = a; // Possible orders: abc acb bac bca cab cba if (a < b && b < c) { x = a; y = b; z = c; } if (a < c && c < b) { x = a; y = c; z = b; } if (b < a && a < c) { x = b; y = a; z = c; } if (b < c && c < a) { x = b; y = c; z = a; } if (c < a && a < b) { x = c; y = a; z = b; } // Otherwise must be cba, with which we intialized xyz. // x y z are now in sorted order. Console.WriteLine("the sum of the numbers is: " + (x + y + z)); Console.WriteLine("the product of the numbers is: " + (x * y * z)); Console.WriteLine("the average of the numbers is: " + ((x + y + z) / 3)); Console.WriteLine("the smallest of the numbers is: " + x); Console.WriteLine("the largest of the numbers is: " + z); Console.WriteLine(); } } }
If you also want to avoid using &&, you'd have to rewrite the ifs like this:
if (a < b) if (b < c) { x = a; y = b; z = c; }
if (a < c) if (c < b) { x = a; y = c; z = b; }
if (b < a) if (a < c) { x = b; y = a; z = c; }
if (b < c) if (c < a) { x = b; y = c; z = a; }
if (c < a) if (a < b) { x = c; y = a; z = b; }
Thursday, August 4, 2011 12:56 PM 
//college instructions are often misleading, when I was in programming classes in college I often utilized features of a language not covered in the initial problm (or hint). //doing this without a loop isn't tecnically impossible, but it sures makes things prettier, and you can still get all of your information with just an If statement, and no && operators. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace numbercsharptest { class Program { static void Main(string[] args) { string[] strNumbers = new string[3]; ConsoleKeyInfo keyRepeat; int[] numbers = new int[3]; int sum, average, product, min, max = 0; bool repeat = true; do { Console.WriteLine("Input three integers..."); int index = 1; do { Console.Write("Enter number #" + index.ToString() + " >"); strNumbers[index  1] = Console.ReadLine(); if (!int.TryParse(strNumbers[index  1], out numbers[index  1])) { Console.WriteLine("Invlaid integer, try again."); continue; } ++index; } while (index <= 3); Console.WriteLine(); sum = numbers[0] + numbers[1] + numbers[2]; average = sum / 3; product = numbers[0] * numbers[1] * numbers[2]; min = numbers[0]; max = numbers[0]; //this is how I would solve it on paper, I'd go through each number //and ask if it's smaller or larger than the one I checked before it. //so I made the first number the min, and the max, now I'll compare each number //to that, if it's smaller, that number becomes the min, and if it's larger //it becomes the max for (int i = 0; i < numbers.Length; ++i) { if (numbers[i] < min) min = numbers[i]; if (numbers[i] > max) max = numbers[i]; } Console.WriteLine("Sum: " + sum.ToString()); Console.WriteLine("Average: " + average.ToString()); Console.WriteLine("Product: " + product.ToString()); Console.WriteLine("Min: " + min.ToString()); Console.WriteLine("Max: " + max.ToString()); Console.WriteLine(""); Console.WriteLine("Would you like to repeat the program? (y/n)"); keyRepeat = Console.ReadKey(); if (keyRepeat.Key == ConsoleKey.N) repeat = false; } while (repeat); Console.WriteLine("Program execution complete..."); } } }
My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under ConstructionThursday, August 4, 2011 4:50 PM 
Also, just because you haven't covered something in class doesn't always mean you can't use it or you'll get a bad grade.
In my experience in college, as long as I use at a minimum, what was asked of me, I could add extra stuff like (using them in a for loop), or getting user input with a loop, and not get penalized. It also shows you have the drive to learn beyond what your class is teaching you, which is the whole point in learning in class in the first place.
My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under ConstructionThursday, August 4, 2011 4:52 PM