There are 1,000 students and 1,000 lockers, numbered from 1 to 1,000, on one side of a long hallway. The first person went down the hall and opened every locker.
The second person went down the hall and changed every second locker (i.e. closed every second locker).
The third person went down the hall and changed every third locker
(i.e. closed each open third locker, and opened each closed third locker). Similarly, the fourth person changed every fourth locker.
The fifth changed every fifth locker, and so on.
After the 1,000th person changed the 1,000th locker door what is the number of the total lcokers open and which lockers are open.
I need some help in geting started and the way i can do this is with nested for loop and NO ARREYS because i have not gone that far in the book.
Please any help is appreciated THANKS A LOT for the time and help.
You need an array for lockers that will have values 1 and 0 implying closed and opened,respectively (or vice versa).And use % operator to toggle the state of the lockers,for each student number(i.e 3rd student will toggle the state of the lockers that have array index when index%3==0).
#include <iostream>
#define NUM_OF_LOCKERS 1000
usingnamespace std;
int main(int, char**) {
bool locker[] = newbool[NUM_OF_LOCKERS];
for (int j=1; j<1000; j++) { //j is the person number
for (int i=0; i<1000; i+=j) { //when j==1 every locker will be switched
//when j==3 every third locker will be switched
locker[i] = !locker[i]; //change locker
}
}
// print results
delete[] locker;
return 0;
}
Are you absolutely sure? If you can't use arrays then you might as well do this problem by hand (which might not be too hard). If you're sure then you should think of how you would do it by hand.
Codist gave you a solution with a nested for loop. We are not here to do your homework, I could type out in 3 minutes the answer to the problem, but Codist already gave you a pretty complete solution. If your professor showed you what he wanted done in class, why don't you just do that?
On the subject of arrays, you NEED them, unless you wish to individually name 1000 variables, or are capable of using a container more complex then an array.
Intrexa(38)
Thanks for taking your time on my topic.
But i want to say that this program can be writen without arrays and the teacher showed the output in class.(so much for NEED them). and he did not have 1000 variables.
Second im there to learn not to cheat, i just wanted someone with a lot more experience and knowledge than i have to give some advice. I am having a hard time coming with a solution or an algorithm for this specific problem.
I also said it could be implemented without an array or 1000 different variables, but all those methods would be more complex. Go ahead, create a 1,000 char long string, create your own 1000 bit data type, store it in a linked list. If you want to store the data, you will need an array, or a more advanced container.
That being said, I do suppose you could calculate each locker individually, start at locker 1, simulate 1000 students, output whether it is open or shut, move to locker 2, simulate all 1000 students again, continue through locker 1000. It would be horribly inefficient, and rather useless because it doesn't store the data.
Does this help, cuz for me at this point is like a foreing language.
He is looking for smth like this.
The outside for loop will loop thru the number of lockers that are in the game ...
The inner for loop will be used to determine (count) the number of divisors (or factors) of each locker one at a time.
The number of factors your find (even vs. odd) will determine if the locker is open or closed.