Answered step by step
Verified Expert Solution
Link Copied!

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
image text in transcribed
image text in transcribed
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

Communicate based on goals

Answered: 1 week ago

Question

1. Explain how technology has changed the learning environment.

Answered: 1 week ago