Finding the 2 Largest Values from 3 Inputs

  • Thread starter Thread starter taxi
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 5K views
taxi
Messages
2
Reaction score
0
hi.

i'm trying to make an lmc that gives oupts the 2 largest values from 3 inputs.

i know how to find out the largest from 2 values but can't think of how to get the largest from 3 inputs.

any help would be appreciated

thanks
 
Physics news on Phys.org
I have no idea what an "lmc" program is. Could you elaborate on that?

Regardless, returning the largest pair of three given values is trivial. I'll give you a hint how to write a function that will return the largest single value from three values; writing a function to return the largest two values should then be easy for you. I'll write it in C++ for the sake of argument.

Code:
double max(const double& a, const double& b, const double&c)
{
   double maxTempVariable = a;

   if (b > maxTempVariable)
      maxTempVariable = b;
   if (c > maxTempVariable)
      maxTempVariable = c;

   return maxTempVariable;
}

If you're interested in applying this to more general objects, classes together with appropriately overloaded relational operators is the ways to go.
 
shoehorn said:
I have no idea what an "lmc" program is. Could you elaborate on that?

Regardless, returning the largest pair of three given values is trivial. I'll give you a hint how to write a function that will return the largest single value from three values; writing a function to return the largest two values should then be easy for you. I'll write it in C++ for the sake of argument.

Code:
double max(const double& a, const double& b, const double&c)
{
   double maxTempVariable = a;

   if (b > maxTempVariable)
      maxTempVariable = b;
   if (c > maxTempVariable)
      maxTempVariable = c;

   return maxTempVariable;
}

If you're interested in applying this to more general objects, classes together with appropriately overloaded relational operators is the ways to go.

have a look at this.

http://en.wikipedia.org/wiki/Little_man_computer