heron heron - 3 months ago 18
C++ Question

Ways to find maximum digit

Currently my mini algorithm looks like this.

int a,b,c,max;
cout <<"Enter 3 digits: \t";
cin>>a>>b>>c;
if(a>b && a>c)
max=a;
else if(b>c && b>a)
max=b;
else
max=c;
cout <<"max: "<<max<<endl;


It works but Is there any other way to find maximum of 3 digits?

Answer

In C++11, you could do this:

int max_value = std::max({a, b, c});

It makes use of an overload of std::max which takes std::initializer_list<T> as argument. It implies you could pass more than 3 arguments!

int max_value = std::max({1,2,3,4,5,6,98,10});

Demo : http://ideone.com/FLifw