1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
//Michael Ervin - CS3 - Assignment 2 - Recursive occurence counter - 8/24/2011 - 8:50pm
#include <iostream>
int occurences(int A[], int l, int t)
{
if(l >= 0)
if(A[l] == t)
return 1 + occurences(A, l - 1, t);
else
return 0 + occurences(A, l - 1, t);
return 0;
}
int main()
{
int A[10] = {2, 5, 2, 6, 7, 8, 12, 2, 2, 0};
std::cout << "The number 2 occurs " << occurences(A, 9, 2) << " times";
std::cin.get();
return 0;
}
|