Click here to Skip to main content
15,071,430 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Prompt the user to enter three integers from the standard input, read the three integers using scanf(), and display on the standard output the largest number of the
three.
Note:
· User prompt should be “Please enter three integers: ”
· The output format is “The largest value of a, b and c is m” followed by a newline, where a, b and c are the three integers inputted by the user and m is the largest of
them.
CSS
A sample run of your program is:
Please enter three integers: 23 -9 182
The largest value of 23, -9 and 182 is 182
where "23 -9 182" are user input. Note that the second output line ends with a newline so that the
cursor is positioned to the beginning of the next line on the screen after the program finishes running.



My program:
It works with most cases but when i enter 0 -900 1
it gives me 0 as the largest value. why is that?


C#
#include <stdio.h>
int main() 
{
    int a, b, c, m;
 
    printf("Please enter three integers: ");
    scanf("%d %d %d", &a, &b, &c);
    m = (a>b) ? a : b && (a>c) ? a : c && (b>c) ? b : c;
    
    printf("The largest value of %d, %d and %d is %d\n", a, b, c, m);
    
    return 0;
}
Posted
Updated 24-Feb-10 5:47am
v2

May be you need something as follows:
m = (a > b) ? ((a > c) ? a : c ) : ((b > c) ? b : c);
   
Why not
if (a > b)
{
  if( a > c )
    m = a;
  else
    m = c;
}
else
{
  if (b > c)
    m = b;
  else
    m = c;
}


I mean, if the ternary operator isn't mandatory...
:rolleyes:
   
If you want to use ternary operator there is a trick,
#define Max(a,b) ((a>b)?a:b)

#include <stdio.h>
int main()
{
    int a, b, c, m;
    printf("Please enter three integers: ");
    scanf("%d %d %d", &a, &b, &c);
    m = Max(Max(a,b),c);
    printf("The largest value of %d, %d and %d is %d\n", a, b, c, m);
    return 0;
}


You can use this macro recursively.
   
v2
An "accumulating" method :) :
inline void CheckMax(int& iCurMax, const int& iCheck)
{
  if (iCurMax < iCheck) {
    iCurMax = iCheck;
  }
}

...and its usage:
{
...
  int m(MINSHORT);

  CheckMax(m, a);
  CheckMax(m, b);
  CheckMax(m, c);

  ASSERT(m >= a &&
         m >= b &&
         m >= c);
...
}
   
thanks but i have never seen this stuff you are talking about lool..thanks a lot for ur help though
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900