Considering that
A function that is defined in terms of itself is called recursive.
Without defining the
When doing recursive functions, the
A recurive
function will not make any sense without the base case.
Question: Where is the base case
?
int f(int x) {
if ( x == 0 )
return 0;
else
return 2 * f( x - 1 ) + x * x;
}
bad
recursion exampleConsider the following function and try to find out where is the issue:
int bad(int n) {
if ( n == 0 )
return 0;
else
return bad(n / 3 + 1 ) + n - 1;
}
We have a non-terminating recursive function - bad_recursion.cpp.
How could we get out of the non-terminating case?
void printOut (int n) {
if ( n >= 10 )
printOut( n / 10 );
printDigit(n % 10);
}
Question:
What would be the order of call to functions printOut()
and printDigit()
if we initially call printOut(355)
?
Implement a recursive function to find a digit in any given float number. Consider the following requirements.
find_digit(float n, int d)
4
1
if the digit is found at least 1 time, otherwise, it will return 0
int
numbers, once you have something working, move to float
numbers.Source code: find_digit.cpp
This material is genereated thanks to some extracts from following resources:
AI Overview