Can I please get some help on an array problem?

 

I have this array of 31 price values. I want to compare them to a new array FrequencyArray[][1] where I store the count of how frequent a particular price appears in the list of 31 price values and their price value itself. So we will use LineArray[] as the mass list and do a lookup on FrequencyArray to see if the price exists, if it does, then we just bump up the number in that arrayth location, if it does not exist, we add it and bump the arrayth location holding the count for the new price value.

Here is the code:

void DetermineMasterLines(string Username, string Password, double &TopLine, double &BottomLine)
{   
   double LineArray[31];
   double FrequencyArray[0][1];
   int i = 0;
   
/*

    Code for collecting and populating data for LineArray.  It works so I am not going to post it.

*/

   

   int FreqIndex = 0; 
   int FreqArySize = 0;
   int FreqCount = 0;
   bool IsFound = false;

   FreqArySize = ArraySize(FrequencyArray);

   for (i = 0; i <= ArraySize(LineArray); i++)        // Loop through LineArray one price value at a time
   {         
      if (FreqArySize == 0)                          // If first entry to frequencyarray, then just insert value
      {
         ArrayResize(FrequencyArray, 1);
         FrequencyArray[0][0] += 1;
         FrequencyArray[0][1] = LineArray[i];
         FreqCount += 1;
      }
      
      FreqArySize = ArraySize(FrequencyArray);
      
      if (i > 0)                                          // Skip first loop, first price already inserted
      {
         for (int x = 0; x <= ArraySize(FrequencyArray); x++)  //Loop through frequencyarray to see if price value exists bump up counter
         {
            if (LineArray[i] == FrequencyArray[x][i])
            {
               FrequencyArray[x][0] += 1;
               IsFound = true;
               break;
            }
         
         }
         FreqArySize = ArraySize(FrequencyArray);
         
         if (IsFound == false)                     // Not found, bump up counter and add price value
         {
            ArrayResize(FrequencyArray, 1);
            FreqCount += 1;
            FrequencyArray[FreqCount][0] += 1;
            FrequencyArray[FreqCount][1] = LineArray[i];
         }
      }
      
   }
   FreqArySize = ArraySize(FrequencyArray);

   for (int h = 0; h <= FreqCount; h++)          // Cheesy print out to see if it works.
   {
      Print ("here: ", FrequencyArray[h][0]);
      Print ("here: ", FrequencyArray[h][1]);
   }
   
   string Msg = "";                                             // Ignore rest, just more print out
   for (i = 0; i <= FreqArySize; i++)
   {
      ObjectCreate("Line_"+i, OBJ_HLINE, 0, 0,FrequencyArray[i][1]);
      ObjectSet("Line_"+i, 1, FrequencyArray[i][1]);
      Msg = StringConcatenate(Msg, ", Line_", i, ": ", DoubleToStr(FrequencyArray[i][1], Digits), "other dim: ", DoubleToStr(FrequencyArray[i][0], Digits));
   }
  Print(Msg);

}

I will respond to this thread with the results so you can see what I am working with. Problem is that I am not getting the data of LineArray into the FrequencyArray and I am not sure what I am doing wrong.

 

Here is the output:

2010.08.17 16:32:11     TestLines EURUSD_fx,M1: , Line_0: 0.00000other dim: 2.00000, Line_1: 0.00000other dim: 0.00000
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 0
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: here: 2
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 30 = 1.27069
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 29 = 1.27331
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 28 = 1.27331
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 27 = 1.27553
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 26 = 1.28038
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 25 = 1.28185
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 24 = 1.28242
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 23 = 1.28669
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 22 = 1.28706
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 21 = 1.28731
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 20 = 1.28731
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 19 = 1.28754
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 18 = 1.28763
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 17 = 1.28763
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 16 = 1.28763
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 15 = 1.28763
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 14 = 1.28763
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 13 = 1.28764
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 12 = 1.28813
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 11 = 1.28815
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 10 = 1.28817
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 9 = 1.28817
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 8 = 1.28817
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 7 = 1.29009
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 6 = 1.29154
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 5 = 1.29154
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 4 = 1.29301
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 3 = 1.29785
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 2 = 1.30336
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 1 = 1.30417
2010.08.17 16:32:11     TestLines EURUSD_fx,M1: Line Table 0 = 1.33342
 

LEHayes,

I haven't had an indepth look, but check out the way you have declared the double FrequencyArray[0][1];

I see that you are referencing FrequencyArray[0][ 0 ] & FrequencyArray[0][ 1 ]......the latter does not exist since your array only has 1 element in the second dimension.

 

I was curious about that, but isn't "ArrayResize(FrequencyArray, 1);" supposed to increment the first dimension? I followed some sample code I found (more than one) and they initiated the code in the same way and incremented the array size like I am doing. Not that I disagree with you, I am just trying to understand the right way to do it and why their way was wrong. They seemed to have a much heavier use for the arrays than what I am doing, it should be fairly simple.

 

You are correct, ArrayResize will resize the first dimension. But in your case, the problem lies with the second dimension which only has 1 element and you reference it with FrequencyArray[xyz_variable][0]..............FrequencyArray[xyz_variable][1] does not exist.

 

Just thought I would give my opinion. MT4 help says

int ArrayResize( object &array[], int new_size)
Sets a new size for the first dimension.

I think MT4 wants you to use dimension 2 and above for things that you know the size of but use dimesion 1 for how many there are so you can vary that count.
Since MT4 doesn't handle arrays properly I always stick to single dimension arrays.
 
         if (IsFound == false)                     // Not found, bump up counter and add price value
         {
            ArrayResize(FrequencyArray, 1);

If it's not found, you reset the size back to one instead of increasing the array size.

       if (LineArray[i] == FrequencyArray[x][i])

You're comparing to [x][i] instead of [x][1]

Also, think about whither you should be comparing two doubles with == versus approximately equal.

Simplified 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 (x == FreqArySize){
         ArrayResize(FrequencyArray, x+1);
         FrequencyArray[x][0] = 1;
         FrequencyArray[x][1] = LineArray[i];
         break;
      }
      if (LineArray[i] == FrequencyArray[x][1]){
         FrequencyArray[x][0] += 1;
         break;
      }
   }
}
 
Ruptor:

Just thought I would give my opinion. MT4 help says

int ArrayResize( object &array[], int new_size)
Sets a new size for the first dimension.

I think MT4 wants you to use dimension 2 and above for things that you know the size of but use dimesion 1 for how many there are so you can vary that count.
Since MT4 doesn't handle arrays properly I always stick to single dimension arrays.
What do you mean MT4 doesn't handle arrays properly? How so? What should I look out for?
 
gatornuke:
What do you mean MT4 doesn't handle arrays properly? How so? What should I look out for?
The resize is fine. See ArrayResize
 
kennyhubbard:

You are correct, ArrayResize will resize the first dimension. But in your case, the problem lies with the second dimension which only has 1 element and you reference it with FrequencyArray[xyz_variable][0]..............FrequencyArray[xyz_variable][1] does not exist.


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.

 
WHRoeder:

If it's not found, you reset the size back to one instead of increasing the array size.

You're comparing to [x][i] instead of [x][1]

Also, think about whither you should be comparing two doubles with == versus approximately equal.

Simplified code:


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.