3
I’m trying to learn recursion and I’m doubtful on a question I was doing in c, I need to add up how many numbers of a vector with n elements are larger than a number x. This recursion just doesn’t get into my head, I need a clear.
#include <stdio.h>
#include <stdlib.h>
int busca(int v[], int n, int k)
{
int x = 0;
if(n == 1)
{
if( k < v[0])
x= x + 1;
}
else
{
if(k < v[n])
x = x+ 1;
busca( v, n-1 ,k);
}
return x;
}
int main()
{
int n, k,x;
printf("DIGITE A QUANTIDADE DE ELEMENTOS DO VETOR:");
scanf("%d", &n);
int v[n];
int i;
for(i = 1; i <= n; i++)
{
printf("Digite o elemento %d: ", i);
scanf("%d", &v[i]);
}
printf("Digite um numero: ");
scanf("%d", &k);
//printf ("%d",
printf("x = %d", busca(v, n, k));
return 0;
}