Array or not to array

Back to topics list  | 1 2 3 To post a new topic, please log in or register
avatar
Moderator
3418
GumRai 2016.04.17 14:43 #
 
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.

avatar
Moderator
3206
angevoyageur 2016.04.17 19:45 #
 
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.
avatar
13004
WHRoeder 2016.04.17 20:56 #
 
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.

avatar
Moderator
3418
GumRai 2016.04.17 23:24 #
 

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

avatar
13004
WHRoeder 2016.04.18 03:33 #
 
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.

avatar
Moderator
3418
GumRai 2016.04.18 03:38 #
 
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?
avatar
13004
WHRoeder 2016.04.18 03:43 #
 
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?
avatar
Moderator
3418
GumRai 2016.04.18 04:02 #
 

Ah, I get you now.

Sorry, sometimes my brain dies :)

avatar
2
surf_droopy 2016.04.27 08:03 #
 
I would agree on what the other members here. An array will definitely be necessary. It is like dealing with references.
Back to topics list   | 1 2 3  

To add comments, please log in or register