Pass array name to function?

Post Reply
Radar
Trader
Posts: 437
Joined: Fri Mar 23, 2012 5:39 pm
Location: Round the bend ;)

Pass array name to function?

Post by Radar »

Hey gang,

I have 3 functions to backup, restore, and trim arrays, but at the moment, they all only work with a single array.

Here's my TrimArray function...

Code: Select all

void TrimArray()
{
   for(int i=(ArrayRange(MyTf,0)-1); i>=0; i--)
   {
      EmptyElements=MyTf[i,0];
      if(EmptyElements==0) a++;
   }
   ArraySort(MyTf,WHOLE_ARRAY,0,MODE_DESCEND);
   int CurrentArrayRange=ArrayRange(MyTf,0);
   Alert("Array Size is now ", CurrentArrayRange);
   int NewArrayRange=CurrentArrayRange-a;
   ArrayResize(MyTf, NewArrayRange);
   CurrentArrayRange=ArrayRange(MyTf,0);
   Alert("Array Size is now ", CurrentArrayRange);
   ArraySort(MyTf);
} // End TrimArray
I only learnt how to do the basics with arrays 2 nights ago, so I've been throwing alerts as often as possible, so that I can see where I stuff up ;)

I've tried changing the function to accept the array name as a string, (void TrimArray(string ArrayToTrim)), but execution never gets past the for(int i=(ArrayRange(ArrayToTrim,0)-1); i>=0; i--) line of code :(

So, as the title says, is there any way to pass the array name to the function, and replace the instances of MyTf with variable names so that one set of code suits all, or do I have to repeat the code for each array that I want to trim, (or backup/restore, as the case may be)?

I'm most definitely at the limit of my knowledge with this, and really need an example of how to do it, if it's possible to do at all.

Thanks in advance :)

Have fun!

Radar =8^)
Check out my new, (well, old now), manual trade & automatic scale-in manager,
StackManV2
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Pass array name to function?

Post by dietcoke »

Radar wrote:Hey gang,

I have 3 functions to backup, restore, and trim arrays, but at the moment, they all only work with a single array.

I only learnt how to do the basics with arrays 2 nights ago, so I've been throwing alerts as often as possible, so that I can see where I stuff up ;)

I've tried changing the function to accept the array name as a string, (void TrimArray(string ArrayToTrim)), but execution never gets past the for(int i=(ArrayRange(ArrayToTrim,0)-1); i>=0; i--) line of code :(

So, as the title says, is there any way to pass the array name to the function, and replace the instances of MyTf with variable names so that one set of code suits all, or do I have to repeat the code for each array that I want to trim, (or backup/restore, as the case may be)?

I'm most definitely at the limit of my knowledge with this, and really need an example of how to do it, if it's possible to do at all.

Thanks in advance :)

Have fun!

Radar =8^)
You cannot pass an array as a parameter to a function. What you have to do is pass a reference to the array instead. Decent explanation here: http://en.fortrader.ru/mql/nedokumentir ... -mql4.html

Any way, the syntax is thus:

Code: Select all

// where MyArray is a reference to an integer array
void TrimArray(int& MyArray) 
{
   for(int i=(ArrayRange(MyArray,0)-1); i>=0; i--)
   {
      EmptyElements=MyArray[i,0];
      if(EmptyElements==0) a++;
   }
   ArraySort(MyArray,WHOLE_ARRAY,0,MODE_DESCEND);
   int CurrentArrayRange=ArrayRange(MyArray,0);
   Alert("Array Size is now ", CurrentArrayRange);
   int NewArrayRange=CurrentArrayRange-a;
   ArrayResize(MyArray, NewArrayRange);
   CurrentArrayRange=ArrayRange(MyArray,0);
   Alert("Array Size is now ", CurrentArrayRange);
   ArraySort(MyArray);
} // End TrimArray
You call it like this

TrimArray(MyArrayToTrim);

where MyArrayToSort is the array you want to trim.
Image
Radar
Trader
Posts: 437
Joined: Fri Mar 23, 2012 5:39 pm
Location: Round the bend ;)

Re: Pass array name to function?

Post by Radar »

Hey Dietcoke,

Thanks for that!! :)

It took me 5 hours to just get the basics into my head, and another 12 hours to get from there to here.

I'm off to put this into practise until it sticks :D

Have fun!

Radar =8^)
Check out my new, (well, old now), manual trade & automatic scale-in manager,
StackManV2
Post Reply

Return to “Coders Hangout”