Question

In: Computer Science

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 the Catalan number. Also create a function of Fibonacci that will take a parameter, calculate using recursion the Fibonacci number and return the number. Main will have a do/while loop that will print the menu, enter the selection, and call the functions. Have it quit on 0. Fibonacci are the sequence 1 1 2 3 5 8 13 21 Catalan numbers follow the sequence defined in Chapter 5 of the text. Also, the formula for the Catalan sequence is given here: https://en.wikipedia.org/wiki/Catalan_number (Links to an external site.)

Solutions

Expert Solution

C++ program:

#include<iostream>
using namespace std;
// Function to find the catalan number
unsigned long int catalan(unsigned int n)
{
    // Base case
    if (n <= 1) return 1;
 
    // catalan(n) is sum of catalan(i)*catalan(n-i-1)
    unsigned long int res = 0;
    for (int i=0; i<n; i++)
        res += catalan(i)*catalan(n-i-1);
    // Returning the calculated result
    return res;
}

// Function to calculate the nth Fibonacci number
int fib(int n) 
{ 
    // base conditions at n=1 and n=0
    if (n == 1) 
        return 1;
    else if (n == 0)
        return 0;
    // recursive call for n-1 and n-2 
    return fib(n-1) + fib(n-2); 
} 
  

// main Function
int main()
{
    //  while loop till  user presses 0 to quit
    while (1)
    {
        // printing the menu for user
        int choice;
        cout<<"1. Do Catalan numbers "<<endl;
        cout<<"2. Do Fibonacci numbers (recursive)"<<endl;
        cout<<"0. Quit"<<endl;
        cout<<"Enter selection: ";
        // taking user choice 
        cin>>choice;
        // if choice is 1 calculating Catalan number
        if (choice==1)
        {
            int num;
            cout<<"Enter Catalan number to calculate: ";
            cin>>num;
            cout<<"Catalan number at "<<num<<" is "<<catalan(num)<<endl;
        }
        // if choice is 2 calculating Fibonacci number
        else if (choice==2)
        {
            int num;
            cout<<"Enter Fibonacci number to calculate: ";
            cin>>num;
            cout<<"Fibonacci number "<<num<<" is "<<fib(num)<<endl;
        }
        // if choice is 0 ending the program
        else
        {
            break;
        }
    }
    return 0;
}

Screenshot of code for reference:

Screenshot of output:


Related Solutions

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...
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...
Use C++ to create an application that provide the menu with two options TemperatureConverter_Smith.cpp MENU CONVERTER...
Use C++ to create an application that provide the menu with two options TemperatureConverter_Smith.cpp MENU CONVERTER TEMPERATURE – JAMES SMITH Convert Fahrenheit temperature to Celsius Convert Celsius temperature to Fahrenheit Exit CASE 1: Convert Fahrenheit temperature to Celsius -Display the message to ask to enter Fahrenheit degree from the keyboard -Use the following formula to convert to Celsius degree         Celsius Temperature = (Fahrenheit Temperature – 32) * 5/9 ; -Display the output as below: TemperatureConverter_Smith.cpp TEMPERATURE CONVERTER – JAMES...
IN C++ ONLY... please do both parts of this task ..1 part- Menu driven program First...
IN C++ ONLY... please do both parts of this task ..1 part- Menu driven program First load an array(lists) with the values 3, 4, 84, 5, 2, 47, and 7 in this order by using a function. Then sort the array(list) Use a function to display the menu. Create a program with a menu, with choices 1 for factorial (single value, which everyone will use 20!), 2 for median value , 3 for average, 4 for maximum, 5 for minimum,...
Instructions for C++ project This Assignment will Focus on Software Engineering: Using Functions in a Menu...
Instructions for C++ project This Assignment will Focus on Software Engineering: Using Functions in a Menu Driven Program Hint: Chapter 6 Program 6-10 is a great example of this type of programs. Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1...
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...
Using C++ Write One one single program with two or more functioncallsWrite a C++...
Using C++ Write One one single program with two or more function callsWrite a C++ function, smallest Index, that takes as parameters an int array and its size and returns the index of the smallest element in the array. Also the program should test the function.Write another function that prompts the user to input a string and outputs the string in uppercase letters. You must use a character array to store the string.
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A function to get principal, rate, and term. A function to calculate monthly payment. Test your program by getting the data from the user by using the first function and calculate monthly payment by calling the other function. Add version control and write proper comments with a few blank lines & indentations in order to improve your programming style.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT