Oh, that's right, you're supposed to mimic the students walking through the doors -- oops, my bad!
I was thinking of the for loop at the end to show the final doors that remain open, and that would be sufficient.
Do you want to show the status of the doors, as they change? Little text based display to represent the doors?
This is a little tricky, because you have to use the index of the doors array, in an entirely new manner (for you). Post up your code and let's see where you are at.
I don't need to show the doors as they change, just the end result. For example I would want to print: The doors that remain open are: 2,6,8,57. Something along those lines. I don't have much of a code to show because I am playing around with things to get to what I want. I realize this is nothing like what I need it to be, but this is what I am troubleshooting with to understand what is going on(by using the debugger tool).
Don't make fun of me because of how off I am please :/Code:#include <stdio.h> int main(void) { int i,s,d[100],n; for (i=0;i<100;i++) d[i]=i; i=0; for(s=1;s<100;s++) { for(n=1;i<100;n++) { i=s*n; } i=0; } return (0); }
It still looks like you're throwing code at the problem and hoping something will stick.
The first thing you need to do, even before opening and shutting various doors, is decide how you are going to represent the doors. How do you tell the difference between an open door and a closed one?
Code:while(!asleep) { sheep++; }
Actually you're not that far off at all... based on what you've posted, you mostly need to devise a way to make the door change, which you aren't doing yet... the loops are pretty close though.
I don't mean to lecture... but what is happening now is that analysis phase I discussed earlier, except you're doing it with live code... yes it can be done that way but it's not always the smartest way to do it since you can easily "code yourself into a corner". The bigger the job the more likely that becomes. (And yes, I've done it to myself moe than once.)
You're right, I am analyzing it with live code. I know realize that is a bad habit, but I have troubling doing it without being able to see what is happening.
As far as figuring out which doors are doing what I can't seem to get anywhere close to making it work. I was think that you could have some sort of system that says 0 for closed doors and 1 for open doors, at the end print all the doors with a 1. I can't figure out how to implement that though.
Ok, here's a hint... the door starts out closed... a student goes through and it's open... a student goes through and it closes... student open... student closed... see a pattern? What happens every second time? What is every second number?
#define Open 1
I agree, I thought it added to the clarity of the whole thing.
You need to swap your for loops. Put the studentNumbers on the outer for loop, and the door[] logic, inside the nested for loop. You don't need the third for loop, until you're ready to print the final status.
I would change your doors array size to 101, and set the value of each element of that array, to 0, instead of i. Start your for loops at 1, and stop at <= 100, instead of just < 100.
That will initialize all the doors starting to closed. (what I would call closed, anyway).
Last edited by Adak; 10-16-2011 at 08:53 PM.
While it's undoubtedly true, a simple if() statement will handle that door open or door closed action.
you want s*n, but you need it as the index for the doors[].
That's the hardest thing about this problem, imo.
Ignore the comment about the third for loop. Your code threw me off a bit.
Last edited by Adak; 10-16-2011 at 09:02 PM.
Array[Index];
doors[index]
Your index for the doors array needs to be a multiple of studentNumber * a multiplier, in the inner for loop.
You can use just doors[studentNumber * n] = Open or 1 or Closed or 0.
Check out the pattern:
Student number is 3:
3 * 1 first door he visits -- n equals 1
3 * 2 second door he visits-n equals 2
3 * 3 third door he visits, etc.
3 * 4 fourth door he visits
etc.
Last edited by Adak; 10-16-2011 at 09:08 PM.