## DemonGal711 8

I have to write a code that will produce a 2^n by 2^n board and then fill it (filling it isn't important, I think I got that down). Now, since my board is dynamic, any suggestions on how to deal with this. I'm passing my array from my main function to a void function where it will be printed. So, what would be the best way to handle this?

I figure I have to use pointers to accomplish this, but is that the best way to handle it. And, how would that look. Someone said they were doing something like
int a[m][m]
int *ptr = a;
int **ptr = ptr[m];

but then they said they were getting a lot of bugs. So, any suggestions on how to tackle this. I'm new to pointers so I'm not sure what to try.

## stilllearning 148

Are you familiar with vectors ? If so you can use a 2 dimensional vector and that way you wouldn't have to mess with the memory allocation yourself.

here is an example of how you can allocate and use a 2 dimensional array with pointers.

## Alex Edwards 321

Here's an example of encapsulating a 2D vector and emulating the behavior or a 2D vector through a class--

``````#include <iostream>
#include <vector>
#include <cmath>

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::ostream;
using std::size_t;

class TwoDimensional{

vector<int> dummyVec;
vector< vector<int> > rowContent;

public:
TwoDimensional(unsigned int rowSize = 0, unsigned int columnSize = 0){
rowContent = vector< vector<int> >( static_cast<int>( pow(static_cast<double>(2), static_cast<double>(rowSize)) ),
vector<int>( static_cast<int>( pow(static_cast<double>(2), static_cast<double>(columnSize))), 0));
}

ostream& showDimensions(ostream& out = cout){
out << "Column Size: " << rowContent[0].size() << "\n";
out << "\nRow Size: " << rowContent.size();
return out;
}

ostream& showContents(ostream& out = cout){
for(size_t i = 0; i < rowContent.size(); i++){
for(size_t j = 0; j < rowContent[i].size(); j++){
out << rowContent[i][j] << " ";
}
out << endl;
}
return out;
}

vector<int>& operator[] (unsigned int indice){
if(indice >= 0 && indice < rowContent.size()){
return rowContent[indice];
}
return dummyVec;
}
};

int main(){

TwoDimensional td (2, 4); // 2^N rows, 2^T columns, in this case 2^2 rows, 2^4 columns
td[2][3] = 5;
td.showDimensions() << endl;
td.showContents();
cin.ignore();
cin.get();

return 0;
}``````

## DemonGal711 8

As far as my teacher is concerned, we haven't learned vectors yet so I don't think he'd want us to use them, hence why he told us to use a 2-D array.

So, basically I'm making an array of pointers, passing that array to my function, and then using the pointers to figure out which array I'm using?

It's still reads like any array though by using array[0][0] to read the first one right?

## Sci@phy 97

int a[m][m]
int *ptr = a;
int **ptr = ptr[m];

This doesn't make any sense. If you have to assign dynamic memory, you cannot type `int a[m][m];` .

Yes, you have to use pointers. Don't worry, they're not too scary.
And using 2D array of pointers is a little itchy.

First, you have to create pointer to array of pointers of size m:

``int** array = new int*[m];``

Now you have "outer shell, but you need for each pointer-to-pointer to make m new pointers to int:

``````for (int i = 0; i < m; i++)
array[i] = new int[m];``````

And that's it. After that you can use it just like array: `array[i][j];` etc.

Hope to help :)

## DemonGal711 8

Okay, I got that part, my problem is passing that pointer to another function so I can access each individual element.

Going with Sci@phy array[][], I tried to pass the n (num of cols and rows) and the array like this.
print (n, array);

Then I called it up in my print function like this.

``````void print(int n , int **ar)
{ decor(n);
for (int x = 0; x < n; x++)
{ for (int y = 0; y < n; y++)
{ cout << "| " << ar[x] << " "; }
cout << "|";
decor(n);
}
}``````

It compiles, but it's not the right answer. How do we fix this?

## Sci@phy 97

Remember that array is double pointer, just like ar. You cannot write ar[x], it is 2D array: ar[x][y]!

## chococrack 74

ar[x][y]

it was a 2d array wasn't it?

## DemonGal711 8

Okay, I had originally tried that and it didn't work. I tried it again and it did. Oh well.

Anyway, thanks all.