# Thread: C programming assignment (with arrays)

1. ## C programming assignment (with arrays)

I am new to C and i just learned how to use arrays. I have to complete the task below:
There are 100 doors (numbered 1 through 100) and 100 students
(numbered 1 through 100) waiting in a line. Initially, all the doors are closed. A student numbered x passes through all the doors that are multiples of x (i.e., student numbered x goes to doors x; 2x; 3x; ... ) and changes the status of each door, i.e., opens the door if it is closed and closes the door if it is open. After all the students complete their turns, which doors remain open? Write a C program that mimics this experiment and prints the numbers of the doors that remain open at the end.

I am stuck on how to know which door is in what state. This is what I have now.
Code:
```#include <stdio.h>

int main(void)
{
int i,x=0,j,door,k=1,d[100],s[100];

for (i=1;i<100;i++)
d[i]=i;
for(k=1;k<100;k++)
s[k]=k;
k=1;
for(i=1;i<=100 || k==100;i++)
{
j=d[i]%s[k];
for(;i==100;k++)
i=1;
if(j==0)
}
return;
}```
If I pasted the code wrong I sincerely apologize and I will try again. As you can see the code isn't fully finished. I think I am on the right track I just can't figure out how to know what happens to which door. Thanks for any help.

2. Ok... before you start pounding code that you'll just end up trashing... You need to do some preparatory work...

1) ANAYSE the assignment, until you understand it in very small detail... For example; you might pick one student and follow him or her through a set of doors.

2) PLAN a step by step solution, on paper, to the problem based on your newfound level of understanding.

Now you can turn on your computer...

3) WRITE your code working in small sections testing as you go...

4) TEST your code to see if there are any problems that weren't obvius in the planning stage.

All programmers follow a similar process, even for the simplest of programs.
This is necessary because nobody can solve a problem they don't understand.

3. Originally Posted by CommonTater
Ok... before you start pounding code that you'll just end up trashing... You need to do some preparatory work...

1) ANAYSE the assignment, until you understand it in very small detail... For example; you might pick one student and follow him or her through a set of doors.

2) PLAN a step by step solution, on paper, to the problem based on your newfound level of understanding.

Now you can turn on your computer...

3) WRITE your code working in small sections testing as you go...

4) TEST your code to see if there are any problems that weren't obvius in the planning stage.

All programmers follow a similar process, even for the simplest of programs.
This is necessary because nobody can solve a problem they don't understand.
I feel like that is what I have done. I have analyzed the problem. If all the doors start off closed, we take one student through each of the 100 doors, if that student can be divided into the door number the state of the door changes. Once all of the students have gone through all of the doors print the doors that are in the open state. I guess I am missing the planned portion now. I just keep getting stuck. Thanks for the tips though!

4. You keep getting stuck because you don't yet understand the problem. Your statements are sweeping, general in nature... computers can't deal with that. Also take note that *your* description does not match that of your assignment... Not all students go through all doors.

Take any student... lets say #58 ... how many doors does that student go through?

Take any door... precisely what happens when a student goes through it?

And so on... programming is all about the minutia... you have to understand these things *at the computer's level* because it's a sure and certain thing that it's not going to understand it at ours.

5. Originally Posted by CommonTater
You keep getting stuck because you don't yet understand the problem. Your statements are sweeping, general in nature... computers can't deal with that. Also take note that *your* description does not match that of your assignment... Not all students go through all doors.

Take any student... lets say #58 ... how many doors does that student go through?

Take any door... precisely what happens when a student goes through it?

And so on... programming is all about the minutia... you have to understand these things *at the computer's level* because it's a sure and certain thing that it's not going to understand it at ours.
How does my description not match the assignment? I wasn't saying all students go through all doors. I was saying all students "test" to see if they go through the door or not. Is that not the right mentality.

Student 58 would go through 1 door, door number 58.

If a student goes through the door it changes the door depending on if the door is already closed or open.

I really don't understand what is wrong with my logic. Are you saying my logic doesn't fit the computers logic or that my logic is just plain wrong?

6. Originally Posted by COG92
How does my description not match the assignment? I wasn't saying all students go through all doors. I was saying all students "test" to see if they go through the door or not. Is that not the right mentality.

Student 58 would go through 1 door, door number 58.

If a student goes through the door it changes the door depending on if the door is already closed or open.

I really don't understand what is wrong with my logic. Are you saying my logic doesn't fit the computers logic or that my logic is just plain wrong?
Your logic may not be flawed... but it's still at too high a level...

Ask yourself how you knew that #58 only goes through one door...

How do you know which students go through which doors?
How do you know the state of each door before a student goes through?
How is the door changed when a student does go through?

7. Originally Posted by CommonTater
Your logic may not be flawed... but it's still at too high a level...

How do you know which students go through which doors?
How do you know the state of each door before a student goes through?
How is the door changed when a student does go through?
I'm sorry, but I feel like we are just going in circles. We know which students go through which doors based on the multiples. I can't think of an easier way to find a multiple of something besides division. I guess you could take student number 58 and multiply starting at 1 and ending if the next one goes over 100. That seems more complicated to me though.

If you start with a state of closed, once a student goes trough it opens, then another student it closes, and so forth.

I feel like I am getting even more lost than I was before. I really don't know how else to think of this.

8. Ok... what I'm trying to get you to see is the *process* that is at work...

How do we know which students are going through which doors... We are using multiples of the student's number to know which door they go through... So student 1 goes through door 1 X 1 (1), 1 X 2 (2), 1 X 3 (3) etc. Student 2 goes through door 2 x 1 (2), 2 X 2 (4), 2 X 3 (6), etc. And so on...

If we follow this a bit further... We can also observe that no student goes through a door less than their number...

Also since there are only 100 doors no student can go through door any door numbered higher than 100...

Ok, the action of the doors is such that if we have an open door we close it... if we have a closed door we open it...

You may understand this implicitly, but the computer does not... The computer is a total flaming idiot, you have to tell it *every little thing* and you have to tell it what to do in the exact order you want it to do it... Thus you have to be able to think down to it's level...

Ok so now we have some sense of what's happening...

We obviously need to loop through all student numbers
We need to loop through all multiples of their number < 101
We need to track the status of the doors as open or closed
Then we have to make a report.

See how deeper understanding leads you to a process?

9. Originally Posted by CommonTater
Ok... what I'm trying to get you to see is the *process* that is at work...

How do we know which students are going through which doors... We are using multiples of the student's number to know which door they go through... So student 1 goes through door 1 X 1 (1), 1 X 2 (2), 1 X 3 (3) etc. Student 2 goes through door 2 x 1 (2), 2 X 2 (4), 2 X 3 (6), etc. And so on...

If we follow this a bit further... We can also observe that no student goes through a door less than their number...

Also since there are only 100 doors no student can go through door any door numbered higher than 100...

Ok, the action of the doors is such that if we have an open door we close it... if we have a closed door we open it...

You may understand this implicitly, but the computer does not... The computer is a total flaming idiot, you have to tell it *every little thing* and you have to tell it what to do in the exact order you want it to do it... Thus you have to be able to think down to it's level...

Ok so now we have some sense of what's happening...

We obviously need to loop through all student numbers
We need to loop through all multiples of their number < 101
We need to track the status of the doors as open or closed
Then we have to make a report.

See how deeper understanding leads you to a process?
Thanks for all your help. I really appreciate it. I see your point. That method still seems more complicated to me, but I am going to finish the analyzing based on what you said then try some more planning. At this point I don't see how exactly this is going to work, but I am going to work on it some more and see where it gets me. Thanks again for the help.

10. It's a Word Problem! I love the boolean nature of the doors - they're open, or shut, nothing in between.

Since each student will be passing through a bunch of doors, it seems you'd want to have two for loops:

one for the students
and one nested inside that, for the doors each student will interact with.

The studentNumber could just be an int, maybe. The doors have to be an int array (probably best). Then you'll need another int variable for the "multiplier" of the student number.

The first for loop will increment the studentNumber, and the inner for loop will increment the "multiplier", which I'd call 'n'.

The inner for loop will use that studenNumber * n as the index to the doors array: doors[studentNumber * n].

You'll probably want an if statement with an else attached, inside that inner for loop.

And I want to know which doors are open and which are closed, at the end.

It's a Word Problem! I love the boolean nature of the doors - they're open, or shut, nothing in between.

Since each student will be passing through a bunch of doors, it seems you'd want to have two for loops:

one for the students
and one nested inside that, for the doors each student will interact with.

The studentNumber could just be an int, maybe. The doors have to be an int array (probably best). Then you'll need another int variable for the "multiplier" of the student number.

The first for loop will increment the studentNumber, and the inner for loop will increment the "multiplier", which I'd call 'n'.

The inner for loop will use that studenNumber * n as the index to the doors array: doors[studentNumber * n].

You'll probably want an if statement with an else attached, inside that inner for loop.

And I want to know which doors are open and which are closed, at the end.
Thanks for the nice explanation! That is very similar to what I have right now, but not quite. I'll have to change a few things around. The trouble I am having is being about to print the number of the doors that are open at the end of everything. I'll keep thinking and see what happens. I will certainly let you know what the end result is Thanks again!

It's a Word Problem!
FWIW... I have a solution in about 20 lines here, that I can post after our friend finishes his assignment...

13. I couldn't resist writing one myself, but I haven't run it yet, and certainly won't post it, until he's through. I like keeping the suspense going, anyway. Maybe time for a good mystery movie!

It's a pretty short program, yeah. Maybe 25 lines or so. I wrote it in the forum editor, so I don't have a line counter on the side bar.

Cog92: use an if statement, inside your for loop that prints out the door status.

I couldn't resist writing one myself, but I haven't run it yet, and certainly won't post it, until he's through. I like keeping the suspense going, anyway. Maybe time for a good mystery movie!
... just got finished watching "Imposter", nice bit of scifi with a surprise ending.

It's a pretty short program, yeah. Maybe 25 lines or so. I wrote it in the forum editor, so I don't have a line counter on the side bar.
Cog92: use an if statement, inside your for loop that prints out the door status.
I might have gotten it under 20 lines but decided to toss in a little extra...

Anyway we can all compare when the OP finishes his...