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?
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.
later with another simple issue that you would not have if you took the time to learn the basics of the language.
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.
- www.mql5.com
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
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi people.
I am trying to create a two dimensional array and then print the sorted values. What is wrong on my code?