Maths

  • Thread starter Thread starter Mike Kitchen
  • Start date Start date
M

Mike Kitchen

A small poser.

Given 5 random numbers betwen 1 and 6, what is the easiest way to add up the
3 highest numbers.
I have a somewhat long winded method, but I wondered if anyone has some
simple ideas using the Math class, or any other method.

Thanks in advance
 
Mike Kitchen said:
Given 5 random numbers betwen 1 and 6, what is the
easiest way to add up the 3 highest numbers.

Random rnd = new Random();

int[] i = new int[5];

for (int n = 0; n < i.Length; n++)
{
i[n] = rnd.Next(6) + 1; // between 1 and 6
}

Array.Sort(i);
Array.Reverse(i); // highest first

Console.WriteLine(i[0] + i[1] + i[2]); // sum of top three

P.
 
thanks

Mike

Paul E Collins said:
Mike Kitchen said:
Given 5 random numbers betwen 1 and 6, what is the
easiest way to add up the 3 highest numbers.

Random rnd = new Random();

int[] i = new int[5];

for (int n = 0; n < i.Length; n++)
{
i[n] = rnd.Next(6) + 1; // between 1 and 6
}

Array.Sort(i);
Array.Reverse(i); // highest first

Console.WriteLine(i[0] + i[1] + i[2]); // sum of top three

P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top