Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Created June 13, 2016 12:48
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 CraigRodrigues/91ce82bb53852c0c62a614639abf7b2c to your computer and use it in GitHub Desktop.
Save CraigRodrigues/91ce82bb53852c0c62a614639abf7b2c to your computer and use it in GitHub Desktop.
Recursive Sum Function
//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;
}
@CraigRodrigues
Copy link
Author

Grokking Algorithms exercise 4.1 in C.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment