Skip to content

Instantly share code, notes, and snippets.

@lepoetemaudit
Created July 10, 2018 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepoetemaudit/a2b771d572ebcfcdfdd55cda198bfc30 to your computer and use it in GitHub Desktop.
Save lepoetemaudit/a2b771d572ebcfcdfdd55cda198bfc30 to your computer and use it in GitHub Desktop.
C version of recursive sum (with sentinel)
#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