Thread: Locker Program

  1. #1
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186

    Locker Program

    I have to make a program for my Computer Science class, heres the specs:

    There are 1000 closed lockers in a school. There are 1000 students outside the school. The first student goes into the school and visits every locker and opens it. the second student goes into the school and closes every locker that is a multiple of two. The third student goes into the school and visits every locker thats a multiple of three. If the locker is open he/she closes it, if the locker is closes he/she opens it. The fourth student goes into the school and visits every locker that is a multiple of four. Again if the locker is open he/she closes it, vice versa. This continues until all 1000 students have gone into the school and gone to the appropriate lockers.

    Write a program that will determine and display the status of each locker after all of the students have gone into the school.


    Heres the code i have so far:
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <apvector.h>
    #include <apstring.h>
    #include <apstring.cpp>
    #include <iomanip.h>
    //#include "title.cpp"
    
    class LockerClass
    {
    	private:
    		struct LockerType
    		{
    			int LockerNum;
    			bool Status;
    			double Money;
    		};
    	public:
    		apvector <LockerType> Lockers;
    		LockerClass();
    		~LockerClass();
    		void ChangeStatus();
    		void DisplayOpenClose();
    		void LockerMoney();
    		void LockerSort();
    
    };
    
    
    void main()
    {
    	LockerClass School;
    	School.ChangeStatus();
    	School.DisplayOpenClose();
    	School.LockerMoney();
    	School.LockerSort();
    }
    
    LockerClass::LockerClass()
    {
    	Lockers.resize(1000);
    	for (int K = 0; K < 1000; K++)
    	{
    		Lockers[K].LockerNum = K +1;
    		Lockers[K].Status = false;
    		Lockers[K].Money = 0;
    	}
    }
    
    LockerClass::~LockerClass()
    {
    }
    
    
    void LockerClass:: DisplayOpenClose()
    {
    	clrscr();
    	int K; int N; apstring Open;
    	N = Lockers.length();
    	//Title("Locker Status");
    	for (K = 0; K < N; K++)
    	{
    		if (Lockers[K].Status == 0)
    			Open = "closed";
    		else
    			Open = "open";
    
    		cout << "Locker[" << Lockers[K].LockerNum << "]" << "\t";
    		cout << setw(15) << "Status: " << Open << "\t";
    		cout << setw(20) << "Money: " << Lockers[K].Money;
    		cout << endl;
    		if (K%20 == 0)
    		getch();
    	}
    }
    
    void LockerClass::ChangeStatus()
    {
    for (int K = 1; K < 1000; K++)
    	for (int N = 1; N < 1000; N++)
    	{
    		if (N % K == 0)
    		{
    			if (Lockers[N].Status == 0)
    				Lockers[N].Status = 1;
    			else
    				Lockers[N].Status = 0;
    		}
    	}
    }
    
    
    void LockerClass::LockerMoney()
    {
    	/*
    	int K;
    	double Money;
    	//Title("Locker Money Function");
    	if (Lockers[K].LockerNum == 0)
    		Lockers[K].Money+=.25;
    	*/
    }
    
    void LockerClass::LockerSort()
    {
    getch();
    }
    ignore the LockerSort() and LockerMoney() functions.

    When i run the program, some of the locker status is correct some is not, for example Locker 1 is closed when it should be open.

    I think i've isolated the problem in the ChangeStatus() function, but it might start off in DisplayOpenClose().

    Does anyone see anything wrong with my algorithm?
    If you need more information, ask.
    the best things in life are simple.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    27
    heres what i would do (ruoughly):
    Code:
    bool lockers[1000];
    int multiple
    for(int i=0;i<1000;i++)
        lockers[i]=0; //0 is closed. 1 is open.
    
    for(i=0;i<1000;i++)
    {
         multiple=1;
         while(multiple<1000)
          {
                 if(lockers[multiple]==0)
                      lockers[multiple]=1;
                 else
                      lockers[multiple]=0;
        
                 multiple*=i;
           }
    }
    
    for(int i=0;i<1000;i++)
          cout<<lockers[i];

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Since Status is a bool value, you can do this to alter it:

    Code:
    lockers[multiple]=!lockers[multiple];

  4. #4
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    wouldn't you have to use the modulus operator somewhere, for multiples?

    and for:

    lockers[multiple]=!lockers[multiple];

    where would i place that?
    Last edited by xlnk; 03-28-2002 at 09:10 PM.
    the best things in life are simple.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Replace it with the code with the if(lockers[multiple] == 0).
    Last edited by Crossbow; 03-28-2002 at 10:26 PM.

  6. #6
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    Hey xlnk,

    after you finish your program could you post/attach the status of the lockers after student 1000 has finished.

    heres' mine
    -

  7. #7
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    i replace it but then all the lockers appeared closed.
    Last edited by xlnk; 03-29-2002 at 09:17 AM.
    the best things in life are simple.

  8. #8
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    ihsir, could you please post your code? Thanks.
    the best things in life are simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM