Array or not to array - page 3

 
WHRoeder:
  1. GumRai: but after reading a recent post by WHRoeder, it seems that this could be simplified to
    void RemoveElement(int x,int as,Trades &array[]) //x =element(trade) to be removed, as=array size
      {
       if(x==as-1)                
         {                        
          ArrayResize(array,as-1);
          return;                 
         }                        
       for(;x<as-1;x++)
         {
          array[x]=array[x+1];
         }
       ArrayResize(array,as-1);
       return;
      }
    A) You don't need the first if, the for loop would do nothing in that case. B) I wouldn't even pass as, the function can get it internally.
  2. Array[x] = array[x+1] will work if the struct does not have any strings or dynamic arrays.

Thank you for the info WHRoeder

I need the if because if the element to be removed is the final element, it's just a matter of resizing the array to remove it. If I didn't do that, the loop would give an array out of range error for [x+1]

Unfortunately I have been simply unable to get my head around classes, so have to stick to basic mql4.

 
GumRai:

Thank you for the info WHRoeder

I need the if because if the element to be removed is the final element, it's just a matter of resizing the array to remove it. If I didn't do that, the loop would give an array out of range error for [x+1]

Unfortunately I have been simply unable to get my head around classes, so have to stick to basic mql4.

If x=as-1 the loop is not executed.
 
GumRai: If I didn't do that, the loop would give an array out of range error for [x+1]
if x is the last element, then x == as - 1 so the for statement is
for(;x<as-1;x++)

for(;as-1 < as-1; x++)

for(;false; x++)
The array[x+1] is never executed because the loop does nothing.
 

Yes, you are correct, I confuse myself sometimes.

I should have said that when x==as-1, it has to be dealt with individually as [x+1] doesn't exist

 
GumRai: I should have said that when x==as-1, it has to be dealt with individually as [x+1] doesn't exist
It doesn't have to be "dealt with individually".
The for loop moves elements down. The end condition is such that is doesn't move beyond the last element. If it did, the for loop is wrong and the code breaks.
 
WHRoeder:
GumRai: I should have said that when x==as-1, it has to be dealt with individually as [x+1] doesn't exist
It doesn't have to be "dealt with individually".
The for loop moves elements down. The end condition is such that is doesn't move beyond the last element. If it did, the for loop is wrong and the code breaks.

I'm not following you. The last element cannot be removed by the loop. How else can I remove it if it is not dealt with individually?
 
GumRai: I'm not following you.
The last element cannot be removed by the loop. How else can I remove it if it is not dealt with individually?
Apparently.
The loop moves elements down. Period.
The resize removes that last element. What more is necessary?
 

Ah, I get you now.

Sorry, sometimes my brain dies :)

 
I would agree on what the other members here. An array will definitely be necessary. It is like dealing with references.
Reason: