Can I please get some help on an array problem? - page 2

 
LEHayes:

I think you might have it here. I will copy your code here and test it. I will get back and let you guys know if it solved the problem. Thank you all for your help.


I found a problem with what you had coded, so I made a minor change, I also corrected the print out so that it will tell us which column/row we are seeing in the output, finally, I added a print statement in the comparison condition that will allow us to see the data before we attempt to insert it. Here is the code:

for (i = 0; i <= ArraySize(LineArray); i++){  // Loop through LineArray one price value at a time
   FreqArySize = ArraySize(FrequencyArray);
   for (int x = 0; x <= FreqArySize; x++){  //Loop through frequencyarray to see if price value exists bump up counter
      if (FreqArySize > 1)
      {
         if (LineArray[i] == FrequencyArray[x][1]){
            FrequencyArray[x][0] += 1;
            break;
         }
      }
      
      if (x == FreqArySize){
         Print("Adding LineArray data: ", DoubleToStr(LineArray[i], Digits));
         ArrayResize(FrequencyArray, x+1);
         FrequencyArray[x][0] = 1;
         FrequencyArray[x][1] = LineArray[i];
         break;
      }
   }
}

Here is the output:

2010.08.19 14:51:45     TestLines EURUSD_fx,M1: , Line_0: 0.00000other dim: 2.00000, Line_1: 0.00000other dim: 1.00000, Line_2: 0.00000other dim: 1.00000, Line_3: 0.00000other dim: 1.00000, Line_4: 0.00000other dim: 1.00000, Line_5: 0.00000other dim: 1.00000, Line_6: 0.00000other dim: 1.00000, Line_7: 0.00000other dim: 1.00000, Line_8: 0.00000other dim: 1.00000, Line_9: 0.00000other dim: 1.00000, Line_10: 0.00000other dim: 1.00000, Line_11: 0.00000other dim: 1.00000, Line_12: 0.00000other dim: 1.00000, Line_13: 0.00000other dim: 
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Column 1: 0
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Column 0: 2
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.27121
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.27331
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.27331
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.27676
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.27719
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28108
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28143
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28143
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28171
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28179
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28179
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28187
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28190
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28191
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28196
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28196
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28202
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28220
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28236
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28250
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28275
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28357
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28367
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.28663
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.29015
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.29095
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.29219
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.29650
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.30082
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.30336
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Adding LineArray data: 1.33342
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 30 = 1.27121
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 29 = 1.27331
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 28 = 1.27331
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 27 = 1.27676
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 26 = 1.27719
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 25 = 1.28108
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 24 = 1.28143
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 23 = 1.28143
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 22 = 1.28171
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 21 = 1.28179
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 20 = 1.28179
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 19 = 1.28187
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 18 = 1.28190
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 17 = 1.28191
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 16 = 1.28196
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 15 = 1.28196
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 14 = 1.28202
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 13 = 1.28220
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 12 = 1.28236
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 11 = 1.28250
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 10 = 1.28275
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 9 = 1.28357
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 8 = 1.28367
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 7 = 1.28663
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 6 = 1.29015
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 5 = 1.29095
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 4 = 1.29219
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 3 = 1.29650
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 2 = 1.30082
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 1 = 1.30336
2010.08.19 14:51:45     TestLines EURUSD_fx,M1: Line Table 0 = 1.33342

It is easy to see that in the loop we still have the data, but it never gets inserted into the array.

 

I have and idea, I think I need to loop through frequency array first to see if the number exists, if not, then outside of the frequency array loop, I need to add the value of linearray. However, this still does not resolve why I am not getting any data into the frequency array. I will make this modification now.

 

Here is the focal point of the problem:

         ArrayResize(FrequencyArray, FreqArySize + 1);
         FreqArySize = ArraySize(FrequencyArray);
         FrequencyArray[FreqArySize][0] = 1;
         FrequencyArray[FreqArySize][1] = LineArray[i];
         Print("Adding LineArray data: ", DoubleToStr(LineArray[i], Digits), " FrequencyArray value is: ", DoubleToStr(FrequencyArray[FreqArySize][1], Digits));

Here is the latest output of the print statement in this code:

2010.08.19 16:15:15     TestLines EURUSD_fx,M1: Adding LineArray data: 1.30105 FrequencyArray value is: 0.00000
2010.08.19 16:15:15     TestLines EURUSD_fx,M1: Adding LineArray data: 1.30336 FrequencyArray value is: 0.00000
2010.08.19 16:15:15     TestLines EURUSD_fx,M1: Adding LineArray data: 1.33342 FrequencyArray value is: 0.00000
2010.08.19 16:15:15     TestLines EURUSD_fx,M1: Line Table 30 = 1.26217
regardless of any other code in the file, when we assign the value of one array to another array, we should get the value appearing in the second array on the output.
 

Here is a very simplified snippet of what I am doing:

      double a[1][1];
      double b[1];
      b[0] = 1.12345;
      a[0][0] = b[0];
      
      Print("Array B: ", DoubleToStr(b[0], Digits), ", Array A: ", DoubleToStr(a[0][0], Digits));

Here is the result I get:

2010.08.19 16:26:32     TestLines2 EURUSD_fx,M1: Array B: 1.12345, Array A: 1.12345
Why am I not getting this on the code I am trying to actually run? Why isn't the value being put into the second array?
 
LEHayes:


The second value is the column sizing, which MQL does not allow you to change the number of columns only the number of rows. The first value is the number of rows which is what I want to resize anyway. The declaration of [][1] means that I want 2 columns but at the moment no rows.

So the first thing I do in the dual for loop is to increase the rows and then add the data. This way, I am only increasing the needed number of rows as I have data to enter.

I believe that by example where I got the code from this part is correct.


According to my experience, the declaration of [][1] will give you 1 column, not 2 as you require.
 

I am convenced that there is a bug with the multi-dimensional array. I took the basic little test I made and placed data in multiple element locations and the only one that returned the data was the 0,0 location. Regardless of how many elements I allocated for either the rows or the columns.

 
FreqArySize = ArraySize(FrequencyArray);
FrequencyArray[FreqArySize][0] = 1;
FrequencyArray[FreqArySize][1] = LineArray[i];

If the array size is 5, index is 0 to 4. You are accessing one beyond the array.

The declaration of [][1] means that I want 2 columns but at the moment no rows.

No that means you want 1 column. [][2] means you have 2 columns [][0] and [1]
 
WHRoeder:

If the array size is 5, index is 0 to 4. You are accessing one beyond the array.

No that means you want 1 column. [][2] means you have 2 columns [][0] and [1]


You are not seeing the size increment prior to this entry the following is just above the section you are looking at:

ArrayResize(FrequencyArray, FreqArySize + 1);
This increases the arrays size to accomodate the entry.
 
LEHayes:


You are not seeing the size increment prior to this entry the following is just above the section you are looking at:

This increases the arrays size to accomodate the entry.

That increases the number of rows. Does NOTHING about the number of columns. The declaration must be [][2]


Also in your loop, when

if (FreqArySize > 1)
executes you will compare beyond the end of the array. Drop that If and move the x==size to the top:
   for (int x = 0; x <= FreqArySize; x++){  
      if (x == FreqArySize){ ... }
      if (LineArray[i] == FrequencyArray[x][1]){
            FrequencyArray[x][0] += 1;
            break;
      }
    }
      
 

I solved my array problem by avoiding the multiple array scenario. Because the data was finite, I knew what my overall count was going to be so I am living with this solution.

   double LineArray[38];
   int    CountArray[38];
   double PriceArray[38]; 
   int i = 0;
   
   for (i = 0; i <= 37; i++)
   {
      LineArray[i] = 0.0;
      CountArray[i] = 0;
      PriceArray[i] = 0.0;
   }
   
   
/* Populate LineArray data here */
   
   
   bool IsFound = false;
   for (i = 0; i <= ArraySize(LineArray); i++)
   {  // Loop through LineArray one price value at a time

      for (int x = 0; x <= ArraySize(CountArray); x++)
      {  //Loop through frequencyarray to see if price value exists bump up counter
         if (PriceArray[x] == LineArray[i])
         { // Loop through to find price and increment if found
            CountArray[x] = CountArray[x] +1;
            IsFound = true;
         }
      }
      
      if (IsFound == false)
      {
         CountArray[i] = CountArray[i] + 1;
         PriceArray[i] = LineArray[i];
      }
      else
      {
         IsFound = false;
      }

   }
   
   for (i = 0; i <= ArraySize(CountArray); i++)
   {
      if (PriceArray[i] > 0)
      {
         Print(DoubleToStr(PriceArray[i], Digits), "\t", DoubleToStr(CountArray[i], 0), "\t", DoubleToStr(LineArray[i], Digits));
      }
   }

I can also take the output from the log and dump it into excell thanks to using the tabs. Now all I have to do is process it.

I would like to thank everyone who had provided input. I hope my solution helps you too in the future.

Reason: