I have to write a program that will take in five numbers then tell you which is smallest and which is biggest. I can only use if statements for comparison. I can only think of doing it in a way that would take fifty odd lines of code. There must be some far simpler algorithim to do this. Does anyone know how. This is not a homework question.
… you don’t need to sort all of them.
The simplest is to do it in two passes. Start with the biggest.
Assume that the biggest is the first number, and call it the current biggest (keep the current biggest value seen so far in a separate variable). Then, for each one of the other numbers, compare against the other numbers, and see if they are bigger than the current biggest. If they are, then they are the new current biggest.
Obviously, this works really well if you have the numbers in an array, and can use a for loop, because then it becomes about four lines of code, no matter how many numbers you have. Otherwise you have basically a comparison and possible reassignment of ‘current biggest’ for each number.
The same reasoning works for ‘smallest’.
Can you see what to do from here?
This is what I got. I just keep feeling that there must be an easier way to do it.
#include <stdio.h>
int main()
{
int num1, num2, num3, num4, num5, biggest, smallest;
printf("Please enter five integers:
");
scanf("%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5);
biggest = num1;
smallest = num1;
if (num2 > biggest)
biggest = num2;
if (num3 > biggest)
biggest = num3;
if (num4 > biggest)
biggest = num4;
if (num5 > biggest)
biggest = num5;
if (num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
if (num4 < smallest)
smallest = num4;
if (num5 < smallest)
smallest = num5;
printf("
The largest number is %d, the smallest is %d.
", biggest, smallest);
return 0;
}
void printBigSmall(int *numArray, int arraySize)
{
int smallest, biggest;
smallest = biggest = *numArray; //Set both values to first element in array
for (int i = 1; i < arraySize; i++, numArray++)
{
if (*numArray < smallest)
smallest = *numArray;
if (*numArray > biggest)
biggest = *numArray;
}
printf("The biggest number is %d. The smallest is %d", smallest, biggest);
}
Hope I’ve remembered my C syntax. Of course, you can make it a lot more terse than that, but it would be harder to read.
Not that you’re not ‘sorting’ the numbers at all. Just picking values. If you have access to a sort algorithm, you could do it like this:
void printBiggest(int *array, int arraySize)
{
sort(array, SMALL_TO_BIG);
int smallest = array[0];
int biggest = array[arraySize-1];
}
However, for something like this there’s no need to sort the entire array, and the first code I posted is faster because it can find the two values in one pass through the array, whereas the sort algorithm will need some greater amount of passes to sort the array (how many depends on the sorting algorithm in question).
Oh, and if you need to handle manual input of the numbers, just allocate an array first, like this:
int numArray[5];
printf("Please enter five integers:
");
scanf("%d%d%d%d%d", &numarray[0], &numArray[1],
&numArray[2], &numArray[3], &numArray[4]);
Then proceed as above.
I think I got it now, thanks.
A handy trick:
max(a, b) = (a + b + |a - b|)/2
min(a, b) = (a + b - |a - b|)/2
They may be handy, but aren’t very efficient compared to a simple comparison. I can’t think of a situation where these tricks would be useful.
On some machines, it’s more efficient to do a long list of arithemetic opcodes than a branch which would cause a pipeline bubble.
I’m not saying this would make a real difference in the actual perceived speed, but it’s one example where a mathematical expression would be more useful than a branchy algorithm.