Created
June 13, 2016 12:48
-
-
Save CraigRodrigues/91ce82bb53852c0c62a614639abf7b2c to your computer and use it in GitHub Desktop.
Recursive Sum Function
This file contains hidden or 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
//n is the last index of the array | |
int arr_sum(int arr[], int n ) | |
{ | |
//base case | |
if (n == 0) | |
{ | |
return arr[0]; | |
} | |
return (arr[n] + arr_sum(arr,n-1)); | |
} | |
int main(void) | |
{ | |
int arr[] = {1,2,3,4,5}; | |
int sum; | |
sum = arr_sum(arr,4); | |
printf("\nsum is:%d\n",sum); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Grokking Algorithms exercise 4.1 in C.