Count items in array with specified value (MLQ4)

 

How can I count the number of items in an array that have the value of "USDCHF"?

 

int start() {

   string xyz[99];
   xyz[0] = "USDCAD";
   xyz[1] = "USDCHF";
   xyz[2] = "EURUSD";
   xyz[3] = "USDJPY";
   xyz[4] = "AUDUSD";
   xyz[5] = "USDCHF";
   xyz[6] = "USDCHF";
   xyz[7] = "USDJPY";
   xyz[9] = "AUDUSD";

   Alert( countXYZ("USDCHF") );
   
   return(0);
  
}

void countXYZ (string x) {
 return(3);
}

 

 
hknight:

How can I count the number of items in an array that have the value of "USDCHF"?

How would you do it manually with a piece of paper and a pencil ?
 
hknight: How can I count the number of items in an array that have the value of "USDCHF"?
  1.    string xyz[99];
       xyz[0] = "USDCAD";
       xyz[1] = "USDCHF";
       xyz[2] = "EURUSD";
       xyz[3] = "USDJPY";
       xyz[4] = "AUDUSD";
       xyz[5] = "USDCHF";
       xyz[6] = "USDCHF";
       xyz[7] = "USDJPY";
       xyz[9] = "AUDUSD";
    
    You can NOT access uninitialized strings. xyz[8] and xyz[10..]

  2. If these are constant, you can initialze them with
       string xyz[]={ "USDCAD", "USDCHF", "EURUSD", "USDJPY",
                      "AUDUSD", "USDCHF", "USDCHF", "USDJPY",
                      "AUDUSD"};

  3. Pass the array and
    return a value

       Alert( countXYZ("USDCHF", xyz, 9) );
       return(0);
    }
    
    int countXYZ (string x, string xyz[], int n){
      int count=0;
      for(int i=n-1; i>=0; i--) if (xyz[i]==x) count++;
      return(count);
    }
    What are Function return values ? How do I use them ? - MQL4 forum
    or
       Alert( countXYZ("USDCHF", xyz) );
       return(0);
    }
    
    int countXYZ (string x, string xyz[], n=WHOLE_ARRAY){
      if (n==WHOLE_ARRAY) n = ArraySize(xyz);
      int count=0;
      for(int i=n-1; i>=0; i--) if (xyz[i]==x) count++;
      return(count);
    }