-
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.
-
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];
-
Since Status is a bool value, you can do this to alter it:
Code:
lockers[multiple]=!lockers[multiple];
-
wouldn't you have to use the modulus operator somewhere, for multiples?
and for:
lockers[multiple]=!lockers[multiple];
where would i place that?
-
Replace it with the code with the if(lockers[multiple] == 0).
-
Hey xlnk,
after you finish your program could you post/attach the status of the lockers after student 1000 has finished.
heres' mine
-
i replace it but then all the lockers appeared closed.
-
ihsir, could you please post your code? Thanks.