D02 Answer


#define NUMCOLS 15
#define NUMROWS 5

void print2DArray ( int x[][NUMCOLS], int nrows )
{
  int r, c;  /* row and column indexes for the array */

  /* Print elements in row major order */
  for ( r=0; r<nrows; r++ )
  {
    for ( c=0; c<NUMCOLS; c++ )
      printf("%3d ", x[r][c]  );
    printf("\n");
  }
}

Comments: If you want to put print2DArray() in a separate file for use with later puzzles, write it like this:

/* --- start of file myArray.h --- */
#define NUMCOLS 15

void print2DArray ( int x[][NUMCOLS], int nrows );
/* --- end of file -- */



/* --- start of file print2Darray.c --- */
#include <stdio.h>
#include <stdlib.h>
#include <myArray.h>

void print2DArray ( int x[][NUMCOLS], int nrows )
{
  ... as above ...
}

/* --- end of file -- */

Include myArray.h with any program that needs to use print2DArray() and make print2DArray.c part of the project.

Unfortunately, all your arrays will now have 15 columns (or whatever value you have given NUMCOLS.)