Link to home
Start Free TrialLog in
Avatar of swingler
swingler

asked on

Create an array of pointers to two dimensional arrays

At first glance this seemed as though it should be easy but I'm having a tough time figuring out how to make this happen.  So basically what I need to know is the syntax for declaring an array of pointers to 2D arrays of integers and how to make the pointers point to my two dimensional arrays of integers. Thanks in advance for any help.
Avatar of stefan73
stefan73
Flag of Germany image

Hi swingler,
Use vectors. For readability, you can use a typedef chain:

typedef int payload;
typedef vector< vector < payload > > TwoDimArr;
typedef vector<2Darr*> NestedArr;

...and there you are.

Cheers,
Stefan
Avatar of Axter
Example:
      int Array[3][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
      int (*Arrayp)[5] = &Array[0];

      for(int x= 0;x < 3;++x)
      {
            for(int y= 0;y < 5;++y)
                  cout << Arrayp[x][y] << ", ";
      }
If you must use horrible pointers and horrible arrays, in the following we have an array of 2D arrays and the imaginatelyly named p points to the first of these....

--------8<--------
#include <iostream>

int main()
{
      typedef int int_100_x_100[100][100];
      int_100_x_100* p = new int_100_x_100[10];

      int value = 0;
      for (int i = 0;i < 10;i++)
            for (int x = 0;x < 100;x++)
                  for (int y = 0;y < 100;y++)
                        p[i][x][y] = ++value;

      {
            int i,x,y;
            std::cout << "Choose an array 0..9: ";std::cin >> i;
            std::cout << "Choose an x-coord 0..99: ";std::cin >> x;
            std::cout << "Choose a y-coord 0..99: ";std::cin >> y;
            if (i >= 0 && i < 10 && x >= 0 && x < 100 && y >= 0 && y < 100)
                  std::cout << "Value is: " << p[i][x][y] << '\n';
      }

      delete []p;
}
--------8<--------
Avatar of swingler
swingler

ASKER

I'm going to sweeten the pot, as it seems there is a better solution than my idea of an array of pointers to 2D arrays

stefan73-
If I were to use your typedef chain how would that effect the way in which I would access the data. Sample code would be appreciated

rstaveley-
I have to agree that the use of pointers and arrays isn't my ideal situation. Would you suggest a solution similar to stefan73's proposed solution?
swingler,

No comment on the code I posted, which does answer your original question?
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> I have to agree that the use of pointers and arrays isn't my ideal situation. Would you suggest a solution similar to stefan73's proposed solution?

what would be the situation which you think you have to use an array of pointers to two dimensional arrays? that would better qualify what 'solution' should be used.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Axter-
 I appologize if you are feeling left out. Your solution does indeed answer my original question; however, after reading your solution and stefan73's solution I realized pointers to arrays was probably not the simplest solution for me to implement. My grasp of pointers has always been tenuous at best, and if I can avoid using them I'm all for it. I think I was getting in over my head with an array of pointers to 2D arrays. :)

CoolBreeze-
 I have three sparse matrices that I need to be able to compare and manipulate(add & remove items) and pass to functions.


>> I have three sparse matrices that I need to be able to compare and manipulate(add & remove items) and pass to functions.

Can you give us the declartions for these functions?
Axter-
 Sorry, I can't. They haven't been written yet.
>>Sorry, I can't. They haven't been written yet.

Then maybe you can give us a better idea of what you're trying to do, or what is your end goal.

The more information you can give us about your requirements, the better are the odds that you'll get a satisfactory answer.
My end goal is to create 2 2D matrices and add points to it and then to compare the 2D matrices and store the points that match in a third 2D matrix and to output that third matrix.
from my understanding, the size of a matrix is unlikely to change (maybe I'm wrong). Even if it changes, usually you don't assign it back to the same matrix (am I right?).

so I would suggest just a linear 1D array. First element stores the number of columns, second element stores the number of rows. The rest of the elements of the array stores the actual data. And if your matrices' size is going to be large, you can apply run-length encoding here, simple and fast (less fast than without the encoding).

assuming without encoding, comparing two matrices is just comparing the third element to the last element of the two arrays. Storing the points is just copying the elements that are equal to a third array.

by adding/removing items, do you actually mean changing an element in the matrix? I can't imagine a 2x2 matrix with one more element added in.

I don't see the reason of using pointers here, maybe I am missing something.
>>My end goal is to create 2 2D matrices and add points to it and then to compare the 2D matrices and store the points that match in a third 2D matrix and to output that third matrix.

Easy example:

void SomeFunc(int (*Array1)[5], int (*Array2)[5], int (*Array3)[5])
{
     for(int x= 0;x < 3;++x)
     {
          for(int y= 0;y < 5;++y)
               Array3[x][y] = Array1[x][y] + Array2[x][y];
     }
}

int main(int , char*)
{
     int Array1[3][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
     int Array2[3][5]={{1,1,1,1,1},{1,1,1,1,11},{11,11,11,11,11}};
     int Array3[3][5];

     SomeFunc(&Array1[0], &Array2[0], &Array3[0]);
      
Thanks for the input everyone!
rstavely-
 Your sample code causes a seg fault on the resize statement.

intVV.resize(100);

 Any ideas why that might be?
Works fine for me on GNU 3.2 and VC 7.1, and it looks like standard stuff. What's your environment?
It shouldn't be necessary, but what happens if you replace...

    intVV.resize(100);

...with....

    intVV.reserve(100); // Reserve the elements first (shouldn't be necessary!)
    intVV.resize(100); // Now resize the array, using the reserved elements

If it still crashes, does it bomb out on the reserve or resize?
Try:

      IntVVV intVVV(10);
      std::cout :: "Size is " << intVVV.size() << '\n';

...just to confirm that the IntVVV array is initially correctly sized as 10.

If you've got a manky STL implementation, you could try...

      IntVVV intVVV;
      //intVVV.reserve(10);   // <- Try this too, if it still plays up
      intVVV.resize(10);

...to resize the array explicitly... but it really oughtn't to be necessary.

Please let me know what you find. I'm curious.
Doh!

Sorry rstaveley, your code works great. Chalk that up to user error. I created an instance of IntVVV but didn't specify any size, like so:

IntVVV intVVV;

Once I initialized the IntVVV with a size everything fell into place.

Thanks for your help!
No worries, swingler, glad that it wasn't the Universe falling down around me as it is apt to do from time to time ;-)
//I hope the following code will solve the problem
//here I tried to create a 2X3 matrix with a single dimension array created dynamically
int row=2,col=3 //these values can be accepted dynamically
int val=1,i=0,j=0;

//create and insert into 2D array
/*matrix will look like following
1     2    3
4     5    6
*/

int *matrix=NULL;
matrix= new int[row*col];
for (i=0;      i<row;      i++)
     for(j=0;      j<col;      j++, val++)
     {
      cout<<((2*i)+(i+j))<<endl;
      m[(2*i)+(i+j)] = val;
    }

//Display 2D array
for (i=0;      i<row;      i++)
   {
      for(j=0;      j<col;      j++)            
            cout<<m[(2*i)+(i+j)]<<"      ";
      cout<<endl;
   }      

delete [] m; //releasing the memery allocated
Guys please note the changes made.
there were some errors in my previous comment.
I'm really sorry for misleading you guys..

//here I tried to create a 2X3 matrix with a single dimension array created dynamically

int row=2,col=3; //these values can be accepted dynamically

//create and insert into 2D array
int val=1,i=0,j=0;

int *matrix=NULL;
matrix= new int[row*col];
for (i=0;      i<row;      i++)
      for(j=0;      j<col;      j++, val++)
      {
            cout<<((2*i)+(i+j))<<endl;
            matrix[(2*i)+(i+j)] = val;
      }

//Display 2D matrix
for (i=0;      i<row;      i++)
{
      for(j=0;      j<col;      j++)            
            cout<<matrix[(2*i)+(i+j)]<<"      ";
      cout<<endl;
}      

delete [] matrix;   //Release the memory allocated
What are those hard-coded 2s doing in there?

Perhaps this helps...
--------8<--------
#include <iostream>
#include <iomanip>

int main()
{
      int rows = 2,columns = 3;

      int *matrix = new int[rows*columns];

      int value = 1;
      for (int row = 0;row < rows;++row)
            for (int column = 0;column < columns;++column)
                  matrix[row*columns+column] = value++;

      for (int row = 0;row < rows;++row) {
            for (int column = 0;column < columns;++column)
                  std::cout << std::setw(4) << matrix[row*columns+column];
            std::cout << '\n';
      }

      delete []matrix;
}
--------8<--------
Your mistake was that you were making your row size governed by the number of rows rather than the number of columns. Using more explicit variable names makes it more obvious.
Yea..you are correct..
Hi rstaveley
I'm really sorry to tell u that that piece of code will not work rather we can go for my code...
did u test that code before posting it..

suppose when u insert items...
the second row will overwrite the contents of the first row..i think i'm correct just chekc it out
shibi
Just checked it (compiled with VC 7.1) and got this output:

   1   2   3
   4   5   6

Looks OK to me.

With VC 6, you need to put a scope round the for loops to prevent it from brumbling about reusing the variable name and return 0, because it predates the standard.

i.e.
--------8<--------
#include <iostream>
#include <iomanip>

int main()
{
     int rows = 2,columns = 3;

     int *matrix = new int[rows*columns];

     int value = 1;
{
     for (int row = 0;row < rows;++row)
          for (int column = 0;column < columns;++column)
               matrix[row*columns+column] = value++;
}

{
     for (int row = 0;row < rows;++row) {
          for (int column = 0;column < columns;++column)
               std::cout << std::setw(4) << matrix[row*columns+column];
          std::cout << '\n';
     }
}

     delete []matrix;
     return 0;
}
--------8<--------

If you are using GNU 3.2+, you should be OK.

Are you getting a logic error or a compilation error?

The second row doesn't overwrite the first because the row variable offsets the index by the required number of columns.

      matrix[row*columns+column] = value++;
                ^^^