Last active
June 13, 2016 13:21
-
-
Save CraigRodrigues/14cedd4b6e294b535a7e75bed88b1349 to your computer and use it in GitHub Desktop.
Recursive Counting 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
#include "stdio.h" | |
//n is the last index of the array | |
int arr_count(int count, int n) | |
{ | |
//base case | |
if (n == 0) | |
{ | |
return count; | |
} | |
return (count + arr_count(count,n-1)); | |
} | |
int main(void) { | |
// Disable stdout buffering | |
setvbuf(stdout, NULL, _IONBF, 0); | |
int arr[] = {1,2,3,4,5,6,7,8,9}; | |
int count = 1; | |
count = arr_count(count, 8); | |
printf("\ncount is: %d\n", count); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No real use of the array though. It just uses the count. Hmmm