Thread: C programming assignment (with arrays)

  1. #31
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by COG92 View Post
    Hm...I am not sure what you are getting at. I keep wanting to go back to the even odd thing with your light switch analogy.
    If you know the intial state and count the clicks you can tell if it's on or off...
    That is... if it starts out being off and you click it an even number of times, you know it's off... an odd number of clicks tells you it's on. (This premise is beyond easy to test, unless you don't have lights in your house...)

    Now... how can you apply that to your problem?

  2. #32
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    If you know the intial state and count the clicks you can tell if it's on or off...
    That is... if it starts out being off and you click it an even number of times, you know it's off... an odd number of clicks tells you it's on. (This premise is beyond easy to test, unless you don't have lights in your house...)

    Now... how can you apply that to your problem?
    Isn't that what I said? I am struggling to see how it is different then my even odd thing. Depending on the number of times the door is changed it will tell you if the door is open or closed. For example, if the door starts off closed, and you change it 4 times, it's open and even. I could have sworn that's what I said either. Unless I am wrong again, which with my current luck I probably am.

  3. #33
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No you're not wrong... Perhaps I just read over it too quickly.

    So, the question still remains... how can you apply that to your problem?


    What I'm trying to do is to get you to think in smaller "blobs"... the way the computer does...
    No kidding, that really is the programmer's "big trick"... the ability to take huge things and reduce them to itty bitty little steps...

  4. #34
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    No you're not wrong... Perhaps I just read over it too quickly.

    So, the question still remains... how can you apply that to your problem?


    What I'm trying to do is to get you to think in smaller "blobs"... the way the computer does...
    No kidding, that really is the programmer's "big trick"... the ability to take huge things and reduce them to itty bitty little steps...
    At least I am not wrong, for once. So if we count each time a particular door happens to work with s*n and that number ends up being even the door number is open. Right? Am I close? Like if door 5 shows up 3 times the door is closed. If door 6 shows up 6 times the door is still open.

  5. #35
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    With one if statement in your inner for loop, you have everything you need, and you don't need to count any door visits. If the door is closed when the student gets there, you open the door (set it to 1 let's say). Else, you close the door (because it was already open).

    You're not wrong, but it's not the simplest way.

  6. #36
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by Adak View Post
    With one if statement in your inner for loop, you have everything you need, and you don't need to count any door visits. If the door is closed when the student gets there, you open the door (set it to 1 let's say). Else, you close the door (because it was already open).

    You're not wrong, but it's not the simplest way.
    Logically that makes completely sense. Now, I am just trying to figure out what my for loops are supposed to look like since you said to swap them. I am having trouble figuring out how to make s go up by 1 and then go back to the outer for loop after that.

  7. #37
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by COG92 View Post
    At least I am not wrong, for once. So if we count each time a particular door happens to work with s*n and that number ends up being even the door number is open. Right? Am I close? Like if door 5 shows up 3 times the door is closed. If door 6 shows up 6 times the door is still open.
    Other way round... if the doors are initially closed an even number of students going through will mean it's closed...

    off ... click on...click off...click on... click off ...

    Even numbers always return the intial state.

  8. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by COG92 View Post
    Logically that makes completely sense. Now, I am just trying to figure out what my for loops are supposed to look like since you said to swap them. I am having trouble figuring out how to make s go up by 1 and then go back to the outer for loop after that.

    Enlarge doors[] to 101, and your 1 off problems go away.

    Add <= 100, if you don't have it already, for your for loop stop test.


    Code:
    First for loop, take studNumber from 1 to 100 {
    
       Second for loop takes n from 1 to 100, but stops when studNumber * n > 100 {
      
          if the door is open close it, else open it (it was closed).
       }
    }
    Each door inside the second for loop, will be doors with the index studNumber * n.
    Last edited by Adak; 10-16-2011 at 09:48 PM.

  9. #39
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by Adak View Post
    Enlarge doors[] to 101, and your 1 off problems go away.

    Add <= 100, if you don't have it already, for your for loop stop test.


    Code:
    First for loop, take studNumber from 1 to 100 {
    
       Second for loop takes n from 1 to 100, but stops when studNumber * n > 100 {
      
          if the door is open close it, else open it (it was closed).
       }
    }
    Each door inside the second for loop, will be doors with the index studNumber * n.
    This is what I have right now. Am I on the right track at least?
    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++)
        {
    	    d[s*n]=1;
            for(n=1;(s*n)<=100;n++)
            {   
                if(d[s*n]==1)
                	d[s*n]=0;
                else
                	d[s*n]=1;
            }
            
        }
        return (0);
    }

  10. #40
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes... that is one way to solve it... and it will work...

    However line 13 may sabotage you.

    Now you just need to write the report and see if it gives you the right answers.

    (FWIW... I did it a slightly different way...)

  11. #41
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    With the current code I have the value of d[i] never really changes, which means I can't print d[i]. And the values for d[s*n] are either one or zero. Needless to say I am not sure what to print.

  12. #42
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by COG92 View Post
    With the current code I have the value of d[i] never really changes, which means I can't print d[i]. And the values for d[s*n] are either one or zero. Needless to say I am not sure what to print.
    You need to look at the status, after calculation, of each door... if d[i] == 1, it's open... The reason you don't see the doors changing perminently in the array is that every time through the loop you set it so the door is open on line 13, you need to get that out of there so the program operates on the actual array's status as it changes.

  13. #43
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I agree. Delete line 13. A last simple for loop outside of all the other for loops, and you're done.
    Last edited by Adak; 10-16-2011 at 10:11 PM.

  14. #44
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    I guess I don't fully understand it then. If I delete line 13 how will d[s*n] ever change? Won't it just stay 0 since it never gets set to anything else?

  15. #45
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by COG92 View Post
    I guess I don't fully understand it then. If I delete line 13 how will d[s*n] ever change? Won't it just stay 0 since it never gets set to anything else?
    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...

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, 10:26 AM
  2. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  3. assignment of arrays problem
    By HumbuckeR in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2006, 04:25 PM
  4. Assignment of Two-Dimensional Arrays
    By LuckY in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2004, 03:40 PM
  5. Assignment of two char arrays
    By moemen ahmed in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2002, 12:44 AM