What if C++ had no built-in facility for two-dimensional arrays? It is possible to emulate them yourself with wrapper functions around a one- dimensional array. The basic idea is shown below. Consider the following two-dimensional array:
int matrix[2] [B];
It can be visualized as a table:
matrix[0][0] | matrix[0][l] | matrix[0][2] |
matrix[l][0] | matrix[ 1 ] [ 1 ] | matrix[l][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 matrixlD[6];
matrix[0][0] | matrixlD[0][l] | matrixlD[0][2] | matrixlD[l][0] | matrixlD[l][l] | matrixlD[l][2] |
Here, the mapping is as follows:
matrix[0][0] would be stored in matrixlD[0]
matrix[0][l] would be stored in matrixlD[l]
matrix[0][2] would be stored in matrixlD[21
matrix[l][0] would be stored in matrixlD[3]
matrix[l][l] would be stored in matrixlD[4]
matrix[l][2] would be stored in matrixlD[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 two- dimensional 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 Lwo-dimensional array of size rows * col umns.
Note that int ptr = create2DArray(2,3) ; would create an array analogous to that created by i nt ptr [2] [3];
void set (int *arr, int rows, int columns,
int desired_row, int desired_column, int val);
This stores val into die emulated two-dimensional array at position desired_row, desi red_col umn. 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 die 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 die column the caller would like to access.
val is die value to store at desired_row and desired_col umn.
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, desi red_col umn. 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_col umn is the zero-based index of the column the caller would like to access.
Create a suitable test program that invokes all three functions.
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.