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

(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.
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
Do it in python please Write a program using functions and mainline logic which prompts the...
Do it in python please Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask the user to input their favorite SuperHero and display a positive or funny comment about the chosen SuperHero. If RBG is chosen, display "You can't spell TRUTH without RUTH." Utilize a Do-While Loop to redisplay the menu to the user after item 1, 2 or 3 is executed. If item 4 is chosen, end the program. If the user chooses 4,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT