Hmm... first, never assign an integer value to a double. You can make that a[i] = i*1.0 ; would be better for the compile.
Second, you should refrain from having formal parameters (double& a[]) with the same name as public variables (double a[10]), because the compiler would be confused between the public variable and the formal parameter. It would choose what is instructed to choose and it won't give a damn on what you actually want.
Plus that in the second for cycle, you do not reassign the values of the array with something like a[i]=sum; after you call addElements.
In the end, I don't use printf.
I put a normal Print("a[",i,"]=",a[i]); and I got something else.
Hmm... first, never assign an integer value to a double. You can make that a[i] = i*1.0 ; would be better for the compile.
Second, you should refrain from having formal parameters (double& a[]) with the same name as public variables (double a[10]), because the compiler would be confused between the public variable and the formal parameter. It would choose what is instructed to choose and it won't give a damn on what you actually want.
Plus that in the second for cycle, you do not reassign the values of the array with something like a[i]=sum; after you call addElements.
In the end, I don't use printf.
I put a normal Print("a[",i,"]=",a[i]); and I got something else.
Phew... Good pointers... I'm getting back to the code to see if names of the arrays are different.
Very nice remark about printf
I thought about assigning 1.0*i, but with no changed results (just an usual warning at compilation).
Thank you very very much for your comment. At least now I have a track.
I will come back with results.
In my code, The array names were different altogether. When I created the test script, I made the mistake of declaring array a and formal parameter array with the same name.
I was confused about printf messages. Print function works. printf doesn't work in this case.
It seems the code works actually. But I will double check everything and post here back if something related to arrays eventually pops up.
But now another little thing appeared, not related to my problem, but still a bug (from my point of view).
This is the new code (a little changed).
double a[10]; void OnStart() { //--- for (int i=0;i<10;i++) { a[i] = i; //printf("initial state: a[%d]=%d",i,a[i]); Print("initial state: a[",i,"]=",a[i]); } double sum; for (int i=0;i<10;i++) { addElements(a,i,a[i]); //THIS DOESN'T WORK //a[i] = sum; // THIS WORKS } for (int i=0;i<10;i++) { //printf("final state: a[%d]=%d",i,a[i]); Print("final state: a[",i,"]=",a[i]); } } bool addElements(double& inputArray[], int i, double& inputSum) { int len = ArraySize(inputArray); inputSum = 5*inputArray[i]; return (true); }
why addElements(a,i,a[i]) doesn't work? Weird values appear there. I guess it's about pointers.
Am I missing something or it's a bug ?
- www.mql5.com
I get this, running this script:
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[9]=9
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[8]=8
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[7]=7
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[6]=6
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[5]=5
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[4]=4
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[3]=3
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[2]=2.000000000000028
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[1]=4.056481920730334e+031
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[0]=0
Yes, we have a bug confirm. Works perfectly with the other line:
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[9]=45
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[8]=40
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[7]=35
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[6]=30
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[5]=25
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[4]=20
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[3]=15
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[2]=10
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[1]=5
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[0]=0
Your code has one a little but fatal mistype. Try this
double a[10]; void OnStart() { //--- for (int i=0;i<10;i++) { a[i] = i; printf("initial state: a[%d]=%G",i,a[i]); } double sum; for (int i=0;i<10;i++) { addElements(a,i,sum); } for (int i=0;i<10;i++) { printf("final state: a[%d]=%G",i,a[i]); } } bool addElements(double& a[], int i, double& sum) { int len = ArraySize(a); sum = 5*a[i]; return (true); }
I get this, running this script:
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[9]=9
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[8]=8
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[7]=7
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[6]=6
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[5]=5
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[4]=4
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[3]=3
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[2]=2.000000000000028
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[1]=4.056481920730334e+031
2010.09.27 17:36:21 ifmihai (USDCAD,D1) final state: a[0]=0
Yes, we have a bug confirm. Works perfectly with the other line:
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[9]=45
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[8]=40
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[7]=35
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[6]=30
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[5]=25
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[4]=20
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[3]=15
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[2]=10
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[1]=5
2010.09.27 17:38:58 ifmihai (USDCAD,D1) final state: a[0]=0
Your code has one a little but fatal mistype. Try this
Yes, but the mistype was in printf. This doesn't cancel the fact there is something fishy about passing an array item to a procedure expecting a double as reference.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Here is some code I made (a test script):
Why all values are 0 in initial state and final state?
Even if it's something I don't get about arrays in MQL5, I really don't even know where to start to understand this situation.
Half of my code is broken because of this stuff.
Help pls?