Question
How can I make these loops work? I am suppose to make 2 different loops, if the number of students is even, it is supposed
How can I make these loops work?
I am suppose to make 2 different loops, if the number of students is even, it is supposed to start at the end and iterate through the locker numbers, making every locker at the position of the number of the student (meaning if std = 2, then every 2 lockers, if is 4, every 4 lockers and so on) become true if false and false if true.
The second loop is supposed to run only if the number of the student is odd and greater than 1 and start at the beginning of the array and iterate through the locker numbers, making every locker at the position of the number of the student (meaning if std = 3, then every 3 lockers, if is 5, every 5 lockers and so on) become true if false and false if true.
That is what I have, but somehow it is not working at all.
if (std % 2) { for (i = locker; i > 0; i--) { for (j = 1; j <= std; j += i) { lockers[i] = !lockers[i]; } } }
//for the odd number of students (third student and on) if (!(std % 2) && std > 1) { for (i = 0; i <= locker; i++) { for (j = 1; j <= std; j += i) { lockers[i] = !lockers[i]; } } }
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