Question

In: Computer Science

C++ Create a recursive program what will test three recursive functions. It would have a menu...

C++

Create a recursive program what will test three recursive functions. It would have a menu like:

1. Recursive factorial

2. Towers of Hanoi

3. Recursive summation

0. Exit

Enter selection: 3

Enter number: 4

The recursive summation will take an integer and sum value from 1 to the integer. Don’t enter a number if the selection is 0 for quit. Please have the functions in an implementation file and the prototypes of the functions in a header file and the main in a separate file.

Solutions

Expert Solution

//header file

//.h file

//header file
int recursive_factorial(int n);
void recursive_TowersOfHanoi(int n,char fromRod,char toRod,char auxRod);
int recursive_summation(int n);

//implementation file

//implementation
#include<iostream>
#include "recursivefunction.h"
using namespace std;
//method to find factorial of given n
int recursive_factorial(int n)
{
   if(n==1)return 1;
   return n*recursive_factorial(n-1);
}
//method to sole tower of hanoi problem
void recursive_TowersOfHanoi(int n,char fromRod,char toRod,char auxRod)
{
if (n == 1)
{
cout << "Move disk 1 from rod " << fromRod <<
" to rod " << toRod<<endl;
return;
}
recursive_TowersOfHanoi(n - 1, fromRod, auxRod, toRod);
cout << "Move disk " << n << " from rod " << fromRod <<
" to rod " << toRod << endl;
recursive_TowersOfHanoi(n - 1, auxRod, toRod, fromRod);
}
//method to find summation from 1 to n
int recursive_summation(int n)
{
   if(n==1)return 1;
   return n+recursive_summation(n-1);
}

//main file

#include<iostream>
#include "implementation.cpp"
using namespace std;
int main()
{
   //printing menu
   while(true)
   {
       int c,n;
       cout<<"1. Recursive factorial\n2. Towers of Hanoi\n3. Recursive summation\n0. Exit \nEnter selection:";
       cin>>c;
       if(c==0)break;
       else if(c==1)
       {
           cout<<"Enter number: ";
           cin   >>n;
           cout<<"Factorial of "<<n<<" is :"<<recursive_factorial(n)<<endl;
       }
       else if(c==2)
       {
           cout<<"Enter number of rods : ";
           cin   >>n;
           cout<<"Solution to towers of hanoi :\n";
           recursive_TowersOfHanoi(n,'A','C','B');
       }
       else if(c==3)
       {
           cout<<"Enter number: ";
           cin   >>n;
           cout<<"Summation of "<<n<<" is :"<<recursive_summation(n)<<endl;
       }
       else cout<<"Invalid option\n\n";  
   }
  
   return 0;
}

output:

1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection:1
Enter number: 5
Factorial of 5 is :120
1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection:1
Enter number: 4
Factorial of 4 is :24
1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection:3
Enter number: 5
Summation of 5 is :15
1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection:2
Enter number of rods : 2
Solution to towers of hanoi :
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
1. Recursive factorial
2. Towers of Hanoi
3. Recursive summation
0. Exit
Enter selection:0


Process exited normally.
Press any key to continue . . .


//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.


//if you have any doubts, ask me in the comments


Related Solutions

Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program should allow the user to select one of the options according to whether lengths, weights or volume are to be converted, read the volue to be converted and the units, and then call the appropriate function to carry out the conversion In unit out unit I C (inch to centimeter 1 in = 2.4 cm) F C (feet to centimeter 1 ft = 30.4...
Please C++ create a program that will do one of two functions using a menu, like...
Please C++ create a program that will do one of two functions using a menu, like so: 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 1 Enter Catalan number to calculate: 3 Catalan number at 3 is 5 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 2 Enter Fibonacci number to calculate: 6 Fibonacci number 6 is 8 Create a function of catalan that will take a parameter and return...
In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In...
In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character. Example: Input of “Hello,...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish...
In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish and Spanish to English. Your translation program should use arrays for this program. You will need to populate the arrays with the contents of the English and Spanish data files provided. The two files are ordered such that each word in the one file corresponds to the respective translation in the other (i.e.: the first word in the ENG.txt file corresponds to the first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT