Write a recursive function that searches an array of integers of size N and returns the number of integers equal to a target value
so all i did was this:
Code:
#include <stdio.h>
#include <stdlib.h>
int search(int n,int x[],int size,int count[0]) {
if (size == -1) {
return count[0];
}
else {
if (x[size] == n){
count[0]++;
}
search(n,x,size-1,count);
}
}
int main(void){
int count=0;
int x[10]= {1,2,3,2,9,3,10,2,2,2};
search(2,x,sizeof(x)/sizeof(int) - 1,&count);
printf("%d\n",count);
system("pause");
return 0;
}
it gets me the correct result for different values, but am I correct? I'm not using any loops to get my result, just an if statement to increase the count
Thanks in advance