Problem With Assigning Value to an Array

 

Hey all, first post...I have searched the forums and the documentation to try and figure out why this is not working. Here is my code:

extern int TimeFrame1 = 1;
extern int TimeFrame2 = 5;
extern int TimeFrame3 = 15;
extern int TimeFrame4 = 30;
bool NewBarTF[4] = {false false false false};
int BarCount[4] = {0 0 0 0};
int TFArray[4];
double StoArray[4];
double IndMatTF1[1][1];
double IndMatTF2[1][1];
double IndMatTF3[1][1];
double IndMatTF4[1][1];




int start()
  {
  int BarInd=0;
  for(int j=0;j<=3;j++)
   {
      if(NewBarTF[j] == true)
         {
            switch(j)
            {
               case 0:
                  BarInd = BarCount[j];
                  ArrayResize(IndMatTF1,BarInd);
                  IndMatTF1[BarInd,0] = StoArray[j];
               case 1:
                  ArrayResize(IndMatTF2,BarCount[j]);
                  IndMatTF2[0,BarCount[j]] = StoArray[j];
               case 2:
                  ArrayResize(IndMatTF3,BarCount[j]);
                  IndMatTF3[0,BarCount[j]] = StoArray[j];
               case 3:
                  ArrayResize(IndMatTF4,BarCount[j]);
                  IndMatTF4[0,BarCount[j]] = StoArray[j];                  
            }   
            BarCount[j] = BarCount[j] + 1;      
         }
   }
   
   return(0);
  }

The line in case 0:

                  IndMatTF1[BarInd,0] = StoArray[j];

Does not properly assign the value in StoArray element j to IndMatTF1. I have tried checking all elements and nothing all zeros all the time even when StoArray[j] is not a zero.

Any help would be greatly appreciated.

Thanks,

J

 
jacad:

Hey all, first post...I have searched the forums and the documentation to try and figure out why this is not working. Here is my code:

The line in case 0:

Does not properly assign the value in StoArray element j to IndMatTF1. I have tried checking all elements and nothing all zeros all the time even when StoArray[j] is not a zero.

Any help would be greatly appreciated.

Thanks,

J


I figured it out, so just in case anyone else runs into this:

Apparently trying to assign an array element to another array element doesn't work so I had to assign as follows:

BarInd = BarCount[j];
ArrayResize(IndMatTF1,BarInd);
b = StoArray[j];
IndMatTF1[BarInd,0] = b;

Hope this helps someone else avoid a headache!

J

 
jacad:

Apparently trying to assign an array element to another array element doesn't work [...]

That should work fine, you must have a logic error. For example this works just fine:

double a[]={1,2}; 
double b[]={3,4};
a[0]=b[1];
Print("a[0]=",a[0]);
 
gordon:

That should work fine, you must have a logic error. For example this works just fine:


Thanks for the response gordon. Yeah I am able to get a statement like what you presented to work, what I had done was use array1(element1) as an index for another array i.e. array2(array1(element1),0) which then had an element from array3 assigned ==>

array2(array1(element1),0) = array3(element1);

I'm not sure if there was an error in how I was typing in the indices for array2 or if it doesn't allow a variable array element to be used as an index, eitherway it's working now!

Thanks,

J

 
jacad:

[...] what I had done was use array1(element1) as an index for another array i.e. array2(array1(element1),0) which then had an element from array3 assigned [...]

It should still work fine as long as the array used as an index for another array is of type int (otherwise it won't compile, with the error "array index is to be an integer"):

double a[]={1,2}; 
double b[]={3,4};
int c[]={0,1};
a[c[1]]=b[1];
Print("a[1]=",a[1]);
 
double IndMatTF1[1][1];
...
BarInd = BarCount[j];
ArrayResize(IndMatTF1,BarInd);
IndMatTF1[BarInd,0] = StoArray[j];
  1. You might try
    double IndMatTF1[][1];

  2. BarCount[j] is zero in the posted code. move the increment up before the ArrayResize. or use
    ArrayResize(IndMatTF1,BarInd+1);
Reason: