Maximum Number from 3 numbers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <conio>

int maximum (int,int,int);

main()
{
	int a,b,c;

   cout<<"Enter Three Integers: ";
   cin>>a>>b>>c;
   cout<<"Maximum is: "<<maximum(a,b,c)<<endl;
   getch();
   return 0;
}

maximum (int a,int b,int c)
{
	if(a>b)
   {
		if(a>c)
      {	cout<<a;
      }
   elsif(b>a)
   {
   	if(b>c)
      {
      	cout<<b;
      }
   }
   elsif(c>b)
   {
   	if(c>a)
      {
      	cout<<c;
      }
   }
}


Hi, I'm trying to write the code to read maximum number from 3 input of numbers...is that the correct way to write for the function part? TQ...
close.

Your function "maximum" needs to return an integer, not cout something.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <conio>

int maximum (int,int,int);

main()
{
	int a,b,c;

   cout<<"Enter Three Integers: ";
   cin>>a>>b>>c;
   cout<<"Maximum is: "<<maximum(a,b,c)<<endl;
   getch();
   return 0;
}

int maximum (int a,int b,int c) //maximum should have type int.
{
   if(a>b)
   {
      if(a>c)
      {
         return a;
      }
   }// You were missing a } here
   else if(b>a) //You had elsif, that doesn't mean anything
   {
      if(b>c)
      {
         return b; // return instead of cout
      }
   }
   else if(c>b)
   {
   	if(c>a)
      {
      	return c;
      }
   }
}
You can write your function simpler by using conditional operator

1
2
3
4
5
int maximum( int a, int b, int c )
{
   int max = ( a < b ) ? b : a;
   return ( ( max < c ) ? c : max );
}


By the way the previous code written by Stewbond is incorrect!:) It has undefined behavior.
Last edited on
> Hi, I'm trying to write the code to read maximum number from 3 input of numbers...
> is that the correct way to write for the function part?

I would suggest something like this:

1
2
3
4
5
int maximum( int a, int b ) { return a>b ? a : b ; }

int maximum( int a, int b, int c ) { return maximum( maximun(a,b), c ) ; }

int maximum( int a, int b, int c, int d ) { return maximum( maximun(a,b), maximum(c,d) ) ; }


Tip: strive to write code that is as simple as possible:
JLBorges,
your code is not consistent with standard algorithm std::max. So it is better instead of

int maximum( int a, int b ) { return a>b ? a : b ; }

to write
int maximum( int a, int b ) { return a < b ? b : a ; }


Last edited on
Well as long as we're optomising:
1
2
3
4
5
6
7
8
template<typename T>
inline T maximum( T a, T b ) { return a>b ? a : b ; }

template<typename T>
inline T maximum( T a, T b, T c ) { return maximum( maximum(a,b), c ) ; }

template<typename T>
inline T maximum( T a, T b, T c, T d ) { return maximum( maximum(a,b), maximum(c,d) ) ; }


@Stewbond, thanks...but when I use that code to run and insert number like 2,3,4...the ouput always shows the center number which is 3, may I know which part wrong? TQ...

@vlad from moscow, Thanks...I'm still a beginner, can I know what does that means?
1
2
int max = ( a < b ) ? b : a;
return ( ( max < c ) ? c : max );


same to @JLBorges
Not really understand how the code works...can help explain...thanks..
Well as long as we're optimising:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <type_traits>

template< typename T, typename U > inline
typename std::common_type<T,U>::type maximum( T a, U b )
{ return a>b ? a : b ; }

template< typename FIRST, typename... REST > inline
typename std::common_type<FIRST,REST...>::type maximum( FIRST a, REST... b )
{ return maximum( a, maximum(b...) ) ; }

#include <iostream>
#include <string>

int main()
{
    double d = 8.6 ;
    int i = 24 ;
    short s = 19 ;
    std::cout << maximum( d, i, s, 'a' ) << '\n' ;

    std::string str = "abcd" ;
    const char* cstr = "efgh" ;
    std::cout << maximum( str, cstr ) << '\n' ;
}



> Not really understand how the code works...can help explain

int maximum( int a, int b, int c ) { return maximum( maximun(a,b), c ) ; }

To find the maximum of three numbers a, b and c
a. find the maximum of a and b - say: int temp = maximum(a,b) ;
b. find the maximum of c and temp - maximum( temp, c ) ;


> JLBorges, your code is not consistent with standard algorithm std::max. So it is better
> instead of int maximum( int a, int b ) { return a>b ? a : b ; }
> to write int maximum( int a, int b ) { return a < b ? b : a ; }

Yes. Thanks.
Last edited on
Haha, I like that.
I was actually just looking up the ... operator to see if this was possible.

Edit: I tried the code above but got a few errors. So... I simplified it to
1
2
3
4
5
6
inline int maximum( int a, int b ) { return a>b ? a : b ; }

inline int maximum( int a, int b, ...)
{ 
	return maximum( a, maximum(b, ...) ) ; 
}

But I still get: 1>Snippets.cpp(8): error C2059: syntax error : '...'

What's the correct syntax for doing this with .... Also, what is the name given to ... so I can google it? Googling "..." doesn't really work.
Last edited on
Google for 'variadic templates C++'
That would give results like this: http://www.devx.com/cplus/Article/41533/1954

Note: The compiler with VS 2010 does not support it.
@JLBorges, THanks

int maximum( int a, int b, int c, int d ) { return maximum( maximun(a,b), maximum(c,d) ) ; }
Why do we need use up to 4 variable where actually I only need to compare between 3 numbers?

int maximum( int a, int b ) { return a < b ? b : a ; }
"?" is a command?

int maximum( int a, int b, int c ) { return maximum( maximun(a,b), c ) ; }
From this, I guess at 1st it will compare between a and b right? Let say, a=6, b=5, so we take "a" and compare with "c" after that right?
So, how the compiler knows that I want to compare between a and b? Is that "maximum" is a command too? Or it's just a variable?

Thanks...
> "?" is a command?

?: is the arithmetic if operator (aka conditional operator, ternary operator)
See: http://en.wikipedia.org/wiki/%3F:#C.2B.2B


> Is that "maximum" is a command too? Or it's just a variable?

No, it is a function (one of the overloads)
1
2
3
int maximum( int a, int b ) { return a>b ? a : b ; }

int maximum( int a, int b, int c ) { return maximum( maximun(a,b), c ) ; }

See: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr312.htm


> Why do we need use up to 4 variable where actually I only need to compare between 3 numbers?

We didn't really need a 4 variable version; it was just added for illustrative purposes.
@Stewbond

What's that
...
? Is that a question mark to C++? haha...
Sorry I'm getting off topic. I didn't realize it was a C++0x thing.

Still there must be some support in prior standards for this. I'm thinking specifically of the printf() function which is from C. The header looks like:
int printf(const char * _Format, ...);

EDIT: It's an ellipsis! It specifies when a function should accept an unknown number of arguments and with unknown types.
Last edited on
haha...is that in int printf(const char * _Format, ...); VSC++? Coz I saw a lot variable use "_" inside VSC++...don't know why? I currently using Borland C++ but using VSC++ to do GUI...
> I'm thinking specifically of the printf() function which is from C. The header looks like:

va_list, va_start, va_arg and va_end in <cstdarg>
http://en.wikipedia.org/wiki/Stdarg.h
Topic archived. No new replies allowed.