Created
July 10, 2018 09:34
-
-
Save lepoetemaudit/a2b771d572ebcfcdfdd55cda198bfc30 to your computer and use it in GitHub Desktop.
C version of recursive sum (with sentinel)
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(int *numbers) { | |
if (numbers[0] == -1) { | |
return 0; | |
} else { | |
return numbers[0] + sum(numbers + 1); | |
} | |
} | |
int main() { | |
int numbers[] = {1, 2, 3, 4, 5, 6, -1}; | |
printf("Result: %d\n", sum(numbers)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment