(a) In this task we consider ptr as a 2D matrix. Whereas, number of rows would be fixed but make number of columns variables. To do this, instead of using arrays of fixed column size allocated on stack, you will make each element of array to have nColumns (passed as argument to the function) elements and store them on heap using new operator. Next, initialize the values of these arrays randomly using for loop and ptr. Finally display the sum of each individual array. You are not allowed to access the array using original variable name. (b) In the previous task we make number of columns of a 2D matrix variables. Here we will make number of rows variables as well. Thus we will have a way of defining a generic 2D matrix according to user choice. Now your goal is to write a function that receives three arguments: (i) an alias to a 2D pointer; (ii)number of rows; and (iii) number of columns; Now your goal is to first allocate the memory for rows and then for columns dynamically using new operator. (c) In this function, your goal is to write code for deallocating a dynamically allocated 2D matrix. Your function will receive three arguments: (i) a 2D pointer; (ii) number of rows; and (iii) number of columns. Complete the code to properly deallocate.
(a) In this task we consider ptr as a 2D matrix. Whereas, number of rows would be fixed but make number of columns variables. To do this, instead of using arrays of fixed column size allocated on stack, you will make each element of array to have nColumns (passed as argument to the function) elements and store them on heap using new operator. Next, initialize the values of these arrays randomly using for loop and ptr. Finally display the sum of each individual array.
You are not allowed to access the array using original variable name.
(b) In the previous task we make number of columns of a 2D matrix variables. Here we will make number of rows variables as well. Thus we will have a way of defining a generic 2D matrix according to user choice. Now your goal is to write a function that receives three arguments: (i) an alias to a 2D pointer; (ii)number of rows; and (iii) number of columns; Now your goal is to first allocate the memory for rows and then for columns dynamically using new operator.
(c) In this function, your goal is to write code for deallocating a dynamically allocated 2D matrix. Your function will receive three arguments: (i) a 2D pointer; (ii) number of rows; and (iii) number of columns. Complete the code to properly deallocate.
Step by step
Solved in 2 steps with 1 images