Can anyone tell me how to calculate frequency of each array elements in c? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone tell me how to calculate frequency of each array elements in c?

eg 12342628569 :- here frequency of 2 is 3

29th Sep 2017, 2:02 AM
Manisha
Manisha - avatar
8 Answers
+ 1
Frequency of 2 is not 3, it is 3/ size of array == 3/11 double freq(int * array, unsigned size, int value){ double res = 0.; if(array && size) for(unsigned i = 0; i < size; ++i) if(array[i] == value) res += 1; return size?res/size:res; } if you wanted to know the number of time the value appears in the array, change double to unsigned and change the return line by : return res;
29th Sep 2017, 5:01 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
thanks for your attention
29th Sep 2017, 11:40 AM
Manisha
Manisha - avatar
0
but you you have misunderstood the question
29th Sep 2017, 11:43 AM
Manisha
Manisha - avatar
0
In which way ?
29th Sep 2017, 11:54 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
the question is we have to count how many times no appears in array
29th Sep 2017, 12:00 PM
Manisha
Manisha - avatar
0
yes, I also answered this just in case after the code ^^
29th Sep 2017, 12:56 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
is this code is for every no in array?
29th Sep 2017, 1:27 PM
Manisha
Manisha - avatar
0
Yes, just have to change the third parameter to calculate for another number
29th Sep 2017, 2:36 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar