Good afternoon!
I am struggling to split an Array (A) of Size e.g. 10 into two other Arrays (B and C). Array B should contain all even elements of A and C, apparently, all odd elements of A.
Example:
A = [5, 4, 7, 2, 9, 6, 8, 11, 25, 17} ->
B = [5, 7, 9, 8, 25]
C = [4, 2, 6, 11, 17]
Best!
Good afternoon!
I am struggling to split an Array (A) of Size e.g. 10 into two other Arrays (B and C). Array B should contain all even elements of A and C, apparently, all odd elements of A.
Example:
A = [5, 4, 7, 2, 9, 6, 8, 11, 25, 17} ->
B = [5, 7, 9, 8, 25]
C = [4, 2, 6, 11, 17]
Best!
You can use this code :
int A[10] = {5, 4, 7, 2, 9, 6, 8, 11, 25, 17}; int B[5]; int C[5]; int indexB = 0; int indexC = 0; for ( int i = 0 ; i < 10 ; i++ ) { if ( MathMod(i,2) == 0 ) { Print( i, " B"); B[indexB] = A[i]; indexB++; } else { Print( i, " C"); C[indexC] = A[i]; indexC++; } }
You can use this code :
Thank you very much Biantoro Kunarto
Works very well! :-)
Thank you very much Biantoro Kunarto
Works very well! :-)
Here's an alternative method for using a simple dynamic array-list so you won't have to mess with the array sizing, and you can also sort...
#include <Arrays\ArrayInt.mqh> class SplitArray : public CArrayInt { public: void AssignEven(const CArrayInt &arr) { Clear(); for(int i=0;i<arr.Total();i++) if(arr[i]%2==0) Add(arr[i]); } void AssignOdd(const CArrayInt &arr) { Clear(); for(int i=0;i<arr.Total();i++) if(arr[i]%2!=0) Add(arr[i]); } void ToLog() { for(int i=0;i<Total();i++) Print(this[i]); } }; void OnStart() { SplitArray all,even,odd; srand(_RandomSeed); for(int i=0;i<50;i++) all.Add(rand()); even.AssignEven(all); even.Sort(); odd.AssignOdd(all); odd.Sort(); even.ToLog(); odd.ToLog(); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Good afternoon!
I am struggling to split an Array (A) of Size e.g. 10 into two other Arrays (B and C). Array B should contain all even elements of A and C, apparently, all odd elements of A.
Example:
A = [5, 4, 7, 2, 9, 6, 8, 11, 25, 17} ->
B = [5, 7, 9, 8, 25]
C = [4, 2, 6, 11, 17]
Best!