www.digitalmars.com         C & C++   DMDScript  

D - accesing an array

reply "Carlos" <carlos8294 msn.com> writes:
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
parent reply Pavel Minayev <evilone omen.ru> writes:
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
parent reply "Carlos" <carlos8294 msn.com> writes:
"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:

   g[i]=new int[4];          //is there another way to do it?
This line should probably read g[cg[i]] = new int[4];
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.
   //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.
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.
Jun 23 2002
parent reply Pavel Minayev <evilone omen.ru> writes:
On Sun, 23 Jun 2002 13:21:36 -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];
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.
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...
 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
parent "Carlos" <carlos8294 msn.com> writes:
"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:

 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.
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...
Thanks! It was a programming mistake. I hadn't notice that. Thanks.
Jun 23 2002