Creating and printing a two dimensions Array

 

Hi people.

I am trying to create a two dimensional array and then print the sorted values. What is wrong on my code?

void OnTick()
  {
   int as=0;
   double ordersArray[][2];
   ArrayResize(ordersArray,as);
   for(int x=OrdersTotal()+1; x>=-1; x--)
     {
      if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
        {
         as++;
         ArrayResize(ordersArray,as);
         ordersArray[as-1][0]=(double)OrderLots();
         ordersArray[as-1][1]=(int)OrderTicket();
 /// Print(ordersArray[as-1][0], " ", ordersArray[as-1][1] );
         
        }
     }
   ArraySort(ordersArray,WHOLE_ARRAY,0,MODE_DESCEND);


//--- print sorted array
string str;
   for(int i=0; i<OrdersTotal(); i++)
     {


         str=DoubleToString(ordersArray[i-1][0]);

      Print(str);
     }
  
 Print (" END " );
//+------------------------------------------------------------------+
 
Ioannis Christopoulos:

Hi people.

I am trying to create a two dimensional array and then print the sorted values. What is wrong on my code?

//--- print sorted array
string str;
   for(int i=0; i<OrdersTotal(); i++)
     {


         str=DoubleToString(ordersArray[i-1][0]);

      Print(str);
     }

Are you possibly getting an out of range error here? I suppose there is no ordersArray[-1][0]. Think about which field of the array you want to access through index i=0.

 

I tried a different approach because the for loop confuses me..

so... i edit the code with a while loop....

//+------------------------------------------------------------------+
void OnTick()
  {
   Print (" UNSORTED " );
   int x=0;
   double ordersArray[][2];
      ArrayResize(ordersArray,x+1);

   while(x<OrdersTotal()+1)
     {
     
      if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
        {
         
         ArrayResize(ordersArray,x+1);
        
         ordersArray[x][0]=(double)OrderLots();
         ordersArray[x][1]=(int)OrderTicket();
  Print(DoubleToString(ordersArray[x][0]), " ", (ordersArray[x][1]) );
         
        }
        x=x+1;
     }
   ArraySort(ordersArray,WHOLE_ARRAY,0,MODE_DESCEND);
 Print (" SORTED " );
//--- print sorted array

x=0;
   while(x<OrdersTotal()+1)
     {
      x=x+1;
        Print(DoubleToString(ordersArray[x][0]), " ", (ordersArray[x][1]) );

    
     }
  
 Print (" END " );
//+------------------------------------------------------------------+

the result on journal is 


 2019.07.02 00:36:39   UNSORTED 

 2019.07.02 00:36:39  0.40000000 1

 2019.07.02 00:36:39  0.20000000 2

 2019.07.02 00:36:39  0.30000000 3

 2019.07.02 00:36:39  0.50000000 4

 2019.07.02 00:36:39   SORTED 

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39   END 


Why cant i print the sorted values? what am i missing here?

 
Ioannis Christopoulos #:

I tried a different approach because the for loop confuses me..

so... i edit the code with a while loop....

the result on journal is 


 2019.07.02 00:36:39   UNSORTED 

 2019.07.02 00:36:39  0.40000000 1

 2019.07.02 00:36:39  0.20000000 2

 2019.07.02 00:36:39  0.30000000 3

 2019.07.02 00:36:39  0.50000000 4

 2019.07.02 00:36:39   SORTED 

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39   END 


Why cant i print the sorted values? what am i missing here?

There are some tutorials/articles on the basics of the language, go read them, also read the docs, don't try anything complex until you got
the basics, otherwise you'll keep writing "random" code where you are not even properly formatting your code.

 
Alexandre Borela #:

There are some tutorials/articles on the basics of the language, go read them, also read the docs, don't try anything complex until you got
the basics, otherwise you'll keep writing "random" code where you are not even properly formatting your code.

Very helpful

 
Ioannis Christopoulos #:

Very helpful

You still don't know the basics of the language, you don't even properly format the code, if I fix your code, you'll just come back
later with another simple issue that you would not have if you took the time to learn the basics of the language.
 
Ioannis Christopoulos #:

I tried a different approach because the for loop confuses me..

so... i edit the code with a while loop....

the result on journal is 


 2019.07.02 00:36:39   UNSORTED 

 2019.07.02 00:36:39  0.40000000 1

 2019.07.02 00:36:39  0.20000000 2

 2019.07.02 00:36:39  0.30000000 3

 2019.07.02 00:36:39  0.50000000 4

 2019.07.02 00:36:39   SORTED 

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39  0.00000000 0

 2019.07.02 00:36:39   END 


Why cant i print the sorted values? what am i missing here?

Your procedure of testing the sorting function with a series of unique numbers is pretty much what I was going to suggest next.

I am not sure if this is a part of the problem: an array can only have one type of variables. In the beginning you define it as double, but later you store int numbers in it. Which should not be a problem, except the other way around if you store double in int type variables you are going to truncate the value. So I am unsure why it shouldn't show the numbers correctly. Except you could put (double) in front of the ticket numbers.

I highly recommend making the debugger your own by reading the second section in this article: "2.Debugger" https://www.mql5.com/en/articles/654

The debugger is NOT the same as the tester. You have to set stop points prior to starting the debugger and then mark and right click variables to put them under watch. Then you can go through all the stop points and watch how the variables change. This is the easiest and most effective way to find the root of such problems.

And if I may put  out the helpful part in Alexandre's post: Read the docu on the topics ArraySort and Multidimensional Arrays thoroughly.

Debugging MQL5 Programs
Debugging MQL5 Programs
  • www.mql5.com
This article is intended primarily for the programmers who have already learned the language but have not fully mastered the program development yet. It reveals some debugging techniques and presents a combined experience of the author and many other programmers.
 
pennyhunter #:

I highly recommend making the debugger your own by reading the second section in this article: "2.Debugger" https://www.mql5.com/en/articles/654


Thank you for your suggestion. It will help me a lot