Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: A silly question about C

Author: Ricardo Gibert

Date: 11:20:56 07/21/04

Go up one level in this thread


On July 21, 2004 at 06:01:40, Tony Werten wrote:

>On July 20, 2004 at 18:09:47, Antonio Senatore wrote:
>
>>
>>Hi friends:
>>
>>I have an array of 100 elements (positive integers) and I need to know what is
>>the highest value stored in the array. Does anyone know a way faster than
>>
>>max_value = 0;
>>
>>for (i=0; i &le= 99; i++) {
>>       if (values[i] &ge max_value) max_value = values[i];
>>}
>>
>
>You are comparing 2 things: values[i] with max_value and i with 99. Bringing
>this down to (almost) 1 should improve it.
>
>store oo at values[100];

The above can be inconvenient and can be avoided by working from the outside
inward:

    i = 0; j = 99;
    while (1)
    {
        while (values[i] < values[j])  i++;
        if (i == j)  break;
        j--;
        while (values[j] < values[i])  j--;
        if (i == j)  break;
        i++;
    }
    max_value = values[i];

>
>max_value=values[0];
>i=1;
>while (true) {
>   while (values[i]<=max_value) inc(i);
>   if (i==100) break;
>   max_value=values[i];
>}
>
>Tony
>
>>I work in C
>>
>>Many thanks in advance
>>
>>Antonio



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.