Lines 17 and 19 handle it. It's so simple once you see it.
You will have several of these moments of < facepalm > with C. If you don't, you're not learning anything new.
I don't think you're cheating... well, not unless you tell me you "borrowed" someone else's code... We call that "scoop and poop coding" mostly because that's what the end result usually is... poop ...
Not to worry... I figure you just got a little too deep into experimental code...
I've always found programming to be a deeply interresting challenge. Every new project brings it's own set of questions and problems and for me it's just about the best puzzle solving exercise on the planet. Being self-educated, I can attest to how much you learn and how well you remember when you're struggling for every character...
Keep going... finish your assignment... then I'll show you how I solved it...
And yes, I used my little 4 step plan, just like always.
Thanks for that! I really enjoy the problem solving aspect of programming, and I usually try to figure it all out on my own. This time after working for several hours and getting a little too close to the due date I decided to seek help. You both have been a huge help and I greatly appreciate it. I really hope I can adapt your 4 step plan because I think it would really help and it would keep me from getting so frustrated.
I am still having issues figuring out the printing, but hopefully I can figure it out. I look forward to seeing how you solved it.
Ok... I have doors 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100 as open. Is that what you have?
Yeah, the last thing you want to do is to start coding before you even know what you're writing. It's always a good idea to spend some time thinking it over before you start. Of course on complex programs, you write piles and piles of notes. The one thing that never happens in real world programming is that you hand a programmer an assignment and he heads on over and starts typing...
As I'm sure my friend Adak would agree... C is easy... Programming is hard.
That makes complete sense. And it is probably a good idea to get in that habit now, even if the codes are fairly small. I am starting to realize how C would be easy and programming would be hard. Lol.
Here is my code:
Code:#include <stdio.h> int main(void) { int i,s,d[101],n=1; for (i=0;i<=100;i++) d[i]=0; for(s=1;s<=100;s++) { for(n=1;(s*n)<=100;n++) { if(d[s*n]==1) d[s*n]=0; else d[s*n]=1; } } for(i=0;i<=100;i++) if(d[i]==1) printf("%d\t",i); return (0); }
Looks pretty good...
Here's mine, but I tossed in a little extra...
Code:#include <stdio.h> int main (void) { char doors[101] = {0}; // doors closed int stu, dor, tmp; // for counters printf("100 students, 100 doors...\n"); // calculate door status for (stu = 1; stu < 101; stu++) for (dor = 1; (dor * stu) < 101; dor++) doors[dor * stu]++; printf("The following doors would be open...\n"); for (tmp = 1; tmp < 101; tmp++) if (doors[tmp] & 1) printf("%d\t",tmp); printf("\n\nEach door was entered [times] ...\n"); for (tmp = 1; tmp < 101; tmp++) printf("%d [%d]\t",tmp,doors[tmp]); printf("\n\n"); return 0; }
That's what I got, also. I have not analyzed it to see if it's correct or not though. I believe it is, but that's as far as I can go to affirm the accuracy.
Yes, programming is a lot more than using the language of your choice. I have spent more than a week, working on a problem by hand, (an hour or two each day), before I saw the pattern I needed to program, and could put it into the small steps that the program could use.
Practice improves your problem solving skills a great deal, and programming is all about solving problems, imo. It's like any other language or sports skill. You want to be good at it, you practice, practice, and practice.
Nice code CommonTater! I like the added part that mentions how many times the door was opened. Thank you both for all the help, tips, and advice! I really appreciate it! Now, I get to go work on the second problem that's due This time I will make sure to do the 4 step process! And...I will continue to practice! Hope you both have a good one!
No worries... It's been fun.
If you get stuck on the new one, just post up your code and we'll see what we can do...
You're welcome.