Recursion Practice: Do the programs below, comment and show the output window.
Mutual Recursion with example of Hofstadter Female and
Male sequences
Mutual recursion is a variation recursion. Two functions are called mutually recursive if the first function makes
a recursive call to the second function and the second function, in turn, calls the first one.
In software development this concept is used in circular dependency which is a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as mutually recursive.
A great example of mutual recursion would be implementing the Hofstadter Sequence.
Hofstader Sequence
1- In mathematics, a Hofstadter sequence is a member of a family of related integer sequences defined by non-
linear recurrence relations. In this example we are going to focus on Hofstadter Female and Male sequences:
// C++ program to implement Hofstader Sequence // using mutual recursion
#include <bits/stdc++.h>
using namespace std;
int hofstaderFemale(int); int hofstaderMale(int);
// Female function
int hofstaderFemale(int n)
{
if (n < 0)
return 0; else
}
if (n == 0) return 1;
else
return (n
- hofstaderFemale(n - 1));
n)
- hofstaderMale(n - 1));
// Male function
int hofstaderMale(int
{
if (n < 0)
return 0; else
}
if (n == 0) return 0;
else
return (n
// Driver Code int main()
{
int i;
cout << "F: ";
for (i = 0; i < 20; i++)
cout << hofstaderFemale(i) << " ";
cout << "\n";
cout << "M: ";
for (i = 0; i < 20; i++)
cout << hofstaderMale(i)<< " ";
return 0; }
A- Explain the functionality of following functions.
2-
/* Assume that n is greater than or equal to 1 */ int fun1(int n)
{
if(n == 1)
Disadvantages of Circular Dependency/Mutual Recursion:
1. Circular dependencies can cause a domino effect when a small local change in one module spreads into other modules and has unwanted global effects
2. Circular dependencies can also result in infinite recursions or other unexpected failures.
3. Circular dependencies may also cause memory leaks by preventing certain very primitive automatic
garbage collectors (those that use reference counting) from deallocating unused objects.
return else
return
0;
1 + fun1(n/2);
}
3-
/* Assume that n is greater than or equal to 0 */ void fun2(int n)
{
if(n == 0)
return;
fun2(n/2);
cout << n%2 << endl;
}
4-
void fun1(int n)
{
int i = 0;
if (n > 1) fun1(n - 1);
for (i = 0; i < n; i++) cout << " * ";
}
5-
#define LIMIT 1000 void fun2(int n)
{
if (n <= 0)
return;
if (n > LIMIT)
return;
cout << n <<" ";
fun2(2*n);
cout << n <<" ";
}
6- Predict the output of following program #include <iostream>
using namespace std;
void fun(int x)
{
if(x > 0)
{
fun(--x);
cout << x <<" ";
fun(--x); }
}
int main()
{
int a = 4;
fun(a);
return 0; }
7- Predict the output of following program. What does the following fun() do in general? #include <iostream>
using namespace std;
int fun(int a[],int n)
{
int x;
if(n == 1) return a[0];
else
x = fun(a, n - 1);
if(x > a[n - 1]) return x;
else
return a[n - 1];
}
int main()
{
int arr[] = {12, 10, 30, 50, 100}; cout << " " << fun(arr, 5) <<" "; getchar();
return 0;
}
版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。