Like Tree1Likes

C programming assignment (with arrays)

This is a discussion on C programming assignment (with arrays) within the C Programming forums, part of the General Programming Boards category; Originally Posted by CommonTater That's what lines 16 through 19 do for you... They test the state of the door ...

  1. #46
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    That's what lines 16 through 19 do for you...
    They test the state of the door and flip it over from 0 to 1 or 1 to 0...

    No insinuation here... but one might expect you to have known that, having written the code yourself...
    Oh, duh! I can't believe I missed that. I realize you said no insults, but now I feel like a cheater and it makes me want to figure out a different way to solve it and do it that way. If it helps I have learned a lot.

  2. #47
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    CommonTater likes this.

  3. #48
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  4. #49
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    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.

  5. #50
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Ok... I have doors 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100 as open. Is that what you have?

  6. #51
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  7. #52
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by COG92 View Post
    Ok... I have doors 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100 as open. Is that what you have?
    Yep that's what I got ... so post your code then I'll post mine... Hopefully Adak is still on line and can add his too..

  8. #53
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    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);
    }

  9. #54
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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; }

  10. #55
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  11. #56
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Practice improves your problem solving skills a great deal, and programming is all about solving problems, imo.
    Yeppers... that's why they call it "coding a solution"...

  12. #57
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    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!

  13. #58
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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...

  14. #59
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    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...
    Thanks! I plan on working on it for the next couple days so if I have issues I will certainly see if you guys can help!

  15. #60
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome.

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: ISO C++ forbids assignment of arrays
    By JonathanS in forum C Programming
    Replies: 5
    Last Post: 09-19-2011, 11:26 AM
  2. Replies: 3
    Last Post: 04-26-2009, 09:54 AM
  3. assignment of arrays problem
    By HumbuckeR in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2006, 05:25 PM
  4. Assignment of Two-Dimensional Arrays
    By LuckY in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2004, 04:40 PM
  5. Assignment of two char arrays
    By moemen ahmed in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2002, 01:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21