Question: Hello, I am working on a exercise with C++ regarding Pointers. I am asked to modify the implementation of a grid using pointers. The idea
Hello, I am working on a exercise with C++ regarding Pointers. I am asked to modify the implementation of a grid using pointers. The idea to modify the code that creates figure 1, to create figure 2. I have attached the sample code and image for reference.

// Example of a program that uses pointers and nested for-loops to create a table
#include
void print(int ** table, int maxrow, int maxcol);
int main()
{
int ** grid; // pointer to a pointer to integers to create the grid int *element; // pointer to an int to move through the row int col; // loop control variable int row; // control variable int maxr, maxc; // maximum number of rows and columns that the dynamic array will have char ans;
do {
system("cls");
cout
cin >> maxr >> maxc;
grid = new int*[maxr]; // create a dynamic arrays of POINTERS TO INTEGERS with maxr elements
cout
for (row = 0; row
grid[row] = new int[maxc]; // create a dynamic array of INTEGERS with maxc elements to implement it
for (int row = 0; row
for (int col = 0; col
grid[row][col] = (row + 1) * 10 + (col + 1); // store the calculated value
for (row = 0; row
{
element = grid[row]; // store in element the address of the first element in the row
for (col = 0; col
{
cout
cout
cout
element++; // move on to the next element in the row using pointer arithmetic
}
cout
}
system("pause");
cout
print(grid, maxr, maxc); // call function print to display the table
cout
cout
for (row = 0; row
delete[] grid[row]; // delete the dynamic array created to implement it
delete[] grid; // delete the array of POINTERS TO INTEGERS used to access the rows
//cout
grid = nullptr; // set grid to the null pointer
cout
cout
cin >> ans;
} while (ans == 'y' || ans == 'Y');
return 0;
}
void print(int ** table, int maxrow, int maxcol) // receives the pointer to the grid and the max number of rows and columns to display
{
int row, col;
for (row = 0; row
{
cout
for (col = 0; col
{
cout
}
cout
}
cout
}
In this exercise, you have to modify the provided implementation of a grid using pointers (see figure 1) so that it is implemented as shown in figure 2. Figure 1 Figure 2 In the implementation of Figure 1 the array of pointers is used to access the rows of the grid while in the implementation of Figure 2 the array of pointers is used to access the columns
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
