Min and Max

The C language is rather weak when it comes to array functions. In fact, as far as I know, the standard library doesn’t contain a single array function.

Well, yeah: You can count the strings.h functions as array functions, but they operate only on char arrays. Missing from the library are a host of other array functions, some of which you’d never know existed unless you’ve dabbled in some higher-level programming languages.

As a sample, here are some of my favorite array functions from other languages. (I’m using generic names in this examples.)

array_sort() This function gobbles a numeric array, sorts the contents low-high or high-low (however you specify) and returns the sorted array. Most languages have a slew of functions that sort arrays in every possible way.

array_average() This function returns the average value stored in the array. Similarly, other array functions let you mathematically manipulate the array’s contents in a variety of obscure ways.

array_chop() This function (actually a family of functions) slices, dices, splits and spits up an array in various ways.

array_max() This function returns the highest (maximum) value of all the values stored in the array.

array_min() This function returns the lowest (minimum) value stored in the array.

Any grizzled veteran of the C language will promptly explain to you that having such functions in the C library is utterly useless. After all, you can always code your own. And, in fact, that’s the assignment for this month.

Don’t fret! I’m not going to be cruel. In fact, the array_max() and array_min() functions are frequently found in most C programming tomes as basic function examples. Such examples are missing in my C programming books, but that doesn’t mean you miss out on the fun.

Below you see sample code that carries out the basic operations for the exercise: An array is created and filled with random values. Your job is to craft the array_max() and array_min() functions, stubbed out in the code.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 20

int array_max(int *a,int s);
int array_min(int *a,int s);

int main()
{
    int array[SIZE];
    int x;

/* Fill the array with random values */
    srand((unsigned)time(NULL));        /* seed randomizer */
    for(x=0;x<SIZE;x++)
        array[x] = rand() % 100 + 1;

/* Display the array */
    puts("The Array:");
    for(x=0;x<SIZE;x++)
        printf("%3d ",array[x]);
    putchar('\n');

    printf("Highest value: %d\n",
        array_max(array,SIZE));
    printf("Lowest value: %d\n",
        array_min(array,SIZE));

    return(0);
}

int array_max(int *a,int s)
{
}

int array_min(int *a,int s)
{
}

One of the limitations of C is that the array size must be passed to the function in addition to the array’s base address; unlike some higher-level languages, the array’s size isn’t information known to the program. (You could use sizeof to fetch the array’s size and then divide it by sizeof(int), but this code simply passes the SIZE constant to the array functions, which works.)

As I urge every month, please attempt to solve the puzzle on your own before you click the link below to view my solution. Also, keep in mind that my code is one of many potential solutions to the exercise.

Exercise Solution

Leave a Reply