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...
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 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
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest...
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest value in the array. If the array is empty, return the “end” pointer ·and takes as parameters: (1)   a pointer to double. The pointer is the address of the start of an array, (2)   the “end” pointer to the address after the array (3)   and the address of the smallest value seen so far ·Write main() to test this function – try a case where the array...
it should be c++ data structure Write a recursive function that draws the following shape. X...
it should be c++ data structure Write a recursive function that draws the following shape. X XXX XXXXX XXXXXXX XXXXXXXXX XXXXXXXXX XXXXXXX XXXXX XXX X The length of the longest row in the shape and the shape's character are function parameters. In above shape the longest row is 9 and the pattern's character is "X”.
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT