E
Eric Lilja
Assignment: Create a 3x4 2-dimensional array of integers. Populate each
element using some non-random scheme so that no two elemens contain the
same value. Then create two functions for printing the 2D array. One
should only work for set column size, accepting only the number of rows
as an additional argument, one should accept both the number of rows
and columns as arguments.
So, I came up with:
#include <stdio.h>
static void print_2d_array(int[][4], int);
static void print_2d_array_2(const int *, int, int);
int
main(void)
{
int array[3][4] =
{
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11},
};
int num_rows = sizeof(array) / sizeof(array[0]);
int num_cols = sizeof(array[0]) / sizeof(array[0][0]);
print_2d_array(array, num_rows);
print_2d_array_2(&array[0][0], num_rows, num_cols);
return 0;
}
/* Even if specify int array[2][4], the compiler would
happily accept our 3x4 array. Therefore we leave
that dimension empty, passing the number of rows
as an argument. However, if we try to pass a 2D-array
where the number of columns is not 4, the compiler
will complain. */
static void
print_2d_array(int array[][4], int num_rows)
{
int row = 0;
int column = 0;
for(row = 0; row < num_rows; ++row)
for(column = 0; column < 4; ++column)
printf("[%i][%i] = %i\n", row, column, array[row][column]);
}
static void
print_2d_array_2(const int *ptr, int num_rows, int num_columns)
{
int currow = 0;
int curcol = 0;
int offset = 0;
for(; currow < num_rows; ++currow)
{
for(curcol = 0; curcol < num_columns; ++curcol) /* curcol = 0
needed! */
{
/* The layout of the 2D-array is linear as seen by our
program,
on a row-by-row basis. All rows have the same number of
columns.
Rows and columns are indexed starting from 0.
So if we have five colums per row and we want the element
in
the second column of the second row, we want element with
offset
six (offset 0 gives us the element at [0][0]).
offset = index_of_wanted_row * num_cols_per_row +
index_of_wanted_column
or
offset = 1 * 5 + 1 = 6 */
offset = currow * num_columns + curcol;
printf("[%i][%i] = %i\n", currow, curcol, *(ptr + offset));
}
}
}
Output when run:
$ ./foo.exe
Calling print_2d_array().
[0][0] = 0
[0][1] = 1
[0][2] = 2
[0][3] = 3
[1][0] = 4
[1][1] = 5
[1][2] = 6
[1][3] = 7
[2][0] = 8
[2][1] = 9
[2][2] = 10
[2][3] = 11
Calling print_2d_array_2().
[0][0] = 0
[0][1] = 1
[0][2] = 2
[0][3] = 3
[1][0] = 4
[1][1] = 5
[1][2] = 6
[1][3] = 7
[2][0] = 8
[2][1] = 9
[2][2] = 10
[2][3] = 11
Is this what my instructor is after?
/ E
element using some non-random scheme so that no two elemens contain the
same value. Then create two functions for printing the 2D array. One
should only work for set column size, accepting only the number of rows
as an additional argument, one should accept both the number of rows
and columns as arguments.
So, I came up with:
#include <stdio.h>
static void print_2d_array(int[][4], int);
static void print_2d_array_2(const int *, int, int);
int
main(void)
{
int array[3][4] =
{
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11},
};
int num_rows = sizeof(array) / sizeof(array[0]);
int num_cols = sizeof(array[0]) / sizeof(array[0][0]);
print_2d_array(array, num_rows);
print_2d_array_2(&array[0][0], num_rows, num_cols);
return 0;
}
/* Even if specify int array[2][4], the compiler would
happily accept our 3x4 array. Therefore we leave
that dimension empty, passing the number of rows
as an argument. However, if we try to pass a 2D-array
where the number of columns is not 4, the compiler
will complain. */
static void
print_2d_array(int array[][4], int num_rows)
{
int row = 0;
int column = 0;
for(row = 0; row < num_rows; ++row)
for(column = 0; column < 4; ++column)
printf("[%i][%i] = %i\n", row, column, array[row][column]);
}
static void
print_2d_array_2(const int *ptr, int num_rows, int num_columns)
{
int currow = 0;
int curcol = 0;
int offset = 0;
for(; currow < num_rows; ++currow)
{
for(curcol = 0; curcol < num_columns; ++curcol) /* curcol = 0
needed! */
{
/* The layout of the 2D-array is linear as seen by our
program,
on a row-by-row basis. All rows have the same number of
columns.
Rows and columns are indexed starting from 0.
So if we have five colums per row and we want the element
in
the second column of the second row, we want element with
offset
six (offset 0 gives us the element at [0][0]).
offset = index_of_wanted_row * num_cols_per_row +
index_of_wanted_column
or
offset = 1 * 5 + 1 = 6 */
offset = currow * num_columns + curcol;
printf("[%i][%i] = %i\n", currow, curcol, *(ptr + offset));
}
}
}
Output when run:
$ ./foo.exe
Calling print_2d_array().
[0][0] = 0
[0][1] = 1
[0][2] = 2
[0][3] = 3
[1][0] = 4
[1][1] = 5
[1][2] = 6
[1][3] = 7
[2][0] = 8
[2][1] = 9
[2][2] = 10
[2][3] = 11
Calling print_2d_array_2().
[0][0] = 0
[0][1] = 1
[0][2] = 2
[0][3] = 3
[1][0] = 4
[1][1] = 5
[1][2] = 6
[1][3] = 7
[2][0] = 8
[2][1] = 9
[2][2] = 10
[2][3] = 11
Is this what my instructor is after?
/ E