Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/sum.c
Created Jun 13, 2016

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Jun 13, 2016

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
You can’t perform that action at this time.