[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 370

 

Good afternoon!

Can you give me a hint, please.....

Indic_mas[]//main array

Indic_mas_copi[]//array to which the main array is copied.

n //number of elements in Indic_mas_copi[]

1. does the entry Indic_mas_copi[n]=0 mean that we have zeroed all elements of the array?

2. After performing certain operations, the array Indic_mas_copi[] has changed the values of some elements,

The next iteration repeats copying Indic_mas[] into Indic_mas_copi[].

Does Indic_mas_copi[] need to be zeroed (or empty) before this copying

or will Indic_mas[] elements automatically replace the previous Indic_mas_copi[] elements ?

 
LOA:

Good afternoon!

Can you give me a hint, please.....

Indic_mas[]//main array

Indic_mas_copi[]//array to which the main array is copied.

n //number of elements in Indic_mas_copi[]

1. does the entry Indic_mas_copi[n]=0 mean that we have zeroed all elements of the array?

2. After performing certain operations, the array Indic_mas_copi[] has changed the values of some elements,

The next iteration repeats copying Indic_mas[] into Indic_mas_copi[].

Does Indic_mas_copi[] need to be zeroed (or empty) before this copying

or will Indic_mas[] elements automatically replace previous Indic_mas_copi[] elements ?


1. If n is the number of elements in Indic_mas_copi[], then writing Indic_mas_copi[n] is not correct because you are going outside the array in this entry. Do not forget that numbering of array cells starts from zero cells. Therefore, the number of the last cell of the array you can address is n-1

2. will be replaced. Check this with the script.

 
drknn:


1. If n is the number of elements in Indic_mas_copi[], then writing Indic_mas_copi[n] is not correct because you are going outside the array in this entry. Do not forget that numbering of array cells starts from zero cells. That's why the number of the last array cell to be addressed is n-1.

2. there will be substitutions. Check it with the script.


Thank you very much, Vladimir!

Then to zero the elements of the array you need the following?

double i;
for (i=n-1;i>=0;i--)Indic_mas_copi[i]=0; 
 
LOA:


Thank you very much, Vladimir!

Then to zero the elements of the array you need the following?


No - it's too complicated. There is a simpler way.

int ArrayInitialize( double &array[], double value)
Sets all elements of a numeric array to the same value. Returns the number of initialized elements.

Specify the name of the array in brackets and put zero as the second parameter. This will cause all elements of the array to be zeroed.

 
LOA:


Thank you very much, Vladimir!

Then to zero the elements of the array you need the following?

ArrayInitialize(Array_Name,0);
 
О... Vladimir beat me to it... :)
 
artmedia70:
ArrayInitialize(Array_Name,0);

This is better: https://docs.mql4.com/ru/array/ArrayInitialize - there is a concrete code example
 
drknn:


No - this is too complicated. There is a simpler way.

int ArrayInitialize( double &array[], double value)
Sets all elements of a numeric array to the same value. Returns the number of initialized elements.

Specify array name in brackets, and put zero as the second parameter. As a result, all elements of the array will be zeroed.


Thank you. It really is easier that way.

Then the loop can be used in a situation where we need to zero some of the elements?

Or, say, through a single element.

 
LOA:

Good afternoon!

Can you give me a hint, please.....

Indic_mas[]//main array

Indic_mas_copi[]//array to which the main array is copied.

n //number of elements in Indic_mas_copi[]

1. does the entry Indic_mas_copi[n]=0 mean that we have zeroed all elements of the array?

2. After performing certain operations, the array Indic_mas_copi[] has changed the values of some elements,

The next iteration repeats copying Indic_mas[] into Indic_mas_copi[].

Does Indic_mas_copi[] need to be zeroed (or empty) before this copying

or will Indic_mas[] elements automatically replace the previous Indic_mas_copi[] elements ?

No, you don't have to. All elements of Indic_mas_copi[] will be replaced by those copied from the main Indic_mas[] array
 
LOA:


Thank you. It's really simpler that way.

Then the loop can be used in a situation where you need to zero out some of the elements?

Or, say, through one element.


A loop can be used when you want to zero or reinitialise some elements... A loop, on the other hand, goes through them all one by one. Therefore, inside the loop we can introduce a rule that will sort the required cells of the array. For example, we only need to sort out negative numbers and double them. Therefore we will write the following in the loop

If the value in the current cell of the array is less than zero, then it takes the value of that cell of the array multiplied by two. Otherwise, continue

P.S.

if(massiv[i]<0){
  massiv[i]=massiv[i]*2;
}
else{
  continue;
}
Although from the processor's point of view, if we know ahead of time exactly what we need to double, it's better to apply addition than multiplication - addition will take less CPU time.