Define a function int occurrences(int A[], int size, int key) that counts the number of times a key value occurs in an array. Write one version of this function using iteration and another using recursion.
I have already done the iterative part; i am attempted to solve the recursive part of this problem...I haven't been able to figure out a way to make it work but this is what I have:
const int size = 7;
int rOccurences(int A[], int size, int key){ //count occurrences of key
int keyCount = 0;
if (size == 0) //key does not occur in array
return keyCount = 0;
I realize after trying this it returns 7, I know why. I am just having trouble thinking about this recursively. If someone could point me in the right direction that would be awesome. This is a review question, it is not a graded assignment. However, help would still be greatly appreciated.
the number of times key occurs in A is the number of times key occurs in A[0] (which is zero or one) plus the number of times key occurs in the rest of A:
or, alternatively, it is the number of times key occurs in the last element of A, that is, A[size-1], plus the number of times key occurs in the rest of A: