Author: Heiner Marxen
Date: 07:00:43 01/04/05
Go up one level in this thread
On January 04, 2005 at 08:43:45, Antonio Senatore wrote:
>
>Hi friends:
>
>I need to find the index of the highest value in an array of N integer positive
>numbers. Does anyone know a faster algorithm than the following:
>
>int find_index (int *arr, int N) {
>
> int best_val = -INF, i, index;
>
> for (i = 0; i < N; i++) {
> if (arr[i] > best_val) {
> index = i;
> best_val = arr[i];
> }
> }
> return index;
>}
>
>Thank you very much in advance
>Antonio
You could use arr[0] to initialize best_val (and index=0),
and start with i=1.
BTW, you return random garbage for N<=0.
Cheers,
Heiner
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.