Question: 4. Two-dimensional dynamic arrays The concept of dynamic array that you implemented in Problem 3 above can be extended to a multi-dimensional array. In this
4. Two-dimensional dynamic arrays The concept of dynamic array that you implemented in Problem 3 above can be extended to a multi-dimensional array. In this program, you will implement a dynamic 2-D array. Declaration Just like a one dimensional array name is a pointer, a 2-D array name is also a pointer, but it is a different type of pointer. For example, you declare an ordinary pointer as int "p: A double-referenced pointer will be declared as int *p 2D Dynamic Memory Allocation: You allocate memory to a 2D array in a similar way you do to a 1D array. For example, if we want to allocate memory to pointer p: p-new intIsizel: where p is the name of the pointer, int is the data type, size is the size of the array. Similarly, if we want to allocate memory to pointer p 2D: P 2D-new int [row_size] where p 2D is the name of the pointer, int* is the data type, row size is the size of the 2D array. A 2D array is an array with 1D arrays as members, and each row of the 2D array is a member 1D array. In a 2D array, the row size is related to the size of the 2D array (i.e. how many 1D arrays are in that 2D array) while the column size is associated with the type of the 2D array (i.e. how big the individual 1D arrays are). Here we don't know column size at compile time and hence we use int* Now we need to allocate memory to each row of the 2D array. For this we can use a loop as: 5 (int //Prompt user for the value of column_size p 2D[i]-new int [column_size] for =0;rowsize: ++i) - Dereferencing: To extract the values from a 2D array, you have to deference twice-hence it can also be
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
