(Last Revision -- 03/12/00)
A Program that Illustrates Misuse of Recursion
/*
A program that calculates N choose K using a very inefficient
recursive method.
*/
#include <iostream.h>
int NchooseK(int N, int K);
int main ()
{
int N , K , ans;
cout << endl ;
cout << "Give values for N and K, and this program will output" << endl ;
cout << "the value of N choose K: " ;
cin >> N >> K ;
cout << endl ;
ans = NchooseK(N,K) ;
cout << endl ;
cout << "The answer is: " << ans << "." << endl ;
return 0;
}
int NchooseK(int N, int K)
{
cout << "Now computing " << N << " choose " << K << "." << endl ;
if ( (N==K) || (0==K) ) return 1;
else return ( NchooseK(N-1, K-1) + NchooseK(N-1,K) ) ;
}