Word document

advertisement
/* factorial.c
This shows how factorial can be computed, both non-recursively as
well as recursively.
Recursion is used here to compute the answer using return values.
Sample run:
[bert] ~/demos/functions> factorial
Please enter a value for factorial computation: 4
Non-recursive answer is: 24
Please enter a value for recursive factorial computation: 5
Factorial of 5 is: 120
[bert] ~/demos/functions>
*/
#include <stdio.h>
long factorial( long n)
{
if( n == 0)
return 1;
else
return ( n * factorial( n-1));
}
// recursive call
main()
{
int i, number;
int answer = 1;
//Non-recursively find factorial of a non-zero number
// that isn't *too* large
printf("Please enter a value for factorial computation: ");
scanf("%d", &number);
for (i=number; i>1; i--)
answer = answer * i;
printf("Non-recursive answer is: %d \n", answer);
// Now do it recursively
printf("Please enter a value for recursive factorial computation: ");
scanf("%d", &number);
printf("Factorial of %d is: %d\n", number, factorial( number) );
}
/* reverseDigits.c
This shows how input digits can be displayed in reverse order,
both non-recursively as well as recursively.
Recursion is used here to display the answer a piece at a time
on the way *into* the recursion. This is called "head" recursion.
Sample run:
[bert] ~/demos/functions> reverseDigits
Non-recursively print a number's digits in reverse order.
E.g. for 2468: 8642
Recursively print a number's digits in reverse order.
E.g. for 1357: 7531
*/
#include <iostream.h>
void printReverse( int number)
{
while (number > 0) {
cout<< number % 10;
number /= 10;
}
cout<< endl;
}
void recursivePrintReverse(int number)
{
cout << number % 10;
number= number / 10;
if (number)
recursivePrintReverse( number);
else
cout << '\n';
}
main()
{
cout<<"Non-recursively print a number\'s digits in reverse order.
\n";
cout<< " E.g. for 2468: ";
printReverse( 2468);
cout<< "Recursively print a number\'s digits in reverse order. \n";
cout<< " E.g. for 1357: ";
recursivePrintReverse( 1357);
}
/*
reverseText.c
Recursively reverse input text. It is not possible
to do this non-recursively for variable sized input
without storing it temporarily in some data structure
such as an array.
Recursion is used here to display the answer a piece at
a time on the way back *out* of the recursion, which is
called "tail" recursion.
Sample run:
[bert] ~/demos/functions> reverseText
Enter the text to be reversed, followed by a dollar ($) sign:This is
Cool stuff! $
!ffutslooCsisihT
[bert] ~/demos/functions>
*/
#include <iostream.h>
void reverse()
{
char c;
cin >> c;
// Note this skips white space
if (c != '$') {
reverse();
cout << c;
}
}
int main()
{
cout<< "Enter the text to be reversed, ";
cout<< "followed by a dollar ($) sign:";
reverse();
cout<< endl;
}
/*
recursionExamples.c
Illustrate various recursive functions. Functions F1, F2,
and F3 taken from Adams, et. al. "C++: An Introduction to
Computing," Prentice Hall, 1995.
*/
#include <iostream.h>
int F1(int num1, int num2)
{
if (num1 > num2)
return 0;
else if (num2 == (num1 + 1) )
return 1;
else
return F1( num1 + 1, num2 - 1) + 2;
}
double F2( double x, unsigned n)
{
if (n == 0)
return 0;
else
return x + F2( x, n-1);
}
unsigned F3( int n)
{
if (n < 0)
return F3(-n);
else if (n < 10)
return n;
else
return F3( n / 10);
}
double F4( double x, int n)
{
if (n == 0)
return 1.0;
// anchor case
else if (n >0)
return F4( x, n - 1) * x;
// inductive step
else {
cerr << "\n Negative exponent *** exiting *** \n";
return -1.0;
}
}
int main()
{
cout<<
cout<<
cout<<
cout<<
"Call sample function using F1( 1, 5)\n";
F1( 1, 5);
"\n Call sample function using F1( 8, 3)\n";
F1( 8, 3);
cout<< "\n Call sample function using F2( 2, 4)\n";
cout<< F2( 2, 4);
cout<< "\n This implemented multiplication\n";
cout<< "\n Call sample function using F3( 256)\n";
cout<< F3( 256);
cout<< "\n This gives first digit of input\n";
cout<< "\n Call sample function using F4( 2, 5)\n";
cout<< F4( 2, 5);
cout<< "\n This gives exponentiation.\n" << endl;
}
Results of run:
[bert] ~/demos/functions> recursionExamples
Call sample function using F1( 1, 5)
6
Call sample function using F1( 8, 3)
0
Call sample function using F2( 2, 4)
8
This implemented multiplication
Call sample function using F3( 256)
2
This gives first digit of input
Call sample function using F4( 2, 5)
32
This gives exponentiation.
Download