Okay, so this is not actually some problem but i realy need help here.

There are given some numbers (ex. 53, 67, 172, 18, 942, 342) and i need to find the largest one.

What is the best and easiest way to do this ?

Thank you.

Printable View

- 02-27-2009mutomboBest way to find maximum number
Okay, so this is not actually some problem but i realy need help here.

There are given some numbers (ex. 53, 67, 172, 18, 942, 342) and i need to find the largest one.

What is the best and easiest way to do this ?

Thank you. - 02-27-2009QuantumPete
Think about it this way. Before you traverse your array (or whatever data structure you're using) you have a maximum of 0. Then as you traverse your array, you compare the number with your current maximum. If the number is larger, make that the new maximum. When you've finished going through the array, you've got the largest number.

This doesn't work so well for negative numbers though.

QuantumPete - 02-27-2009mutombo
That's easy to do, but i have another problem in here now.

The numbers are stored in 2 arrays, so when i find the maximum i also need to find out where that number is placed. Example:

x[1] = 10

x[2] = 152

y[1] = 199

y[2] = 987

In this case it is easy to find that 987 is the maximum, but i also need to know that the maximum number is placed in y[2].

Any ideas ? - 02-27-2009matsp
So, as before, you start with a max that is really small (e.g. 0). Then you walk along the array(s) and whenever you find a new "biggest so far", you set a second and third variable to indicate which position and which array it is.

--

Mats