# Thread: i solved this recursion, is it correct?

1. ## i solved this recursion, is it correct?

Write a recursive function that searches an array of integers of size N and returns the number of integers equal to a target value

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

2. Hey, your recursive call coupled with if and increment is serving as loop only!

3. Originally Posted by technam
Hey, your recursive call coupled with if and increment is serving as loop only!
yea i know what i meant is that i'm not using any for/while/do while loops inside the function to get the desired result

4. You might want to name your function count rather than search. I do not understand why you have the count parameter. It seems to me that you just need one parameter for the pointer to the first element of the array, another parameter for the size of the array (or subarray), and yet another parameter for the target value. You would then make full use of the return value.

I note that your indentation can be improved, but you did give it a try and posted in code tags

5. Originally Posted by laserlight
You might want to name your function count rather than search. I do not understand why you have the count parameter. It seems to me that you just need one parameter for the pointer to the first element of the array, another parameter for the size of the array (or subarray), and yet another parameter for the target value. You would then make full use of the return value.

I note that your indentation can be improved, but you did give it a try and posted in code tags

but how can i do this without using the variable count? I used it to count the number of times the target appears in the specified array

6. Originally Posted by jackhasf
but how can i do this without using the variable count? I used it to count the number of times the target appears in the specified array
Why not return the count of the number of times the target appears in the specified array?

7. Originally Posted by laserlight
Why not return the count of the number of times the target appears in the specified array?
i think i return it

i have return count[0];

do you mean anything else?

8. Originally Posted by jackhasf
i think i return it

i have return count[0];
That is true, but not all control paths return a value (what does your function return if the size is not equal to -1?). Basically, you are using the count parameter as an out parameter (or more precisely, as an in/out parameter). But what I am saying is that you do not need it at all.