# Thread: Determine the largest and smallest of three integers

1. ## Determine the largest and smallest of three integers

I have just been introduced to a new chapter in C and I'm a bit confused/lost.

I'm constructing a program that will prompt the user for three integers and then determine the largest and the smallest.

Code:
```#include <stdio.h>

void largest();//determines the largest integer entered of 3 inputs
void smallest();//determines the smallest integer of 3 inputs

int main(void)
{
int input1, input2, input3;

printf("Enter 3 integers with a space in between : ");
scanf("%d %d %d", &input1, &input2, &input3);

if((input1 >= input2) && (input1 >= input3))//if the first input is the largest
{
largest();

if(input2 <= input3)//if the first input is the largest and the second input the smallest
smallest();

else if(input3 <= input2)//if the first input is the largest and the third input the smallest
smallest();
}
else if((input2 >= input1) && (input2 >= input3))//if the second input is the largest
{
largest();

if(input1 <= input3)//if the second input is the largest and the first input the smallest
smallest();

else if(input3 <= input1)//if the second input is the largest and the third input the smallest
smallest();
}
else if((input3 >= input1) && (input3 >= input2))//if the third input is the largest
{
largest();

if(input1 <= input2)//if the third input is the largest and the first input the smallest
smallest();

else if(input2 <= input1)//if the third input is the largest and the second input the smallest
smallest();
}
return 0;
}
void largest()
{
int large;

printf("Among the numbers you entered\n");
printf("the largest was %d and the smallest was ", large);//should be printed when the largest intger is determined
}
void smallest()
{
int small;

printf("%d.", small);//should be printed when the smallest integer is printed
}```

2. Forgot to mention, I suspect the problem area to be:

Code:
```}
void largest()
{
int large;

printf("Among the numbers you entered\n");
printf("the largest was %d and the smallest was ", large);//should be printed when the largest intger is determined
}
void smallest()
{
int small;

printf("%d.", small);//should be printed when the smallest integer is printed
}```

3. Maybe you should pass some parameters to those functions.

4. I'm sure that you realise your functions largest() and smallest() won't actually print any (valid) values as your main function stands? If you want them to, try passing the value to be printed into them as a parameter.

BTW, your main logic is horribly convoluted. Have a Google at how ternary operators work in C.

5. i will give you some steps:

1) declare your variables outside any function (after the #include part)
2) put the following algorithm (translate it to C) in the largest function:
int biggest
if input1 is bigger than input2 then
biggest is equal to input1
else
biggest is equal to input2

if biggest is lesser than input3 then
biggest = input3

display biggest
3) put the following algorithm (translate it to C) in the smallest function:
int smallest
if input1 is bigger than input2 then
smallest is equal to input1
else
smallest is equal to input2

if smallest is lesser than input3 then
smallest = input3

display smallest
4) end of all the steps

i hope the above steps help.

6. I assume this is for education? If so, don't continue with this code, it'd probably get a horrible grade. Start over... Use UltraKing's advice.

7. yes it is for school, I have to use functions..(I could have used an easier method, but the focus is design)

I will only be graded on whether it works or not (not what it looks like)...

The problem is I'm not completely sure how it works...hence the reason that I haven't passed the values to it's corresponding function

8. you may get a good grade if it only works. but, there is one thing which
must not be skipped. every c programmer, should have the following basis
(at a minimum):

s\he must write clean code
s\he must not use magic numbers (numbers used without reference)

and the list goes on. if you ask me, it never hurts to have a clean code
than a spagheti-code (spagheti-code means a code which cant be
understanded by others). its better safe than sorry.

9. You can sort three items with just three "compare and swap" operations. Then the first and last item will be your smallest and largest.
Do you think that would have been easier?

10. THIS WILL WORK
#include<stdio.h>
#include <conio.h>
void largest(int);//determines the largest integer entered of 3 inputs
void smallest(int);//determines the smallest integer of 3 inputs

int main(void)
{
int input1, input2, input3;

clrscr();
printf("Enter 3 integers with a space in between : ");
scanf("%d %d %d", &input1, &input2, &input3);

if((input1 >= input2) && (input1 >= input3))//if the first input is the largest
{
largest( input1);

if(input2 <= input3)//if the first input is the largest and the second input the smallest
smallest( input2);

else if(input3 <= input2)//if the first input is the largest and the third input the smallest
smallest( input3);
}
else if((input2 >= input1) && (input2 >= input3))//if the second input is the largest
{
largest(input2);

if(input1 <= input3)//if the second input is the largest and the first input the smallest
smallest(input1);

else if(input3 <= input1)//if the second input is the largest and the third input the smallest
smallest(input3);
}
else if((input3 >= input1) && (input3 >= input2))//if the third input is the largest
{
largest(input3);

if(input1 <= input2)//if the third input is the largest and the first input the smallest
smallest(input1);

else if(input2 <= input1)//if the third input is the largest and the second input the smallest
smallest(input2);
}
return 0;
}
void largest(int a)
{

printf("Among the numbers you entered\n");
printf("the largest was %d and the smallest was ", a);//should be printed when the largest intger is determined
}
void smallest( int small)
{

printf("%d.", small);//should be printed when the smallest integer is printed
}

11. > THIS WILL WORK
This hasn't bothered to read the forum rules on how to post code with code tags

12. Originally Posted by Salem
> THIS WILL WORK
This hasn't bothered to read the forum rules on how to post code with code tags
Salem:

Don't be unfair, now! "This" said he WILL work, but never said he was working at that moment!

SilentPirate:

it's common in sorting / ordering programs to have a function JUST for swapping two elements or variables.

Why not use UltraKing's good advice, but you can call your swap() function with just the addresses of the two variables to be swapped:

Code:
```swap(&num1, &num2);

void swap(int * num1, int *num2) {
int *temp;
//you know the drill probably: ask if you need a hint

}```
That is a good use for a simple function, and quite standard, and elegant.

13. I didn't even try to read the unreadable to work out whether it might work or not.

14. Originally Posted by magestrium
THIS WILL WORK
1. Don't do other's homework for them; read the forum rules
2. That's a horrible solution
3. Use code tags when posting code, or don't post at all

15. Originally Posted by iMalc
1. Don't do other's homework for them; read the forum rules
2. That's a horrible solution
3. Use code tags when posting code, or don't post at all
Wow, thank you for setting this staight iMalc. Wanted to say something about NOT HELPING others by feeding them the solution myself.