Double Array Problem!!! Plz come in...

Hi all,

I try to implement the following: I have 3 sets of integers which may not be equal in size. Let's say first has 4 integers, second has 2 integers and last one has 3 integers. How can I store these? I give the integers at initialization and I want to leave the brackets empty! I don't want to use malloc or sth. like that. I tried following and failed:

Try 1 : int a[][] = {{0,1,2,3},{7,7},{8,8,8}};

Try 2 : int b[3][] = {{0,1,2,3},{7,7},{8,8,8}};

Try 3 : int *a[3];
a[0][] = {0,1,2,3}; ...

I'm waiting for your comments. Thanx.
Try1 and Try2 fails as the 'column' dimensions cant be different in each 'row' .. Not sure, maybe the compiler adds zeros and makes a 3*4 2D array(in this case)

Try3, should definitely not work.

If you want to use a 2D array with different column sizes, use 'vectors'. They are quite easy, and would make your life simple! Plus you can get so many other useful functions with it.
http://cplusplus.com/reference/stl/vector/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
using namespace std;

int main ()
{
vector< vector<int> > a; /*2D array*/
vector<int> tmp; /* a temp 1D array which will be used to add each row*/

/*first row*/
tmp.push_back(0);
tmp.push_back(1);
tmp.push_back(2);
tmp.push_back(3);
a.push_back(tmp);

/*second row*/
tmp.clear();
tmp.push_back(7);
tmp.push_back(7);
a.push_back(tmp);

/*third row*/
tmp.clear();
tmp.push_back(8);
tmp.push_back(8);
tmp.push_back(8);
a.push_back(tmp);
}
thanx for replies. In fact I know column sizes can't be different in arrays but I wonder if there is any trick for that... vectors are useful in this case. I'll use them, but if you have anything to say, don't hesitate :)
Ofcourse there is a trick.

just use

int a[3][4] = {{0,1,2,3},{7,7},{8,8,8}};

The places where the default values arent assigned, get initialized with zero (hopefully!)
Zero points would be a problem since every 3 integers are a point in 3D space (I'm doing a openGL project)! What about modified TRY3:

int *a[3];

a[0] = new int [] {1,2,3,4};
a[0] = new int ........

Of course this is wrong! Do you know the correct version?
Dude,
you first tell that you dont want to use malloc or something, and now you want to use new!

Anyway,

1
2
3
4
5
int *a[3];

a[0] = new int[4] ;
a[1] = new int[2];
a[2] = new int[3];


This should work. Try running it.

I'm just fooling around, sorry... My problem is that integer set sizes may change in the future so I just want to give the set and let the compiler do the allocation... I think I don't need vector because during the execution array sizes won't change. If 'new' is capable of allocating memory for a given array (sth like I gave), that would be great but I haven't seen yet. I think there is.
new is dynamic. That means it can allocate on non-constant expressions (where as normal allocation on the stack may not). This also means you can reassign the pointers that control them to other arrays to change size in compile time. (Though why you wouldn't just use a vector is beyond me.) Be sure to delete a dynamic array when finished with it; the data is not automatically removed. Something in the references will tell you how to operate this stuff.
Using vector is the best solution!

I would like to know if anybody can suggest a shorter easier method to assign default values to a multi-dimensional vector. For example, I need to do so much circus to init a 2D vector:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int a[4][3] = {{1,2,3},{7,7,7},{8,8,8},{5,6,7}};
vector< vector<int> > b(3,3);
vector<int> tmp;
int i,j;

for(i=0;i<4;i++)
{
    tmp.clear();

    for(j=0;j<3;j++)
        tmp.push_back(a[i][j]);

    b.push_back(tmp);
}


What a painful method to initialize a vector, in comparison to a 2D array!!

Anybody has a better solution to intialize a 2D vector?

I did the following and it didn't work! :

-------------------------------------------
int cubeVertices[] = {1, 2, 3, 4, 5 ,6};
int cylinderVertices[] = {1, 2, 3, 4, 5 ,6, 7, 8, 9};
int coneVertices[] = {1, 2, 3};

int *vertices[3];

vertices[0] = cubeVertices; // gives error here...
vertices[1] = cylinderVertices;
vertices[2] = coneVertices;
-------------------------------------------
Oops.. I am sorry to make a mistake in one of the earlier posts for int *a[3]; usage with new .

You are right to get an error here.
1
2
int *vertices[3];
vertices[0] = cubeVertices;


coz, vertices is a pointer to an int array of size 3!! That means vertices is NOT an array, and so you cannot use vertices with square brackets [].
Or
coz, vertices is a pointer to an int array of size 3!! Since you try here to point to an array of size 6, you get an error.

I am sorry but I never work with array of pointers, so I may be wrong again in the explanation above.
it works in visual c++ 6 but not in c++ 2008 express...
I was wrong with this statement
vertices is a pointer to an int array of size 3!!




Topic archived. No new replies allowed.