Sorted List Presentation, Part V

The process of converting the sample program from one that relies upon constants to one that exclusively uses variables is nearly complete. Only two steps are left:

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

By obtaining the widest string in the array (from last week’s Lesson), and assuming that output is sent to a screen that’s 80-columns wide, you can calculate the maximum number of columns possible and then determine the rows required for output. Both values, columns and rows, are then used for the sorted-column process outlined in an earlier Lesson in this series.

The first step is to determine how many columns of strings can fit comfortably across the screen. The assumed width is 80 characters. So, logically, you divide 80 by the widest string in the array. The problem with this approach is that the width value could divide evenly into 80. That would leave no space between the columns. Therefore, you must also add padding to the end of each string displayed, then calculate the number of columns:

/* set string width for printf() format */
    width = widest(fruit,items);
    if( width % 2 )
        width += 3;     /* assert even width */
    else
        width += 2;     /* at least two spaces padding */

Above, the widest() function, presented in last week’s Lesson, obtains the longest string length from array fruit[]. The items variable represents the number of elements in fruit[]. The value returned, the length of the longest string, is stored in variable width.

The if-else structure adds padding to the length of the widest string. If the string is an odd number, determined when width%2 is true, width is increased by 3. Otherwise width is increased by 2. That way at least 2 spaces of padding are added to each column in the output.

To determine how many columns of output can fit across the screen, I use this calculation:

columns = OUTPUT_WIDTH/width;

The OUTPUT_WIDTH constant is set to 80. So if the widest string is 11 characters, the code would pad that out to 14 characters: 80/14 is just under six, so the integer result is 5. The output for an array with the widest string at 11 characters would be displayed in 5 columns.

To obtain the number of rows, I use this equation:

rows = items/columns+1;

The number of items in the array divided by columns obtains the number of rows necessary in a table — but, 1 is added to the result. This increment accounts for the rounding error in OUTPUT_WIDTH/width for the columns calculation.

For example, suppose the array has 36 items and the OUTPUT_WIDTH/width calculation results in 5 columns: 36/5 results in 7 rows (due to integer rounding). Yet 7 rows of 5 columns shows only 35 items. Therefore, you must increment the value of rows to ensure that all array elements are displayed in the table.

Now you have all four former-constants translated into variables. It’s possible at this point to present the final code, but I have one more trick to show you, which I’ll save for next week’s Lesson, the final installment of this series.

Leave a Reply