Nested For Loops, im LOST

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.
Unless you want to declare a thousand integers individually, you might want to think about using arrays
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).
Last edited on
I think it's the prime numbers except for 1 who are locked and the rest of them unlocked.
You need an array for lockers that will have values 1 and 0


I hope you weren't recommending using an int containing either 1 or 0. What's wrong with bools?
I would do somthing like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#define NUM_OF_LOCKERS 1000

using namespace std;

int main(int, char**) {
bool locker[] = new bool[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;
}
You need an array for lockers that will have values 1 and 0



I hope you weren't recommending using an int containing either 1 or 0. What's wrong with bools?

Of course it would be waste of space.
Thanks everyone for the time and the help.

All i know is that my instructor told me to use a nested for loop and nothing for arrays.

Is there a different way?

thanks for the help and time
Last edited on
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.
Im Sure.

I know for a fact that it is a nested for loop(he showed in the class)

and thats what he is looking for
no one with a solution or an idea with a nested for loop?!
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.

Thanks againf for your time and help.
If he did not have arrays or a thousand variables, then how was he able to store the values of a thousand lockers?

You need something to be able to store a true or false value for every number between 1 and 1000.
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.
Last edited on
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.

Thanks again to everyone for their time and help.
Okay, how would you go about getting the number of factors then?
Topic archived. No new replies allowed.