Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a class using a two-dimensional dynamic array

I have a homework assignment. I'm not looking for anyone to do the work for me, I'm just having trouble with one little aspect, although I'd accept advice on other bits as well.

The assignment is:

Write a class using a two-dimensional dynamic array.

The constructor passes in the dimensions of the array. The constructor also intializes all values in the dynamic array to the row index multiplied by the column index.

  1. Swap two columns of the two-dimensional array, where the column indexes are passed in as parameters. Do this just by copying addresses, not values of column elemnets.
  2. Delete a column of the two-dimensional array, where the column index is passed in as a parameter. Do not just use the delete operator on the column array and set the horizontal array element to NULL. Shrink the size of the horizontal array by 1.
  3. Create a print function for the class to print out the values of the two-dimensional array and make sure that your functions are working correctly. After you know that they are working correctly, delete the print function.

I need help understanding how to declare the 2D array in the private section. And, as mentioned, if anyone could give me other hints on how to do it, that would be appreciated.

like image 802
KJP Avatar asked Aug 06 '26 22:08

KJP


1 Answers

It's been awhile since I've done C++ development, but if I remember correctly, you'd do something like th efollowing:

int rows = 5;
int cols = 10;

int** array = new int*[rows];
for (int i = 0; i < rows; i++) {
     array[i] = new int[cols];
}

I might be mistaken; I see posts to the contrary online where you have to fake 2D arrays with a single dimensional array and change your subscripting:

http://en.allexperts.com/q/C-1040/creating-2D-array-dynamically.htm

like image 122
I82Much Avatar answered Aug 08 '26 11:08

I82Much



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!