views:

274

answers:

3

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.

+4  A: 

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

I82Much
thanks, I guess your answer defines part of the implementation section so at the end I should have something like this:class TwoDimensionalArray { private: int **array; public: TwoDimensionalArray(const int, const int);};TwoDimensionalArray::TwoDimensionalArray(const int rows, const int columns){ array = new int*[rows]; for(int i=0; i < rows; i++) array[i] = new int[columns];}int main(){ TwoDimensionalArray *arr1 = new TwoDimensionalArray(5, 10); return 0;}
KJP
A: 

This should give you an idea. I haven't tried to compile this code:

class Array {
   int **arr;
};

Array::Array(int rows, int cols) {
    arr = new int * [rows];  // this will allocate 'rows' many 'int *'s
    if (arr) {               // to ensure memory was allocated
        for (int i = 0; i < rows; i++) {
            arr[i] = new int [cols];  // this will allocate 'cols' many 'int's
            assert(arr[i]);           // to ensure memory was allocated
        }
    }
}

arr is a pointer to an array of int pointers. arr[i] is a pointer to an array of ints i.e. each arr[i] represents one row in your 2-D array. This should get you started.

If you really want to learn C++, try things yourself and learn how to use a debugger. If you aren't comfortable with a debugger yet, use printfs to find out what is going wrong.

Ashwin
A: 

`

class TwoDimensionalArray
{
private:
int *array;
public:
TwoDimensionalArray(const int, const int);
};
TwoDimensionalArray::TwoDimensionalArray(const int rows, const int columns)
{
array = new int
[rows];
for(int i=0; i < rows; i++)
array[i] = new int[columns];
}

int main()
{
TwoDimensionalArray *arr1 = new TwoDimensionalArray(5, 10);
return 0;
}
`

KJP