I'm working on a problem in C, and I have a quick question about it. The problem is as follows: I'm given some sorted array of integers, say, a[i] = { 1, 2, 3, 3, 3 }
. Now, I am supposed to run a program that searches for a given integer, returns the location of the first occurrence and the number of occurrences of that integer in the array.
So, if I was searching for 3
then I would have the first occurrence at a[2]
and there are three occurrences of 3
. For the first, part, of finding the first occurrence, I can simply use strcspn
from the string header file. However, for the second part, is there an inbuilt function that would count the number of instances a particular integer?
I can actually do this with my "bare hands" by simply incrementing a counter variable. However, my professor gave me a hint that the return type should be size_t, suggesting some inbuilt functions could be used. Any help would be appreciated.
Thanks, Alexander