Sorted List Presentation, Part IV

Last week’s Lesson accomplished the task of obtaining the number of elements in the fruit[] array. This week, the task is to obtain the next item, the widest string in the array. This item is currently a constant, but it must become a variable to ensure the code’s flexibility.

Here are the variables the code needs to calculate:

  1. The number of elements in the array – DONE.
  2. The length of the widest string in the array.
  3. The number of columns in the output.
  4. The number of rows in the output.

You must first know the widest string in the array before you can determine how many columns of output you need, and then calculate the number of rows. Remember, this series assumes the terminal window is fixed to 80-characters-wide for output. So, if the widest string is 78 characters, output has only one column. If the widest string is 9 characters, you could squeeze in 10 columns.

To determine the widest string in an array you need a type of max() function. This is a typical demo function found basic programming instruction courses: Return the maximum value in an array.

#include <stdio.h>

int max(int a[])
{
    int x,max;

    max = 0;        /* must initialize max */
    for(x=0;x<8;x++)
    {
        if(a[x] > max)
            max = a[x];
    }

    return(max);
}

int main()
{
    int array[] = {
        2, 13, 3, 17, 5, 19, 7, 11
    };

    printf("The largest value is %d\n",
            max(array));

    return(0);
}

In the max() function, each array element is compared with variable max. If the element is larger, it’s assigned as the new value for max. After all elements are scanned, the value of max is returned.

The max() function has many variations. It’s often a challenge in C to see how few lines of code can be used to write it, as well as how obnoxiously obfuscated it can read.

Here’s the output:

The largest value is 19

A similar approach can be taken for strings. For example:

int widest(char **fruit, int i)
{
    int x,len,max;

    max = 0;
    for(x=0;x<i;x++)
    {
        len = strlen(*(fruit+x));
        max = len > max ? len : max;
    }

    return(max);
}

The first, and perhaps most frightening thing in the widest() function, is the **fruit declaration. This monster is required to pass an array of strings to a function. (Refer to this Lesson for specifics.)

The widest() function itself works similar to the max() function presented earlier. For each element in the array (variable i), string length is obtained. The *(fruit+x) monster grabs each element. The ternary operator sets the value of variable max. This method is yet another way to express the “max” expression. And the largest value — the widest string length — is returned.

When processing the fruit[] array (the example used in this series), here’s the output:

The widest string is 11 chars long.

Click here to view the entire code. This example also incorporates the trick from last week's Lesson that determines the number of elements in the array.

You might wonder why the longest string itself isn't displayed in the output. Keep in mind that the purpose of the width() function is to obtain the longest string length, which is item number two on the list of variables to calculate in the code. The longest string itself isn't important, but its width is necessary to determine the maximum number of columns in which to present the list on an 80-character-wide terminal screen.

Next week's Lesson presents the final two calculations required, both of which rely upon the value of the width variable.

Leave a Reply