Question

In: Computer Science

Write it in C++ The most disgusting recursive problem ever. The McDonald’s near campus sells Chicken...

Write it in C++

The most disgusting recursive problem ever. The McDonald’s near campus sells Chicken McNuggets in orders of 6, 9, or 20. Suppose you are ordering for a party and you know exactly how many McNuggets will be eaten by guests. It turns out that, for any integer n ≥ 44, you can order exactly n Chicken McNuggets at this McDonald’s. For purposes of this problem, you cannot throw out McNuggets or allow them to go uneaten, such as by acquiring n = 44 by buying two twenty packs and a six pack, then discarding two. If the thought of this many Chicken McNuggets is too disgusting, you may pretend you are buying n ≥ 44 celery sticks in bunches of 6, 9, or 20 (feel free to rename the function below in that case). Finish the recursive function below to complete the ordering and return the counts by reference parameters. You may assume for this problem that there will be no overflow or underflow at any point in the problem and that stack space is not a concern. The code has been started for you and is part of a correct solution.

void buyChicken ( unsi gned n , un si gned & num6Packs , un si gned & num9Packs , un signed & num20Packs ) { i f ( 44 == n ) { num20Packs = 1 ; num6Packs = 4 ; num9Packs = 0 ; }

e l s e i f ( 4 5 == n ) { num20Packs = 0 ; num6Packs = 3 ; num9Packs = 3 ; } e l s e i f ( 4 6 == n ) { num20Packs = 2 ; num6Packs = 1 ; num9Packs = 0 ; }

Solutions

Expert Solution

Solution:

If n is divisible by 6, just buy all the num6packs.

else If n is divisible by 9, just buy all the num9packs.

else if n is divisible by 3, buy 1 num9pack and now n will be divisible by 6, so buy all num6packs for remaining nuggets.

else buy one num20pack, and call the buyChicken function recursively.

Following is the function buyChicken in C++

void buyChicken(unsigned n, unsigned &num6packs, unsigned &num9packs , unsigned &num20packs)
{   //base case
    if(n==0)
    return;
    //if n is divisble by 6
    if(n%6==0)
    {
        num6packs = num6packs+n/6;
        return;
    }
    //if n is divisible by 9
    else if(n%9==0)
    {
        num9packs = num9packs+n/9;
        return;
    }
    //if n is divisible by 3
    else if(n%3==0)
    {
        num9packs++;
        num6packs=num6packs+(n-9)/6;
        return;
    }
    //otherwise include a num20pack
    else {
        num20packs=num20packs+1;
        buyChicken(n-20, num6packs, num9packs , num20packs);
    }
}

Following is the complete C++ program to check the same :

#include <iostream>
using namespace std;

void buyChicken(unsigned n, unsigned &num6packs, unsigned &num9packs , unsigned &num20packs)
{   //base case
    if(n==0)
    return;
    //if n is divisble by 6
    if(n%6==0)
    {
        num6packs = num6packs+n/6;
        return;
    }
    //if n is divisible by 9
    else if(n%9==0)
    {
        num9packs = num9packs+n/9;
        return;
    }
    //if n is divisible by 3
    else if(n%3==0)
    {
        num9packs++;
        num6packs=num6packs+(n-9)/6;
        return;
    }
    //otherwise include a num20pack
    else {
        num20packs=num20packs+1;
        buyChicken(n-20, num6packs, num9packs , num20packs);
    }
}
int main()
{
    int n;
    unsigned int num6packs=0,num9packs=0,num20packs=0;
    cout<<"Enter n : ";
    cin>>n;
    buyChicken(n, num6packs, num9packs , num20packs);
    cout<<"num6packs : "<<num6packs<<endl;
    cout<<"num9packs : "<<num9packs<<endl;
    cout<<"num20packs : "<<num20packs;

    return 0;
}

Code demo for reference :

Output :


Related Solutions

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...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that computes the sum of the digits in an integer. Use the following function header: int sumDigits(int n) For example, sumDigits(234) returns 2 + 3 + 4 = 9. Write a test program that prompts the user to enter an integer and displays its sum.
In c++ Write a recursive driver function that will replace each of the odd values in...
In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a specific character from a string b. Write the pseudocode for the program.
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in zyBooks sections 9.4 and 9.5. Prompt the user for the location of a sequence of numbers, via an external file or data entry by the user. If you choose data entry, prompt the user for the number of values and read them into a data structure of your choice. Then use the mergesort algorithm to sort them in ascending order. Finally, prompt for a...
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT