Reputation: 43
the terminal is showing no output (blank)
// if the ages of 3 people are put telling which one is youngest//
#include<stdio.h>
int main()
{
int a1,a2,a3;
printf("enter the age of ram,shyam and ajay");
scanf("%d,%d,%d",&a1,&a2,&a3);
if(a1>a2>a3)
printf("%d,%d,%d",a1,a2,a3);
if(a3>a2>a1)
printf("%d,%d,%d",a3,a2,a1);
if(a2>a1>a3)
printf("%d,%d,%d",a2,a1,a3);
if(a2>a3>a1)
printf("%d,%d,%d",a2,a3,a1);
if(a1>a3>a2)
printf("%d,%d,%d",a1,a3,a2);
if(a3>a1>a2)
printf("%d,%d,%d",a3,a1,a2);
return 0;
}
i am not sure if you should write 3 expressions in an if statement like 1>2>3 please correct me if i am wrong
Upvotes: 1
Views: 110
Reputation: 25326
The line
if(a1>a2>a3)
will not do what you want. According to the rules of operator precedence and associativity, that expression is equivalent to the following:
if ( (a1>a2) > a3 )
The sub-expression (a1>a2)
will evaluate to either 0
(false) or 1
(true). Therefore, the entire expression is equivalent to either
if ( 0 > a3 )
or:
if ( 1 > a3 )
What you want is instead the following:
if ( ( a1 > a2 ) && ( a2 > a3 ) )
Because the &&
operator has lower precedence than the >
operator according to the rules mentioned above, you can write this without the parentheses:
if ( a1 > a2 && a2 > a3 )
Another problem in your solution is that if all 3 input values are identical, then none of the if
statements will be true, and your program will print nothing.
I suggest that you rewrite your logic like this:
#include <stdio.h>
int main( void )
{
int a1, a2, a3;
printf( "Enter the age of ram,shyam and ajay: " );
scanf( "%d,%d,%d", &a1, &a2, &a3 );
if ( a1 > a2 )
{
if ( a2 > a3 )
{
printf( "%d,%d,%d\n", a1, a2, a3 );
}
else
{
if ( a1 > a3 )
{
printf( "%d,%d,%d\n", a1, a3, a2 );
}
else
{
printf( "%d,%d,%d\n", a3, a1, a2 );
}
}
}
else
{
if ( a1 > a3 )
{
printf( "%d,%d,%d\n", a2, a1, a3 );
}
else
{
if ( a2 > a3 )
{
printf( "%d,%d,%d\n", a2, a3, a1 );
}
else
{
printf( "%d,%d,%d\n", a3, a2, a1 );
}
}
}
return 0;
}
Because every if
statement now has a corresponding else
statement, all cases are handled, including the case of all 3 numbers being identical.
This solution is also more efficient, because the maximum number of comparisons required is now only 3 instead of 12.
Alternatively, if you put all numbers into an array, you can sort the array and then print it. This will make things a lot easier when you have more than 3 numbers:
#include <stdio.h>
#include <stdlib.h>
int my_compare( const void *left, const void *right )
{
int a = *(int*)left;
int b = *(int*)right;
if ( a < b )
return 1;
if ( a > b )
return -1;
return 0;
}
int main( void )
{
//define array with many numbers
int arr[] = { 14, 17, 19, 18, 17, 12, 15, 19 };
const int arr_size = sizeof arr / sizeof *arr;
//sort the array
qsort( arr, arr_size, sizeof *arr, my_compare );
//print the content of the sorted array
for ( int i = 0; ; )
{
printf( "%d", arr[i] );
if ( ++i == arr_size )
break;
printf( ", " );
}
return 0;
}
This program has the following output:
19, 19, 18, 17, 17, 15, 14, 12
Upvotes: 1
Reputation: 31
Yes! You can't use the statement like this: if (a1>a2>a3)
, you need to write them like this:
if (a1>a2 && a2>a3)
And also one more thing you need to correct in this program that you need to check the conditions only once for one variable or also you can also take a look on the function by you can do it easily without writing that much much code within some lines you can solve it.
Upvotes: 2
Reputation: 311088
According to the C Standard (6.5.8 Relational operators)
6 Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.
And the operators are evaluated from left to right,
So if statements like that
if(a1>a2>a3)
are equivalent to
if( ( a1 > a2 ) > a3 )
And depending on whether a1
is greater than a2
or not you have either
if( ( 1 ) > a3 )
or
if( ( 0 ) > a3 )
Instead you need to include in the expression logical AND operator like
if( a1 > a2 && a1 > a3 )
But if you will do so the program can output nothing if at least two ages are equal each other.
Also in this call of scanf
scanf( "%d,%d,%d", &a1, &a2, &a3 );
it is better to remove commas between the conversion specifiers. Let the user will enter ages separated by spaces like for example
20 22 21
or
20
22
21
If you want to output the ages in the descending order you could write for example
#include <stdio.h>
int main( void )
{
int a1, a2, a3;
printf( "enter the age of ram,shyam and ajay: " );
scanf( "%d %d %d", &a1, &a2, &a3 );
if ( a1 < a2 )
{
int tmp = a1;
a1 = a2;
a2 = tmp;
}
if ( a2 < a3 )
{
int tmp = a2;
a2 = a3;
a3 = tmp;
}
if ( a1 < a2 )
{
int tmp = a1;
a1 = a2;
a2 = tmp;
}
printf( "%d, %d, %d\n", a1, a2, a3 );
}
Upvotes: 2