Last active
January 17, 2017 12:24
-
-
Save olegon/abd83399fa2bed576a2de27f7e06a03f to your computer and use it in GitHub Desktop.
Exercício 2.7 (https://www.ime.usp.br/~pf/algoritmos/aulas/recu.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int sum_positives (int *v, int n); | |
int main (void) { | |
int v[5] = { -1, 7, -3, 11, 4 }; | |
int result = sum_positives(v, 5); | |
printf("Sum: %d\n", result); | |
return 0; | |
} | |
/* | |
Dado um vetor de inteiros v e um inteiro n >= 0 | |
devolve a soma dos inteiros positivos no intervalo v[0..n-1] | |
*/ | |
int sum_positives (int *v, int n) { | |
if (n == 0) return 0; | |
else { | |
int total = sum_positives(v, n - 1); | |
/* Invariante: total possui a soma dos inteiros positivos do vetor v[0..n-2] */ | |
if (v[n - 1] > 0) return total + v[n - 1]; | |
else return total; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment