Answered step by step
Verified Expert Solution
Question
1 Approved Answer
For program 7, write MATLAB code to simulate a locker puzzle game. As each student enters for the day, beginning at their locker, they will
For program 7, write MATLAB code to simulate a locker puzzle game. As each student enters for the day, beginning at their locker, they will change the state of some lockers (i.e., if the locker is closed, they will open it, and if it is opened, they will close it). The trick is depending on the student, they will skip over some of the lockers. Read the game details Game Details A school has n lockers and n students, All lockers are closed on the first day of school As the students enter school for the day, the first student (S1) opens every locker beginning with locker one (L1) Then, the second student (S2), beginning with the second locker (L2), closes every other locker (i.e., every second locker) Student three (S3), beginning with the third locker (L3), changes every third locker (closes the locker if it is opened and opens the locker if it is closed) . " Student four (S4), beginning with the fourth locker (L4), changes every fourth locker. - Student five (S5), beginning with locker five (L5), changes every fifth locker . This continues until Student n (Sn), changes just the lasth locker Requirements 1. For this program, you MUST use an array to represent the student lockers, and you MUST prompt for n 2. When finished opening/closing lockers, output which lockers are open and how many times they were opened. Recall that you are keeping track of both the number of times the locker was opened as well as closed. For example, if locker 8 in the lockers array has a value of 11, it has been opened and closed a total of eleven times. However, it has only been opened 6 times 3. EXTRA CREDIT (1 point): Include which locker number has changed the most times, including how many times. On MATLAB, look up the max function Hints 1. Begin by initializing the lockers array to all zeros using MATLAB's zeros(...) function. 0 should represent that the locker is closed 2. Use two nested for-loops to manage both the student number and every nh locker. That is, the outer for-loop steps through each student 1 to n. The inner for-loop begins at student n's number (the outer for-loop variable) and steps through every nh locker 3. Let the array be used to track the total times a locker changes (either opened or closed). If the final number of times is even, the locker is closed. If the final number of times is odd, the locker is open
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started