Question
PROGRAM SPECIFICATION For this program, we are going to pretend that C++ has no built-in facility for two-dimensional arrays. It is possible to emulate them
PROGRAM SPECIFICATION
For this program, we are going to pretend that C++ has no built-in facility for two-dimensional arrays. It is possible to emulate them yourself with wrapper functions around a one dimensional array.
Consider the following two-dimensional array:
int matrix[2][3];
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
int matrix1D[6];
matrix1D [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]
Write a program that includes the following functions:
int* create2DArray(int rows, int columns);
This function 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 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 desiredRow, int desiredColumn, int val);
This function stores val into the emulated two dimensional array at position desiredRow, desiredColumn. 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.
desiredRow is the zero-based index of the row the caller would like to access.
desiredColumn is the zero-based index of the column the caller would like to access.
val is the value to store at desiredRow and desiredColumn.
int get(int *arr, int rows, int columns, int desiredRow, int desiredColumn);
This function returns the value in the emulated two dimensional array at position desiredRow, desiredColumn. 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.
desiredRow is the zero-based index of the row the caller would like to access.
desiredColumn is the zero-based index of the column the caller would like to access.
Complete the program that invokes all three functions in a streamlined main() function, as usual.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started