Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Cookie Monster is eating a cookie. However, he is very messy about it! Each time

ID: 3765696 • Letter: C

Question

Cookie Monster is eating a cookie. However, he is very messy about it! Each time he bites the cookie, the cookie breaks into multiple pieces. Eventually, these pieces will become crumbs that are too small for Cookie Monster to bite. For this question, your task is to write a recursive program that will calculate how many crumbs are made whenever Cookie Monster tries to eat a cookie that weighs W grams. The details of the crumb creation are as follows: If the cookie's weight is less than or equal to 0.1, it has become a crumb that is too small for Cookie Monster to bite. Thus, there is 1 crumb. Otherwise, Cookie Monster will bite the cookie and the cookie will break into pieces of equal weight. The number of new pieces is randomly chosen to be 2,3, or 4, but once the number of pieces is chosen, the weight of each piece is the same (one-half, one-third or one-quarter of the previous piece). Cookie Monster (being Cookie Monster) will then try to eat each of these pieces. For example, suppose the initial cookie weighed 0.6 grams. Cookie Monster bites it once, and we randomly determine that the cookie breaks into 2 parts (each part weighs 0.3 grams). Cookie Monster then tries to bite the first 0.3 gram piece, which breaks into 3 parts. Since these parts will each weigh 0.1 grams, they are now crumbs, so we have 3 crumbs so far. Cookie Monster will then bite the second 0.3 gram piece; this time, it breaks into 2 parts, each of which weighs 0.15 grams. Cookie Monster bites each of these parts again; the first breaks into 3 parts, each weighingt 0.05 grams, so we have 3 more crumbs. The second breaks into 4 parts, each weighing 0.0375 grams, so that's another 4 crumbs. There are no longer any pieces larger than 0.1 grams, so the total number of crumbs is 3 + 3 + 4 = 10 crumbs. Tracing through the problem like in the paragraph above is exhausting! But if you bring yourself to trust in the power of recursion, solving this problem is not hard at all. Write a recursive function that simulates eating a single cookie. The weight of the cookie (as a float double) should be a parameter to your function. The function should return an integer, indicating the number of crumbs produced from eating the cookie. To write this function, you will once again need to use random numbers for when the cookie is broken into parts. The expression (rand () % 3 + 2) will give you a random number in the range [2-4]. In your main() function, ask the user for the size of the cookie. Then write a loop that will call your crumb-counting recursive function 1000 times. Use the results of those 1000 simulations to report the average number of crumbs that were produced from eating the cookie. An example of your program running might look like this: It is not necessary that your program actually uses Cookie-Monster style grammar, but of course it is preferable. Finally, test your program, using cookies of weight 5, 10 and 100 grams. Copy the results to a testing document for submission.

Explanation / Answer

Implemented task#1

#include <iostream>
#include <stdlib.h>

using namespace std;

int cookie(double weight) {
   cout << "Weight"<< weight << endl;
   int count = 0;
   int parts = 0, i = 0;
   if(weight <= 0.1)
       return 1;
   else {
       parts = rand() %3+2;
       cout << "Parts: "<< parts <<" Weight"<< weight << endl;
       for(i = 0; i < parts; i++ )
           count += cookie(weight/parts);
   }
   return count;
}

//Main Function
int main() {
   cout << cookie(0.1) <<endl;
   cout << cookie(0.2) << endl;
   cout << cookie(0.6) <<endl;
   cout << cookie(1) <<endl;
}

-------------output--------------------

Weight0.1
1
Weight0.2
Parts: 4 Weight0.2
Weight0.05
Weight0.05
Weight0.05
Weight0.05
4
Weight0.6
Parts: 4 Weight0.6
Weight0.15
Parts: 3 Weight0.15
Weight0.05
Weight0.05
Weight0.05
Weight0.15
Parts: 3 Weight0.15
Weight0.05
Weight0.05
Weight0.05
Weight0.15
Parts: 4 Weight0.15
Weight0.0375
Weight0.0375
Weight0.0375
Weight0.0375
Weight0.15
Parts: 3 Weight0.15
Weight0.05
Weight0.05
Weight0.05
13
Weight1
Parts: 2 Weight1
Weight0.5
Parts: 2 Weight0.5
Weight0.25
Parts: 3 Weight0.25
Weight0.0833333
Weight0.0833333
Weight0.0833333
Weight0.25
Parts: 4 Weight0.25
Weight0.0625
Weight0.0625
Weight0.0625
Weight0.0625
Weight0.5
Parts: 4 Weight0.5
Weight0.125
Parts: 4 Weight0.125
Weight0.03125
Weight0.03125
Weight0.03125
Weight0.03125
Weight0.125
Parts: 3 Weight0.125
Weight0.0416667
Weight0.0416667
Weight0.0416667
Weight0.125
Parts: 2 Weight0.125
Weight0.0625
Weight0.0625
Weight0.125
Parts: 3 Weight0.125
Weight0.0416667
Weight0.0416667
Weight0.0416667
19