Exercise 8 page 331

Solution index                   Textual C program


#include <stdio.h>

#define ROWS 5
#define COLS 4

int main(void) {

  double table [ROWS][COLS] =
    {{ 3.1,  5.5,  2.7, 19.3},
     { 0.2, 15.4, 16.9, -3.0},
     { 7.2,  5.4, 18.0, 13.7},
     {17.8, 15.4, 18.6,  3.0},
     { 7.1, -5.4,  8.7,  0.3}};

  double min_el = table[0][0], max_el = table[0][0];

  int i,j;

  for(i = 0; i < ROWS; i++)
    for(j = 0; j < COLS; j++){
      if (table[i][j] < min_el) min_el = table[i][j];
      if (table[i][j] > max_el) max_el = table[i][j];
    };

  printf("\nThe minimun element is %f, and the maxium is %f\n\n", min_el, max_el);
  return 0;
}

Besides this solution, there is also another solution to this exercise. The other solution uses only a single for loop, and array pointer access instead of array indexing.
 

#define ROWS 5
#define COLS 4
We are about to find the minimum and the maximum element in a two dimensional array. We start to define the diminensions of the array, by two symbolic constants ROWS and COLS. As a convention, we use upper case letters for such constants.
 

double table [ROWS][COLS] =
  {{ 3.1,  5.5,  2.7, 19.3},
   { 0.2, 15.4, 16.9, -3.0},
   { 7.2,  5.4, 18.0, 13.7},
   {17.8, 15.4, 18.6,  3.0},
   { 7.1, -5.4,  8.7,  0.3}};
The next step is the declaration and initialization of the array. The array is called table, the element type is double (a real number type), and the index range is [0 .. 4, 0 ..3] with the given constants discussed above. We use an initializer { ... } which allows us to put concrete numbers into the table, as needed. Alternatively we could have used an initialization programmed with a for loop, or we could have allowed the user of the program to give the initial values of the table (by means of scanf statements).
 

double min_el = table[0][0], max_el = table[0][0];
We declare the variables min_el and max_el, which we use to keep track of the minimum and maximum elements while we traverse the elements. Notice the initial values of these two variables. In general, it would be safe to initialize min_el with the largest possible double number, and similarly to initialize max_el with the smallest possible double number. We chose to initialize the variables to an arbitrary element in the array. This works because each element is to be considered for maximality and minimality.
 

for(i = 0; i < ROWS; i++)
  for(j = 0; j < COLS; j++){
    if (table[i][j] < min_el) min_el = table[i][j];
    if (table[i][j] > max_el) max_el = table[i][j];
  };
We traverse all elements in the array using nested for loops. We visit table[0][0], table[0][1], ..., table[0][3], table[1][0], table[1][1], ...., table[4][3]. Please make sure you understand the visiting order.
 

if (table[i][j] < min_el) min_el = table[i][j];
if (table[i][j] > max_el) max_el = table[i][j];
For each element we encounter we test if it is smaller than the current minimum, and if it is larger than the current maximum element. If so, we reassign min_el or max_el. Notice that we keep track of the minimum and maximum, but not the indexes in the array where the minimum and maxium element occur.
 

printf("\nThe minimun element is %f, and the maxium is %f\n\n", min_el, max_el);
return 0;
When the for loops is finished we print the minium and maxium numbers. For the given table, we get the following output:
The minimun element is -5.400000, and the maxium is 19.300000
 


Generated: Wednesday, March 29, 2006, 12:33:33
This program dissection page is generated from an XML-in-LAML source file