Counting the out-of-order elements of an array Is there any way to write is C function better so the procedure could spend less time to calculate the results?

Assume that the array size is 1.000.000 and  all the numbers are greater than 0.

The function read the array backwards, saves the first number to maximum, then check the next number, a, and if it's greater than the maximum, it adds +1 to total. Then the maxinum takes the value of a and procide to next one until the end of array.

static int total = 1;

int chck_high( int *my_array, int *endp) {
    // this function should only be called if there is at least one value in the array
    int maximum = *(--endp);

    while ( endp &gt; my_array ) {
        int a = *(--endp);

        if ( a &gt; maximum ) {
            total++;
            maximum = a;
        }
    }

    return maximum;
}


I tried to use unsigned ints, but I don't know if it's worth it. Can someone please tell me if there is a better way to write that code?

Previous version of the code:

int process( int *my_array, int *endp) { 
    int a, b; 
    //static int total = 1;
    if ( my_array == 0 ) return 0; 
    if ( my_array == endp ) return INT_MIN;
    else a = *my_array++; 
    if ( (b= process( my_array, endp )) == INT_MIN ) return a; 
    if ( a &gt; b ) 
    {total++; return printf( "%d &gt; %d and total now is %d\n", a, b, total ), a; } 
    return b; 
} 


The two functions called with the following code:

chck_high(my_array, my_array + count);


where my_array is my array with nums and count is the number that says the size of the array.
Avoid static variables as much as possible.
Instead of static total, you could pass in a pointer to total and make the function modify the value it's pointing to.

The names are not great:


Instead of my_array, start would be better
Instead of endp, end would be better
Instead of chck_high, ... I don't know what would be better, because I don't really see the general logic this function represents. It counts the number of times a new local maximum is found going backwards from the end of the range, and sets the value of total. I'm wondering if this logic is really necessary in this form, or perhaps the overall logic of your program could be redesigned to simpler elements.


Instead of a comment like this:


// this function should only be called if there is at least one value in the array



It would be better to use an assertion:

assert(start &lt; end);


Note that this requires to #include &lt;assert.h&gt;

This maybe a matter of taste, but I would find a for loop would be more natural for this instead of while. Using a for loop, in C99 and above, you could declare the loop variable inside the for, which would have extra benefits:


It would help you limit variables in the smallest scope necessary (inside the loop)
It would force you to use a new local variable for looping, instead of reusing the function parameter, which is a good thing


Putting it together, the function would become:

int chck_high(int *start, int *end, int *total) {
    assert(start &lt; end);

    int maximum = *--end;

    for (int * pos = end; pos &gt; start; --pos) {
        int a = *pos;
        if ( a &gt; maximum ) {
            ++*total;
            maximum = a;
        }
    }

    return maximum;
}


To enable C99 mode when compiling with gcc, use the -std=c99 flag.

You can use the function like this, for example:

int main() {
    int arr[] = {1, 2, 51, 41, 4, 5};
    int total = 1;
    int maximum = chck_high(&amp;arr[0], &amp;arr[0] + 6, &amp;total);
    printf("total=%d max=%d\n", total, maximum);
}