D - accesing an array
- Carlos (28/28) Jun 22 2002 This might sound stupid, but I can't access the elements of an array by
- Pavel Minayev (5/9) Jun 23 2002 This line should probably read g[cg[i]] = new int[4];
- Carlos (13/21) Jun 23 2002 Why? I'm only trying to access the members of an array. They're indexed ...
- Pavel Minayev (9/26) Jun 23 2002 Further in your code, you use cg[i] to index g[]. Suppose i == 3,
- Carlos (4/12) Jun 23 2002 by
This might sound stupid, but I can't access the elements of an array by using values in other array. This is the code: int [][]g=new int[8][]; int [8] cg=-1; for (int i=0;i<8;i++) { //store random numbers between 0 & 7 in cg bit r; do { r=true; cg[i]=rand()%8; for (int j=0;j<i;j++) if (cg[i]==cg[j]) r=false; //no repetition } while (!r); } int [] ord=new int[np]; //np is really bigger than 8 ... for (int i=0;i<8;i++) { g[i]=new int[4]; //is there another way to do it? //printf("%d ",cg[i]); //it was to check the values g[cg[i]][0]=ord[i]; //ERROR } in the line marked ERROR I get: Error: ArrayBoundsError Why?? I mean, g.length (I've checked) is 8, and cg only has values between 0 and 8. So what's wrong? Oh, and if I change cg[i] for just i, it goes ok, but the result is not what I want. ------------------------- Carlos 8294 http://carlos3.netfirms.com/
Jun 22 2002
On Sat, 22 Jun 2002 21:26:50 -0500 "Carlos" <carlos8294 msn.com> wrote:g[i]=new int[4]; //is there another way to do it?This line should probably read g[cg[i]] = new int[4];//printf("%d ",cg[i]); //it was to check the values g[cg[i]][0]=ord[i]; //ERROR }Since you haven't allocated g[cg[i]] yet, [0] is not a valid subscript.
Jun 23 2002
"Pavel Minayev" <evilone omen.ru> escribió en el mensaje news:CFN374305216151273 news.digitalmars.com...On Sat, 22 Jun 2002 21:26:50 -0500 "Carlos" <carlos8294 msn.com> wrote:Why? I'm only trying to access the members of an array. They're indexed by integers, and cg[] are integers. I don't get it.g[i]=new int[4]; //is there another way to do it?This line should probably read g[cg[i]] = new int[4];Then why does it work? void something(inout int[][]g) { bit [32] p; for (int i=0;i<8;i++) p[g[i][0]]=true; ... } It's the same case.//printf("%d ",cg[i]); //it was to check the values g[cg[i]][0]=ord[i]; //ERROR }Since you haven't allocated g[cg[i]] yet, [0] is not a valid subscript.
Jun 23 2002
On Sun, 23 Jun 2002 13:21:36 -0500 "Carlos" <carlos8294 msn.com> wrote:Further in your code, you use cg[i] to index g[]. Suppose i == 3, cg[3] == 6. When you do g[i] = new int[4], you create an array, storing it at g[3]. Then you do g[cg[i]], that is, g[6], which is an empty array: it will only be created be when i will reach value of 6...Why? I'm only trying to access the members of an array. They're indexed by integers, and cg[] are integers. I don't get it.g[i]=new int[4]; //is there another way to do it?This line should probably read g[cg[i]] = new int[4];Then why does it work? void something(inout int[][]g) { bit [32] p; for (int i=0;i<8;i++) p[g[i][0]]=true; .... } It's the same case.You mean, you do it on your array.
Jun 23 2002
"Pavel Minayev" <evilone omen.ru> escribió en el mensaje news:CFN374310422767593 news.digitalmars.com...On Sun, 23 Jun 2002 13:21:36 -0500 "Carlos" <carlos8294 msn.com> wrote:byWhy? I'm only trying to access the members of an array. They're indexedThanks! It was a programming mistake. I hadn't notice that. Thanks.integers, and cg[] are integers. I don't get it.Further in your code, you use cg[i] to index g[]. Suppose i == 3, cg[3] == 6. When you do g[i] = new int[4], you create an array, storing it at g[3]. Then you do g[cg[i]], that is, g[6], which is an empty array: it will only be created be when i will reach value of 6...
Jun 23 2002