What if C++ had no built-in facility for two-dimensional arrays? It is
possible to emulate them yourself with wrapper functions around a onedimensional
array. The basic idea is shown below. Consider the following
two-dimensional array:
int matrix[2][3];
It can be visualized as a table:
matrix[0][0] matrix[0][1] matrix[0][2]
matrix[1][0] matrix[1][1] matrix[1][2]
The two-dimensional array can be mapped to storage in a one-dimensional
array where each row is stored in consecutive memory locations (your
compiler actually does something very similar to map two-dimensional
arrays to memory).
int matrix1D[6];
matrix[0][0] matrix1D[0][1] matrix1D[0][2] matrix1D[1][0] matrix1D[1][1] matrix1D[1][2]
Here, the mapping is as follows:
matrix[0][0] would be stored in matrix1D[0]
matrix[0][1] would be stored in matrix1D[1]
matrix[0][2] would be stored in matrix1D[2]
matrix[1][0] would be stored in matrix1D[3]
matrix[1][1] would be stored in matrix1D[4]
matrix[1][2] would be stored in matrix1D[5]
Based on this idea, complete the definitions for the following functions.
int* create2DArray(int rows, int columns);
This creates a one-dimensional dynamic array to emulate a twodimensional
array and returns a pointer to the one-dimensional
dynamic array.
rows is the number of rows desired in the two-dimensional array.
columns is the number of columns desired in the two-dimensional array.
Return value: a pointer to a one-dimensional dynamic array large
enough to hold a two-dimensional array of size rows * columns.
Note that int ptr = create2DArray(2,3); would create an array analogous
to that created by int ptr[2][3];
void set(int *arr, int rows, int columns,
int desired_row, int desired_column, int val);
This stores val into the emulated two-dimensional array at position
desired_row, desired_column. The function should print an error message
and exit if the desired indices are invalid.
arr is the one-dimensional array used to emulate a two-dimensional array.
rows is the total number of rows in the two-dimensional array.
columns is the total number of columns in the two-dimensional array.
desired_row is the zero-based index of the row the caller would like
to access.
desired_column is the zero-based index of the column the caller
would like to access.
val is the value to store at desired_row and desired_column.
int get(int *arr, int rows, int columns,
int desired_row, int desired_column);
This returns the value in the emulated two-dimensional array at position
desired_row, desired_column. The function should print an error message
and exit if the desired indices are invalid.
arr is the one-dimensional array used to emulate a two-dimensional array.
rows is the total number of rows in the two-dimensional array.
columns is the total number of columns in the two-dimensional array.
desired_row is the zero-based index of the row the caller would like
to access.
desired_column is the zero-based index of the column the caller
would like to access.
Create a suitable test program that invokes all three functions.
Sorry the answer is not available at the moment…
If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.
Spread the 'tradition of sharing'.