Thread: Too many loops D:

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    7

    Too many loops D:

    I'm having difficulty with arrays. It's probably an easy thing to understand but I haven't had much time lately to look into it fully.

    My program that I'm trying to make involves arrays but not that much. I'm just having trouble figuring out the loops.

    Here is the assignment:
    Suppose that a row of closed mailboxes are numbered 1 through 150 and that beginning with mailbox 2, we open the doors of all the even-numbered mailboxes. Next, beginning with mailbox 3, we go to every third mailbox, opening its door if it is closed and closing it if it is open. We repeat this procedure with every fourth mailbox, then every fifth mailbox, and so on. Write a program to determine which mailboxes will be closed when this procedure is completed.


    I know there are a good amount of loops. I have to have a for loop within another for loop. But the other loops I'm having trouble figuring out.

    He showed us on the board in class some stuff that should be in the program to help us out but yet I'm still having some trouble.

    Code:
    bool mailbox; [151]
    mailbox = false;
    mailbox = true;
    mailbox != mailbox
    
    for (j=2; j <=150; j++)
    for (i = j; j <= 150; i +=j)
    I probably copied some of this wrong. But those should be about what the for loop within another for loop should look like. Pretty much I'm confused about this in general. If anyone could help me in the right direction, that would be great.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Somewhat...
    Code:
    for (j=2; j <=150; j++)
    for (i = j; i <= 150; i +=j)
    Other than that, you'll need a loop to initialize your array to all true/false depending on what you want open/closed to represent. So, only 3 loops really, no "other loops" besides those. I'd probably use a bitset and call the flip member function to do this:
    Code:
    #include <bitset>
    
    ...
    
    std::bitset<150> mailbox;
    
    for( ... )
        for( ... )
            mailbox.flip(...);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I have to have a for loop within another for loop.
    These are called "nested loops", but I don't think you need nested loops.

    You can run the 3 loops sequentially (one after another).

    It looks like the assignment is about 2 things:

    1- Starting a loop at something other than i=0, or i=1.
    2- Incrementing more than one at a time... Instead of i++, you can use i=i+3, etc.

    And, don't forget that arrays begin at element 0. You can make an array of 151 mailboxes and ignore Box[0], or you can make Mailbox1 equal to Box[0].

    I suggest you start-out with just one loop. Make sure that's working, and add the 2nd loop. When that works, make the 3rd loop. And, I suggest you "simulate" the program by hand to make sure it's working correctly.... You can write the numbers 1-150 on a piece of paper. Loop through all of the numbers, putting a check-mark by all of the open mailboxes.

    P.S. don't try using bitset unless you've studied it already (probably not). Just use a type bool for open/closed. Or, if you haven't studied bool, use an int.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    Quote Originally Posted by hk_mp5kpdw View Post
    I'd probably use a bitset and call the flip member function to do this:
    Code:
    #include <bitset>
    
    ...
    
    std::bitset<150> mailbox;
    
    for( ... )
        for( ... )
            mailbox.flip(...);

    Yeah we didn't learn about bitset yet. What doug said about bool and int is what we should've used.

    I finished it and submitted this online. I actually tried with the 4 loops and got it although, again, probably could've done it with less but program is pretty small so it's ok.

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	cout << "The following mailboxes are closed" << endl;
    	int a,b,c;
    
    	bool mailbox[151];
    	bool close = false;
    	bool open = true;
    
    	for (c = 0; c <= 150; c++) {
    		if (mailbox[c] = close)
    			close = close;
    		else
    			open = close;
    	}
    		
    	for (b=2; b <= 150; b++) {
    		for (a=b; a <= 150; a += b) {
    			mailbox[a] =! mailbox[a];
    		}
    	}
    
    	for (a = 1; a <=150; a++) {
    		if (mailbox[a] == false)
    			cout << a << endl;
    	}
    return 0;
    }

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    for (c = 0; c <= 150; c++) {
        if (mailbox[c] = close)
            close = close;
        else
            open = close;
    }
    What is that supposed to do? Are you trying to initialize the array and set everything closed?

    Code:
    mailbox[a] =! mailbox[a];
    Typo?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    for (c = 0; c <= 150; c++) {
        if (mailbox[c] = close)
            close = close;
        else
            open = close;
    }
    What is that supposed to do? Are you trying to initialize the array and set everything closed?

    Code:
    mailbox[a] =! mailbox[a];
    Typo?

    Good question. For some reason though, the program still worked though.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by hk_mp5kpdw View Post

    Code:
    mailbox[a] =! mailbox[a];
    Typo?
    perfectly legal as I see


    Code:
    mailbox[a] = !mailbox[a];
    it is bool, and operation is to set the value to the opposit

    maybe spacing is strange, but compiler will eat it ok...

    if you want to pass != instead - you get the expression that has no output...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM