Finding the 2 Largest Values from 3 Inputs

  • Thread starter taxi
  • Start date
In summary, the conversation discussed creating a program to find the largest values from three inputs. The speaker gave a hint on how to write a function to find the largest value from three inputs and mentioned using classes and overloaded relational operators for a more general approach. They also provided a link to a page about Little Man Computer.
  • #1
taxi
2
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
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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
 

FAQ: Finding the 2 Largest Values from 3 Inputs

What is the purpose of finding the 2 largest values from 3 inputs?

The purpose of finding the 2 largest values from 3 inputs is to identify the two largest numbers out of the three given numbers. This can be useful in a variety of scenarios, such as finding the top two scores in a set of data or determining the highest and second-highest bids in an auction.

How do you find the 2 largest values from 3 inputs?

To find the 2 largest values from 3 inputs, you can use a simple algorithm that compares each input to the other two and keeps track of the two largest numbers. Alternatively, you can sort the inputs in descending order and select the first two numbers from the sorted list.

What if there are multiple occurrences of the largest values?

If there are multiple occurrences of the largest values, both of them will be included in the final two largest values. For example, if the inputs are 5, 5, and 3, the two largest values will be 5 and 5.

Can the inputs be negative or decimal numbers?

Yes, the inputs can be negative or decimal numbers. The algorithm for finding the 2 largest values from 3 inputs will work with any type of numerical input.

Is there a faster way to find the 2 largest values from 3 inputs?

Yes, there are faster ways to find the 2 largest values from 3 inputs. One way is to use a data structure such as a max heap, which allows for efficient retrieval of the largest values. Another way is to use a divide and conquer approach, where the inputs are divided into smaller groups and the largest values are found within each group before selecting the final two largest values.

Similar threads

Replies
29
Views
2K
Replies
5
Views
4K
Replies
5
Views
1K
Replies
4
Views
2K
Replies
10
Views
1K
Back
Top