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<=intNum_Lockers;S++)
{ //start student loop
for (X=S;X<=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 << "O";
}
else if (lock_state == false)
{
Lockers_Closed +=1;
cout << "X";
}
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;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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<=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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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";
intlockerCount(0);std::cin>>lockerCount;//InitializemaxStudentsandmaxLockerstothelockerCount+1.Sincebothvaluesarethesame//justoneoftheseareneeded.However,forreadabilityI'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'sorders.Thatiscloseeveryeven//numberednumber.Oddnumberedlockerswillbeopen.for(intlockerNum=1;lockerNum<maxLockers;++lockerNum){if(lockerNum%2==0){lockerOpen[lockerNum]=false;}else//oddlocker.{lockerOpen[lockerNum]=true;}}//Startthestudentloopatstudent#3sincestudentnumber1and2'sordershavealreadybeencarriedout.//Students3andlateraretoinspecteverynthlockerandopenclosedlockersandcloseopenedlockers.//Thevalueofnisequaltothestudentnumber.for(intstudentNum=3;studentNum<maxStudents;++studentNum){for(intlockerNum=studentNum;lockerNum<maxLockers;lockerNum=lockerNum+studentNum){std::cout<<"Student"<<studentNum;if(lockerOpen[lockerNum]==true){std::cout<<"closeslocker"<<lockerNum<<"\n";lockerOpen[lockerNum]=false;}else//lockerisclosed.{std::cout<<"openslocker"<<lockerNum<<"\n";lockerOpen[lockerNum]=true;}}}//Now,allstudentshaveexecutedtheirorders.Countthenumberofopenlockers.intopenLockerCount(0);for(intlockerNum=1;lockerNum<maxLockers;++lockerNum){if(lockerOpen[lockerNum]==true){std::cout<<"O";++openLockerCount;}else//lockerisclosed.{std::cout<<"X";}}std::cout<<"\n";std::cout<<openLockerCount<<"openlockers,"<<lockerCount-openLockerCount<<"closedlockers.\n";system("pause");
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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";
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
} //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;
}
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.
I've moved 'lock_state = false' from it's location outside of all loops to just inside the locker loop.
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";
}
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";
}