6
I’m learning recursion and have doubts about using global variables, particularly I think a gambit inelegant, maybe I’m wrong. I made a code to add positive numbers and used a variable called sum. I would like to know if there are other outputs for this question. Code below:
#include<stdio.h>
int soma = 0;
int SomaPositivos(int vet[], int n) {
if (n == 0) {
return 0;
} else {
int aux;
if (vet[n-1] > 0) {
aux = vet[n-1];
soma = soma + aux;
SomaPositivos(vet, n-1);
}
}
return soma;
}
int main () {
int v[20] = {2, 1, 8, 3, 4};
int a;
a = SomaPositivos(v, 5);
printf("%d ", a);
return 0;
}