- #1
GE2014
- 11
- 0
Homework Statement
(see attached files)
basically, I have to solve a puzzle game simulated by matlab. In a school of n students with n lockers, each student opens and closes a locker they walk by. all the lockers start the game closed. the first student changes every locker's status (opens them). the second student changes the status of every 2ed locker. the third student changes every third locker etc etc. the goal is to determine the lockers that are left open after n students have gone through
Homework Equations
The assignment says I will use 2 for loops. one to step through all the students and one inside of the first to step through each nth locker. the 2ed for loop is pretty confusing to me.
The Attempt at a Solution
Heres what I have so far.
students=input('Enter number of students: ')
locker=zeros(1,students);
index=1;
changes=0;
for num= 1:1:students
locker(index)=changes+index;
index=index+1;
end
the code here steps through as if each student changes the status of each locker, starting with the next locker from the previous student.
the array looks like this
students=8
1 2 3 4 5 6 7 8
odd=open
even=closed
when it should look like this
1 2 2 3 2 4 2 4
Attachments
Last edited: