max: compare two numbers withou using [if, switch, :?]


/ Published in: C
Save to your folder(s)

max: compare two numbers withou using [if, switch, :?]


Copy this code and paste it in your HTML
  1. // solution one
  2. //
  3. template <class T>
  4. T max(T a, T b){
  5. return (a + b + abs(a - b)) / 2;
  6. }
  7.  
  8.  
  9. // solution two
  10. //
  11. template <class T>
  12. T max(T a, T b){
  13. T num[2] = [a, b];
  14. return num[a < b];
  15. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.