#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
elseif(b>a) //You had elsif, that doesn't mean anything
{
if(b>c)
{
return b; // return instead of cout
}
}
elseif(c>b)
{
if(c>a)
{
return 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?
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:
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..
#include <type_traits>
template< typename T, typename U > inlinetypename std::common_type<T,U>::type maximum( T a, U b )
{ return a>b ? a : b ; }
template< typename FIRST, typename... REST > inlinetypename 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" ;
constchar* 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 ; }
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?
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(constchar * _Format, ...);
EDIT: It's an ellipsis! It specifies when a function should accept an unknown number of arguments and with unknown types.
haha...is that in int printf(constchar * _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...