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

13 Eggs Problem A tortoise and the hare saw 13 Easter eggs in the yard. Both the

ID: 3890255 • Letter: 1

Question

13 Eggs Problem

A tortoise and the hare saw 13 Easter eggs in the yard. Both the tortoise and the hare

wanted all of the eggs for themselves, so they decided to divide them up among themselves. They would alternate turns and in each turn one to three eggs would be taken. If more than 3 eggs are selected, the selection was illegal and the selection should be made again. They agreed that whichever one took the last egg would be able to keep all of the 13 eggs. The tortoise, being the slowest, was allowed to have the first turn.

First Set of Data:   The turns were as follows:

          Tortoise              Hare          Eggs Left

                                                              13     

                1                                            12     

                                        2                    10

                2                                              8

                                        1                      7   

                3                                              4

                                        3                      1

                1                                              0

          Winner is : Tortoise

Second Set of Data:   The turns were as follows:

          Tortoise              Hare          Eggs Left

                                                              13     

                2                                            11     

                                        2                      9

                1                                              8

                                        3                      5   

                2                                              3

                                        3                      0

          Winner is : Hare

Third Set of Data:   The turns were as follows:

          Tortoise              Hare          Eggs Left

                                                              13     

                2                                            11     

                                        2                      9

                5                                                     ERROR MESSAGE

                3                                              6

                                        2                      4

                3                                              1   

                                        2                             ERROR MESSAGE

                                        1                      0

          Winner is : Hare

Flag Controlled Loop – 13 Eggs Problem

Write the program described BELOW

                                                 Use the file egggame.cpp to get started with this

                                                          exercise.  

                                                  ADD to your c++ program as a comment      
                                                    the output from executing your program.

                                                  USE the 3 sets of data given with the problem description.

Modify the 13 eggs problem.

                                          Write a function that makes the selection for the Tortoise.

                                           The prototype for the function is:   int tortoisePick(int numberEggs, int harePicked)

                                           There is a strategy that you can use so that the Tortoise always wins.                                                  

                                            When the function is called, if numberEggs is equal to 13,

                                             the tortoise is making the first selection. If

                                             numberEggs is not equal to 13 , the hare has

                                             made a selection and the hare’s selection was passed

                                             as the second parameter, harePicked.  

                                          The return value is the number of eggs that the Tortoise selects.

Hint: After the hare picks, if the tortoise makes a selection so that the sum of

both picks is 4, then the tortoise will always be the winner, since 12 is evenly

divisible by 4.

  

Explanation / Answer

(No Strategy)

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

using namespace std;


int tortoisePick(int numberEggs, int harePicked){
      return (4 - harePicked);
}

int main(){

   int total = 13;
   string str;
   int opt;
   int n;


   int count = 1;
   cout << setw(15) << std::left << "Tortoise" << setw(15) << std::left << "Hare" << setw(15) << std::left << "Eggs Left" << endl;
  
   cout << setw(15) << std::left << "" << setw(15) << std::left << "" << setw(15) << std::left << total << endl;
   srand(time(NULL));
   while (total > 0){
      do {

             opt = rand() % 3 + 1;

      } while (opt > total);
      total = total - opt;
      if (count % 2 == 0){
         cout << setw(15) << std::left << "" << setw(15) << std::left << opt << setw(15) << std::left << total << endl;
         n = 1;

      }
      else {
         cout << setw(15) << std::left << opt << setw(15) << std::left << "" << setw(15) << std::left << total << endl;
         n = 2;
      }
     
      count++;
   }

   if (n == 1)
      cout << "Tortoise is winner" << endl;
   else
      cout << "Hare is winner" << endl;
  
}

With Strategy

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

using namespace std;


int tortoisePick(int numberEggs, int harePicked){
     
      if (numberEggs < 13){
         if ((4-harePicked) > numberEggs)
             return numberEggs;
         else
             return (4-harePicked);
      }
      else
         return(rand() % 3 + 1);
}

int main(){

   int total = 13;
   string str;
   int opt;
   int n;
   int harepicked;
   int tortoisepicked;

   int count = 1;
   cout << setw(15) << std::left << "Tortoise" << setw(15) << std::left << "Hare" << setw(15) << std::left << "Eggs Left" << endl;
  
   cout << setw(15) << std::left << "" << setw(15) << std::left << "" << setw(15) << std::left << total << endl;
   srand(time(NULL));
   harepicked = 0;
   while (total > 0){
      do {
          if (count % 2 != 0)
             opt = rand() % 3 + 1;
          else{
             opt = tortoisePick(total,harepicked);
             break;
          }
      } while (opt > total);
      total = total - opt;
      if (count % 2 != 0){
         cout << setw(15) << std::left << "" << setw(15) << std::left << opt << setw(15) << std::left << total << endl;
         n = 1;
         harepicked = opt;
      }
      else {
        
         cout << setw(15) << std::left << opt << setw(15) << std::left << "" << setw(15) << std::left << total << endl;
         n = 2;
      }
     
      count++;
   }

   if (n == 1)
      cout << "Tortoise is winner" << endl;
   else
      cout << "Hare is winner" << endl;
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote