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
size_tsuggests that a library function should be used.size_tshould always be used when returning lengths of things (including the number of elements meeting some criteria in an arbitrarily sized array).strcspngoing to help you with anintarray? Just because ofsize_t, you went poking around in the header files?size_tis an integral type in C which can't be negative: that's why your professor suggestedsize_tinstead ofint. Your count can only be >= 0.