Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In c++ for beginners please, using basic libraries, this chapter introduces pointers and dynamic arrays 7. What if C++ had no built-in facility for two-dimensional
In c++ for beginners please, using basic libraries, this chapter introduces pointers and dynamic arrays
7. 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][3]: It can be visualized as a table: matrix olIO matrix/1 ]/o matrix/01(1]matrix 01121 matrix] 1]1] matrix[ 1 ]12] 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!101 Here, the mapping is as follows: matrix[o]I0] would be stored in matrix1D[O] matrix[o][1] would be stored in matrixiD[1) matrix(o][2] would be stored in matrix1D[2) matrix[1][0] would be stored in matrixiD[3] matrix(1j[1] would be stored in matrixiD[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 two-dimensional array and returns a pointer to the one-dimensional dynamic array. array rows is the number of rows desired in the two-dimensional 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 desired_column is the zero-based index of the column the caller would val is the value to store at desired_row and desired_column. to access. like to access. 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 desired_ row is the zero-based index of the row the caller would like desired_co1umn is the zero-based index of the column the caller would array to accesS. like to access Create a suitable test program that invokes all three functions 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