answersLogoWhite

0

Here's a way to do it in pseudo-code:

  • create an array with two elements
  • if the first number in the provided list is greater than the second one
    • assign the first one to the first array element
    • assign the second one to the second array element
  • else
    • assign the first one to the second array element
    • assign the second one to the first array element
  • for every other number in the list
    • if the number is greater than the first element in the array
      • swap their values
    • if the number is greater than the second element in the array
      • assign it to the second element

And that's it. You may notice that this algorithm will work no matter how many of the largest numbers you're looking for. Here's an example of how to do it in PHP:

function getBiggest($values, $quantity){

if(!is_array($values)) return $values;

$highvalues = array();

foreach($values as $value){

for($n = 0; $n < count($highvalues); $n++){

if($value > $highvalues[$n]){

$v = $value;

$value = $highvalues[$n];

$highvalues[$n] = $v;

}

}

if($n < $quantity) $highvalues[$n] = $value;

}

return $highvalues;

}

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

What is the algorithm to input 3 numbers and output them in ascending order?

To sort three numbers in ascending order, you can use a simple comparison-based algorithm. First, compare the first two numbers and swap them if the first is greater than the second. Then, compare the second number with the third and swap if necessary. Finally, check the first number against the second again to ensure they are in order. This process will yield the numbers in ascending order.


What is 1 7583837563728283727847372784829390493848728378183783748375564752748829?

Two numbers: the first of them is 1 and the second is not!Two numbers: the first of them is 1 and the second is not!Two numbers: the first of them is 1 and the second is not!Two numbers: the first of them is 1 and the second is not!


What is the algorithm for dividing a fraction by a fraction?

Multiply the first fraction by the reciprocal of the second. That is, flip the second fraction over and then multiply the two.


What are the first and second biggest countries?

Russia is first, followed by Canada.


What is 323x 133 equal to in Euclid's algorithm?

To find the product of 323 and 133 using Euclid's algorithm, we first note that Euclid's algorithm is typically used to find the greatest common divisor (GCD) of two numbers rather than their product. However, the product of 323 and 133 is simply 42,919. If you were looking to apply Euclid's algorithm, you would use it to find the GCD first, which can then be used to derive other relationships between the numbers, but it does not directly provide the multiplication result.


Write an algorithm to find the largest number in a group of three integer numbers?

The following algorithm works for any number of integers: Assume the first number is the maximum - maximum = (first number). Compare your assumed maximum with the second number. If the second number is larger than the assumed maximum, replace the old assumed maximum with the second number. Repeat for the third number, for the fourth, etc. - always copying the nth. element to the assumed maximum if you find one that is larger than your previous maximum.


How to write an algorithm that accepts five numbers and displays the sum and average of the numbers?

1.Start Algorithm 2.Enter first number 3.Enter second number 4.Enter third number 5.Enter fourth number 6.Enter fifth number 7.Add five number 8.display five number / 2 9.Display result 10.End Algorithm


Explain first fit algorithm?

A "first fit" algorithm is any algorithm which doesn't care about how "good" a solution is, it just returns the first one that works.


What is consecutive counting numbers?

Two numbers are consecutive if the second one is the one after the first. Mathematically, if the first number is n then the second is n+1.


The sum of two numbers is -42 the first number minus the second number is 52 Find the numbers?

= The sum of two numbers is -42 the first number minus the second number is 52 Find the numbers? =


How do you write an algorithm to accept two numbers and divide the first number by second and display their quotent?

To write an algorithm for dividing two numbers, follow these steps: Start by accepting two input numbers, say num1 and num2. Check if num2 is not equal to zero to avoid division by zero errors. If valid, calculate the quotient by dividing num1 by num2 and store the result. Finally, display the quotient. If num2 is zero, display an error message indicating that division by zero is not allowed.


Does new york have the second biggest population?

No. It's third in population. California is first and Texas is second.