# Counting Occurrences in array

To add comments, please log in or register
17

Hi,

I have  simple array with strings (about 800 entries).

But now I want to determinate frequency of each different  string element. Idea is to take first element of array and then run over all array and count how many times it shows up there.

So my calculation is that the loop should repeat 800x800=640 000 times

```  for(int x=0;x<=800;x++){

string compare = canc_res_2[x][0];
int comp_2 =0;
for(int xx=0;xx<=800;xx++){
Print(compare,"  vs ",canc_res_2[xx][0]);
if(compare==canc_res_2[xx][0]){
comp_2=comp_2+1;

}
}

double procent = (100/800)*comp_2;

Print(compare,"  repeats  ",comp_2, " times. With percentage of ", NormalizeDouble(procent, 10), "%");
}```

My output :

.....

GBP;JPY;USD;CHF;EUR;EUR;CHF;USD;JPY;GBP  vs GBP;JPY;USD;CHF;EUR;EUR;CHF;USD;JPY;GBP

GBP;JPY;USD;CHF;EUR;EUR;CHF;USD;JPY;GBP  repeats  7 times. With percentage of 0.0%

Q1. I don't understand why the percentage is 0.0 % if correct on should be 0.875 %?

Q2. Why loop repeats just one time and with first  array's element, why is that ? My code looks good.

Q3. Is this a good practice to do such easy job to get frequency of single string?

Thanks a lot :)

379

Q1. I don't understand why the percentage is 0.0 % if correct on should be 0.875 %?

Integer arithmetic is cast to a double type.  Make one operand a double type or multiply by 1.0.  See Typecasting.

Before operations (except for the assignment ones) are performed, the data are converted into the maximum priority type. Before assignment operations are performed, the data are cast into the target type.

Examples:

```int    i=1/2;        // no types casting, the result is 0
Print("i = 1/2  ",i);

int k=1/2.0;         // the expression is cast to the double type,
Print("k = 1/2  ",k);  // then is to the target type of int, the result is 0

double d=1.0/2.0;    // no types casting, the result is 0.5
Print("d = 1/2.0; ",d);

double e=1/2.0;      // the expression is cast to the double type,
Print("e = 1/2.0; ",e);// that is the same as the target type, the result is 0.5

double x=1/2;        // the expression of the int type is cast to the double target type,
Print("x = 1/2; ",x);  // the result is 0.0```

Q2 and Q3 depend on how your simple string array is structured and the contents.  Is the array one dimension or is it a two dimensional array?  Does the array have currency symbols (as in your original post) or something else?  Please give a small example.

35

There's no need to start your second loop at xx=0 (you've already checked that element when x was 0) so start it at xx=x+1.
To add comments, please log in or register