#include <stdio.h> #include <stdlib.h> /* Puzzle DD01 -- fill an N by M integer array with ascending values and print them out. */ int main() { const int Nrows = 3, Mcols = 5 ; int x[Nrows][Mcols] ; int count = 0; /* Values to copy to the array */ int r, c; /* row and column indexes for the array */ /* Fill the array with ascending integers */ for ( r=0; r<Nrows; r++ ) for ( c=0; c<Mcols; c++ ) x[r][c] = count++ ; /* Print out the array */ for ( r=0; r<Nrows; r++ ) { for ( c=0; c<Mcols; c++ ) printf("%2d ", x[r][c] ); printf("\n"); } printf("\n"); return 0; }
Here is a version for compilers that compain about variable array dimensions:
#include <stdio.h> #include <stdlib.h> #define Nrows 10 #define Ncols 15 int x[Nrows][Mcols] ; /* Puzzle DD01 -- fill an N by M integer array with ascending values and print them out. */ int main() { int count = 0; /* Values to copy to the array */ int r, c; /* row and column indexes for the array */ /* Fill the array with ascending integers */ for ( r=0; r<Nrows; r++ ) for ( c=0; c<Mcols; c++ ) x[r][c] = count++ ; /* Print out the array */ for ( r=0; r<Nrows; r++ ) { for ( c=0; c<Mcols; c++ ) printf("%2d ", x[r][c] ); printf("\n"); } printf("\n"); return 0; }