Thread: Largest and smallest of 3 inputs

1. Largest and smallest of 3 inputs

Hello,

I am having a problem with this particular program. The user is prompted to insert 3 numbers (integers). The program should check which of the 3 is the largest and which is the smallest.

At the moment, the program prints one line endlessly:

Code:
`    printf("Among the numbers you entered");`
Code:
```#include <stdio.h>

int largest(int large);
int smallest(int small);

int main()
{
int  a, /*first input*/
b, /*second input*/
c, /*third input*/
large, /*largest of a, b and c*/
small; /*smallest of a, b and c*/

printf("Enter 3 integers with a space in between : ");
scanf("%d %d %d", &a, &b, &c);

/*if a is the largest*/
if((a > b) && (a > c))
{
a = large;
largest(large);

/*if a is the largest and b the smallest*/
if(b < c)
{
b = small;
smallest(small);
}

/*if a is the largest and c the smallest*/
else if(c < b)
{
c = small;
smallest(small);
}
}

/*if b is the largest*/
else if((b > a) && (b > c))
{
b = large;
largest(large);

/*if b is the largest and a the smallest*/
if(a < c)
{
a = small;
smallest(small);
}

/*if b is the largest and c the smallest*/
else if(c < a)
{
c = small;
smallest(small);
}
}

/*if c is the largest*/
else if((c > a) && (c > b))
{
c = large;
largest(large);

/*if c is the largest and a the smallest*/
if(a < b)
{
a = small;
smallest(small);
}

/*if c is the largest and b the smallest*/
else if(b < a)
{
b = small;
smallest(small);
}
}
return 0;
}
int largest(int large)
{
printf("Among the numbers you entered");
printf("\nthe largest was %d and the smallest was ", largest(large));
}
int smallest(int small)
{
printf("%d.", smallest(small));
}```

2. Your function doesn't do anything, except call itself. I would expect your largest function to take three inputs, and provide as output the largest of them. (It certainly shouldn't print anything, nor call itself.) (Ditto smallest, with the obvious change.)

3. the functions you declared are supposed to return an integer while they are actually returning nothing, it's a really simple program why are you using all those if..else statements? you could do it by running a simple loop

4. I have to use 2 functions largest() and smallest()....

5. Why are you re-calling the same function when printing the value?? Just print large or small, the argument you are passing to the function.

6. OK, two functions, largest and smallest. Just idea code, not run at all.

Code:
```
int biggest;

biggest = largest(arrayName); //or three int variable names

int largest(int arrayName[NumOfElements] or three int variables: num1, num2, num3) {

int bigNumber = num[0]   or num0;

if(num[1] or num1 > bigNumber)
bigNumber = num[1] or num1;
else if(num[2] or num2 > bigNumber)
bigNumber = num[2] or num2;
else if(num[3] or num3 > bigNumber)
bigNumber = num[3] (or num3);

return bigNumber;
}```
Smallest() would be an almost exact copy of the above.

Why wouldn't something simple like this, do the job?

7. Haven't we just been over this?
You have 12 comparisons, it can be done with three. The array can be sorted using just three comparisons, and then you have the smallest and largest at either end of the array, or in your case, in a and c.