D03 Answer


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

/* Puzzle D03 -- print an N by M integer array*/

void print2DArray ( int *x, int nrows, int ncols )
{
  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<ncols; c++ )
      printf("%3d ", *(x+ncols*r+c)  );
    printf("\n");
  }
}

int main(int argc, char *argv[])
{
  int x[3][7] = { { 0,  1,  2,  3,  4,  5,  6},
                  { 7,  8,  9, 10, 11, 12, 13},
                  {14, 15, 16, 17, 18, 19, 20} };
   
  /* Print the array using our function */
  print2DArray( x, 3, 7  );
  
  printf("\n");
  system("pause");	
  return 0;
}