What is the Maximum Value?

One of the first functions C programmings learn is max(). It’s simple and useful, and it teaches a lot about functions and evaluations. Most programming languages also demonstrate functions similar to max().

For this week’s Lesson, I decided to put the max() function to the test, and apply it to more than two values. I wanted the function to do two things:

1. Return the largest non-zero value in an integer array.
2. Operate on multiple arrays of different sizes.

The first task is effectively the max() function: Compare two values and return the larger. Though with an array, each value is compared with the next. When a larger value is found, it’s stored and used again to make the comparison. Here’s roughly how it works:

x = max = 0;
while(length_of_array)
{
    if( max < array[x] )
        max = array[x];
    length_of_array--;
    x++;
}

After the array is processed, variable max contains its highest value. Being a nerd, of course, I would rewrite the if statement as:

max = max < array[x] ? array[x] : max;

This ternary statement performs the same action as the if statement, plus it enjoys being cryptic.

The max() function, or I call it maximum(), must know when the array ends. As arrays aren't delimited in C, I included -1 as the final element's value. It's also possible to determine the array's size by using the sizeof operator. This value can be passed to the maximum() function as an argument, but I opted not to take this approach.

To output the values, I crafted a second function, output(). This function arose to eliminate redundancy in the main() function where the array's were originally processed.

I also opted to use pointers to the array as function arguments instead of passing the array itself. To me, this approach seems logical, though the puzzle can be solved either way, no problem.

Here's my code:

2020_09_26-Lesson.c

#include <stdio.h>

/* return the maximum value in the array */
int maximum(int *a)
{
    int x,max;

    x = max = 0;
    while( *(a+x) != -1 )
    {
        max = max < *(a+x) ? *(a+x) : max;
        x++;
    }

    return(max);
}

/* output the results */
void output(int *a)
{
    int r,x;

    x = 0;
    r = maximum(a);
    printf("Of these values:");
    while(*(a+x) != -1 )
    {
        printf(" %d,",*(a+x));
        x++;
    }
    printf(" the value %d is the greatest.\n",r);
}

int main()
{
    /* all arrays must terminate with -1 */
    int a1[] = { 20, 9, 19, 11, 17, 3, 51, 1, 26, -1 };
    int a2[] = { 909, 400, 765, 1000, 22, 45, -1 };
    int a3[] = { 3, 9, 12, -1 };

    output(a1);
    output(a2);
    output(a3);

    return(0);
}

Each of the arrays declared in the main() function have different values and different lengths. They're passed to the output() function, which calls the maximum() function to obtain the highest value in each array. Here's a sample run:

Of these values: 20, 9, 19, 11, 17, 3, 51, 1, 26, the value 51 is the greatest.
Of these values: 909, 400, 765, 1000, 22, 45, the value 1000 is the greatest.
Of these values: 3, 9, 12, the value 12 is the greatest.

It's possible to recode the maximum() function to output the smallest integer value in each array. Three changes must be made.

First, the max variable (which you can re-dub min) is initialized with the first element's value, not zero. If you initialize the variable to zero, zero is always returned.

Second, the comparison operator at Line 11 needs to be > not <.

Third, the output text must be adjusted to replace "greatest" with "smallest."

Upon making these changes, here is the updated output:

Of these values: 20, 9, 19, 11, 17, 3, 51, 1, 26, the value 1 is the smallest.
Of these values: 909, 400, 765, 1000, 22, 45, the value 22 is the smallest.
Of these values: 3, 9, 12, the value 3 is the smallest.

It delights me when seemingly simple exercises, such as exploring the max() function, can yield interesting results and be used to help continue exploration and understanding of the C language.

Leave a Reply