Download MetaTrader 5

using arrays names in a loop

To add comments, please log in or register
parham.trader
375
parham.trader  

Hi friends,

I'm going to create a function to put some values in 6 different arrays simultaneously. I want to know whether it is possible to use a loop to get the name of the array and put the name of the mentioned array as the function parameter and make change in it and then again by using the loop and increasing its counter, find the name of the next array name and use it as the function parameter and make the related changes in it by passing it into the function ?

currently I'm using the code as below:

void function_test(int a,double b)
{
x[a]=b;
y[a]=b;
z[a]=b;
m[a]=b;
n[a]=b;
p[a]=b;
}

I'd appreciate if anybody could help me how to write the above-mentioned code in an easier way.

Something like assigning a number to each indicator name so that I could use them in a loop to avoid using each of them separately in the above function. for example assign 1 to x and assign 2 to y and 3 to z and so on in order to use them in a loop instead of using each of their name separately.

Mladen Rakic
65725
Mladen Rakic  
parham.trader:

Hi friends,

I'm going to create a function to put some values in 6 different arrays simultaneously. I want to know whether it is possible to use a loop to get the name of the array and put the name of the mentioned array as the function parameter and make change in it and then again by using the loop and increasing its counter, find the name of the next array name and use it as the function parameter and make the related changes in it by passing it into the function ?

currently I'm using the code as below:

I'd appreciate if anybody could help me how to write the above-mentioned code in an easier way.

Make an array a function parameter

Since they are passed to functions by reference (compiler will make you aware of that if you omit the "&" in the parameter(s) declaration) then you don't need to know the name of the array - just call a function using the array that you want to modify

Carl Schreiber
7673
Carl Schreiber  

or you can write a one liner:

void function_test(int a,double b)
{
   x[a]=y[a]=z[a]=m[a]=n[a]=p[a]=b;
}
parham.trader
375
parham.trader  

thank you both for your kind responses, but my problem is with their name and the way I use them. I want an alternative way to using each of their name separately. I mean an easy way such as using loop or ...

whroeder1
16555
whroeder1  

parham.trader:

Something like assigning a number to each indicator name so that I could use them in a loop to avoid using each of them separately in the above function. for example assign 1 to x and assign 2 to y and 3 to z and so on in order to use them in a loop instead of using each of their name separately.

  1. enum Arrays = { A, b, c, x, y, Z};
    #define arrays_FIRST A
    #define arrays_LAST  Z
    #define arrays_COUNT (Z+1)
    double indexable_array[1][arrays_COUNT];
    :
    int count=0;
    for(Arrays array=A; array<arrays_COUNT; ++array) indexable_array[0][array] = count++;
    // indexable_array[1][A]==0 ... indexable_array[1][Z]==5

  2. enum Arrays = { A, b, c, x, y, Z};
    #define arrays_FIRST A
    #define arrays_LAST  Z
    #define arrays_COUNT (Z+1)
    struct indexable{
       double Arr[arrays_COUNT];
    }
    indexable indexable_array[1]
    :
    int count=0;
    for(Arrays array=A; array<arrays_COUNT; ++array) indexable_array[0].Arr[array] = count++;
    // indexable_array[1].Arr[A]==0 ... indexable_array[1][Z]==5


To add comments, please log in or register