Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/counting.c
Last active Jun 13, 2016

Embed
What would you like to do?
Recursive Counting Function
#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;
}
@CraigRodrigues

This comment has been minimized.

Copy link
Owner Author

commented Jun 13, 2016

First time using repl.it

@CraigRodrigues

This comment has been minimized.

Copy link
Owner Author

commented Jun 13, 2016

No real use of the array though. It just uses the count. Hmmm

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.