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:
ignore the LockerSort() and LockerMoney() functions.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(); }
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.