Menu

incorrect algorithm? locker game

Rusty
2008-02-28
2012-09-26
  • Rusty

    Rusty - 2008-02-28

    This is a homework assignment but I'm completely stumped. The assignment is already late. I'm losing sleep over why this thing wont work. The problem is:
    A principal at an elementary school decides to play a game with the students on the first day of class. There just so happens to be the same amount of lockers as there are students. The principal first lines the students up, and gives her instructions. She tells the first student to go and open all the lockers (assuming all lockers are closed beforehand.) The second student she tells to close all the even lockers (starting at 2, 4, 6, etc...) The third student she tells to check every third locker. If the locker is open, close it. If the locker is closed, open it. The principal tells the fourth student to check every fourth locker in the same manner as the third, and so on and so forth until each student has looped down the line of lockers. After all students have passed by the line of lockers, how many lockers are open, and how many are closed?
    I have attached my source code, which gives the following results for 10 lockers (implicitly that means there are 10 students,) in order from left to right. OOOXXXXXOO (Thats 3 open, 5 closed, 2 open.)
    On paper however I get the following results in order from left to right: OXXOXXXXOX (Thats 1 open, 2 closed, 1 open, 4 closed, 1 open, 1 closed.)
    As far as I know each student should check the locker# corresponding to his place number in line, and jump ahead that many places to the next locker. I must be missing something... Anyway, TIA.

    include <stdio.h>

    include <iostream>

    using namespace std;

    int main()
    {
    int intNum_Lockers;
    int L; // for each locker iteration
    int S; // for each student iteration
    int X; // for each 'check match' iteration
    bool lock_state = false;
    int Lockers_Open = 0;
    int Lockers_Closed = 0;

    cout << "Enter the number of lockers. All non-numeric entries will exit the program:\n";
    cin >> intNum_Lockers;
    do
    {
    if (cin)
    {
    // Start game logic

    for (L=1;L<=intNum_Lockers;L++)
    { //start locker loop

    for (S=1;S&lt;=intNum_Lockers;S++)
    { //start student loop
    
      for (X=S;X&lt;=intNum_Lockers;X=X+S)
      {
          if (X==L)
          {
             if (lock_state == true)
                lock_state = false;
             else if (lock_state == false)
                 lock_state = true;
             break; //escape back to student loop, to iterate next student
          } //end if X=L
    
       } //end for X
    
    } //end student loop
    
       if (lock_state == true)
       {
          Lockers_Open +=1;
          cout &lt;&lt; &quot;O&quot;;
       }
       else if (lock_state == false)
       {
           Lockers_Closed +=1;
           cout &lt;&lt; &quot;X&quot;;
       }
    

    } //end locker loop
    // End game logic

    //output numbers
    cout << "There are " << Lockers_Open << " lockers open, and " << Lockers_Closed << " lockers closed.\n" << endl;
    lock_state = false;
    Lockers_Open = 0;
    Lockers_Closed = 0;

    cout << "Enter the number of lockers. All non-numeric entries will exit the program:\n";
    cin >> intNum_Lockers;
    } //end if (cin)
    } while (cin); // end while

    return 0;

    }

     
    • Eu Genek

      Eu Genek - 2008-02-28

      I apologize for continuing to respond to this post as if it were my monologue. I'm wondering if you haven't learned or aren't allowed to use arrays.

      I believe the problem in your original code has to do with setting the initial state of each locker. Since you are using the same variable to represent the state of every locker then as each new locker is checked it's initial value isn't neccesarily 'false'. Each locker is inspected by every student before the next locker is inspected. This means every locker must be in a closed state before student number 1 inspects it. I believe locker 2 will actually start in an open state since student 1 will open locker 1 setting lock_state to true.

      I made the following change to your code and it appears to fix the problem.

              for ( L = 1; L&lt;=intNum_Lockers; ++L) 
              { //start locker loop 
                  lock_state = false;
      

      I've moved 'lock_state = false' from it's location outside of all loops to just inside the locker loop.

       
    • Eu Genek

      Eu Genek - 2008-02-28

      I think maybe you are forgetting students 1 and 2? Theire instructions are special when compared to the other students.

      I threw the following together quickly and it seems to work. I'll leave finishing and double checking the answers to you.

      include <iostream>

      // A principal at an elementary school decides to play a game with the students
      // on the first day of class. There just so happens to be the same amount of
      // lockers as there are students. The principal first lines the students up, and
      // gives her instructions. She tells the first student to go and open all the
      // lockers (assuming all lockers are closed beforehand.) The second student she
      // tells to close all the even lockers (starting at 2, 4, 6, etc...) The third
      // student she tells to check every third locker. If the locker is open, close it.
      // If the locker is closed, open it. The principal tells the fourth student to check
      // every fourth locker in the same manner as the third, and so on and so forth until
      // each student has looped down the line of lockers. After all students have passed
      // by the line of lockers, how many lockers are open, and how many are closed?

      int main()
      {
      std::cout << "Enter the number of lockers.\n";

      int lockerCount( 0 );
      std::cin &gt;&gt; lockerCount;
      
      // Initialize maxStudents and maxLockers to the lockerCount + 1.  Since both values are the same
      // just one of these are needed.  However, for readability I've made two variables.  The values
      // are lockerCount + 1 so we can ignore locker 0.
      
      const int maxStudents( lockerCount + 1 );
      const int maxLockers( lockerCount + 1 );
      
      bool lockerOpen[maxLockers];
      
      // Set all lockers as they would be after student 1 and student 2 have executed their orders.  Since student 1 is
      // ordered to open all lockers we are really only concerned with student 2's orders.  That is close every even
      // numbered number.  Odd numbered lockers will be open.
      for ( int lockerNum = 1; lockerNum &lt; maxLockers; ++ lockerNum )
      {
          if ( lockerNum % 2 == 0 )
          {
              lockerOpen[lockerNum] = false;
          }
          else    // odd locker.
          {
              lockerOpen[lockerNum] = true;
          }
      }
      
      // Start the student loop at student #3 since student number 1 and 2's orders have already been carried out.
      // Students 3 and later are to inspect every nth locker and open closed lockers and close opened lockers.
      // The value of n is equal to the student number.  
      for ( int studentNum = 3; studentNum &lt; maxStudents; ++studentNum)
      {
          for ( int lockerNum = studentNum; lockerNum &lt; maxLockers; lockerNum = lockerNum + studentNum)
          {
              std::cout &lt;&lt; &quot;Student &quot; &lt;&lt; studentNum;
              if ( lockerOpen[lockerNum] == true )
              {
                  std::cout &lt;&lt; &quot; closes locker &quot; &lt;&lt; lockerNum &lt;&lt; &quot;\n&quot;;
                  lockerOpen[lockerNum] = false;
              }
              else    // locker is closed.
              {
                  std::cout &lt;&lt; &quot; opens locker &quot; &lt;&lt; lockerNum &lt;&lt; &quot;\n&quot;;
                  lockerOpen[lockerNum] = true;
              }
          }        
      }
      
      // Now, all students have executed their orders.  Count the number of open lockers.
      int openLockerCount( 0 );
      for ( int lockerNum = 1; lockerNum &lt; maxLockers; ++lockerNum)
      {
          if ( lockerOpen[lockerNum] == true )
          {
              std::cout &lt;&lt; &quot;O&quot;;
              ++openLockerCount;
          }
          else    // locker is closed.
          {
              std::cout &lt;&lt; &quot;X&quot;;
          }
      
      } 
      std::cout &lt;&lt; &quot;\n&quot;;    
      std::cout &lt;&lt; openLockerCount &lt;&lt; &quot; open lockers, &quot; &lt;&lt; lockerCount - openLockerCount &lt;&lt; &quot; closed lockers.\n&quot;;
      
      system(&quot;pause&quot;);
      

      }

       
    • Eu Genek

      Eu Genek - 2008-02-28

      Although, now that I look at it for more than a couple seconds students 1 and 2 don't have special orders. The following should work too. Sorry.

      include <iostream>

      // A principal at an elementary school decides to play a game with the students
      // on the first day of class. There just so happens to be the same amount of
      // lockers as there are students. The principal first lines the students up, and
      // gives her instructions. She tells the first student to go and open all the
      // lockers (assuming all lockers are closed beforehand.) The second student she
      // tells to close all the even lockers (starting at 2, 4, 6, etc...) The third
      // student she tells to check every third locker. If the locker is open, close it.
      // If the locker is closed, open it. The principal tells the fourth student to check
      // every fourth locker in the same manner as the third, and so on and so forth until each student has looped down the line of lockers. After all students have passed by the line of lockers, how many lockers are open, and how many are closed?

      int main()
      {
      std::cout << "Enter the number of lockers.\n";

      int lockerCount( 0 );
      std::cin &gt;&gt; lockerCount;
      
      // Initialize maxStudents and maxLockers to the lockerCount + 1.  Since both values are the same
      // just one of these are needed.  However, for readability I've made two variables.  The values
      // are lockerCount + 1 so we can ignore locker 0.
      
      const int maxStudents( lockerCount + 1 );
      const int maxLockers( lockerCount + 1 );
      
      // Set all lockers as closed inititially.
      bool lockerOpen[maxLockers];    
      for ( int lockerNum = 1; lockerNum &lt; maxLockers; ++ lockerNum )
      {
          lockerOpen[lockerNum] = false;
      }
      
      // Students are to inspect every nth locker and open any closed lockers and close any opened lockers. 
      // The value of n is equal to the student number.  
      for ( int studentNum = 1; studentNum &lt; maxStudents; ++studentNum)
      {
          for ( int lockerNum = studentNum; lockerNum &lt; maxLockers; lockerNum = lockerNum + studentNum)
          {
              std::cout &lt;&lt; &quot;Student &quot; &lt;&lt; studentNum;
              if ( lockerOpen[lockerNum] == true )
              {
                  std::cout &lt;&lt; &quot; closes locker &quot; &lt;&lt; lockerNum &lt;&lt; &quot;\n&quot;;
                  lockerOpen[lockerNum] = false;
              }
              else    // locker is closed.
              {
                  std::cout &lt;&lt; &quot; opens locker &quot; &lt;&lt; lockerNum &lt;&lt; &quot;\n&quot;;
                  lockerOpen[lockerNum] = true;
              }
          }        
      }
      
      // Now, all students have executed their orders.  Count the number of open lockers.
      int openLockerCount( 0 );
      for ( int lockerNum = 1; lockerNum &lt; maxLockers; ++lockerNum)
      {
          if ( lockerOpen[lockerNum] == true )
          {
              std::cout &lt;&lt; &quot;O&quot;;
              ++openLockerCount;
          }
          else    // locker is closed.
          {
              std::cout &lt;&lt; &quot;X&quot;;
          }
      
      } 
      std::cout &lt;&lt; &quot;\n&quot;;    
      std::cout &lt;&lt; openLockerCount &lt;&lt; &quot; open lockers, &quot; &lt;&lt; lockerCount - openLockerCount &lt;&lt; &quot; closed lockers.\n&quot;;
      
      system(&quot;pause&quot;);
      

      }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.