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 COG92 I don't know why I am having such a hard time with this. I have gotten ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by COG92 View Post
    I don't know why I am having such a hard time with this. I have gotten all the other assignments just fine. I think the arrays confuse me. Anyway, if I were to put an if statement inside that for loop won't it print the status of the doors after every student goes through versus the end result?
    Yes it would... which would be a good debugging tool.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by COG92 View Post
    I don't know why I am having such a hard time with this. I have gotten all the other assignments just fine. I think the arrays confuse me. Anyway, if I were to put an if statement inside that for loop won't it print the status of the doors after every student goes through versus the end result?
    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.

  3. #18
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by Adak View Post
    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).
    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);
    }
    Don't make fun of me because of how off I am please :/

  4. #19
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    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++;
    }

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

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

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

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    #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 09:53 PM.

  9. #24
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    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?
    Are you implying the fact the they change from even to odd numbers? If you start at 0 all closed would be considered even numbers, and odd would be considered open. Is that what you were saying?

  10. #25
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by Adak View Post
    #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. Then you'll have all the doors starting off as closed.
    I didn't see you replied. Which third loop were you referring to? Is saying i=s*n the right thing to do? I wasn't sure about that one. I'll switch my loops, but for now I am going to try to analyze it on paper.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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 10:02 PM.

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by COG92 View Post
    Are you implying the fact the they change from even to odd numbers? If you start at 0 all closed would be considered even numbers, and odd would be considered open. Is that what you were saying?
    Sort of... think about a light switch... it's off... you click it 4 times where is it? On or off... Now you click it 7 times... on or off?

  13. #28
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by Adak View Post
    While it's undoubtedly true, a simple if() statement will handle that.

    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.
    Are you saying d[i]=s*n? Or what exactly do you mean when you say the index for the doors array? Thank you both for taking time to walk through this with me. I apologize that it isn't clicking faster.

  14. #29
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    Sort of... think about a light switch... it's off... you click it 4 times where is it? On or off... Now you click it 7 times... on or off?
    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.

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by COG92 View Post
    Are you saying d[i]=s*n? Or what exactly do you mean when you say the index for the doors array? Thank you both for taking time to walk through this with me. I apologize that it isn't clicking faster.
    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 10:08 PM.

Page 2 of 4 FirstFirst 1234 LastLast
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