# Thread: Too many loops D:

1. ## 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; 
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. 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(...);``` 3. 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, or you can make Mailbox1 equal to Box.

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. Originally Posted by hk_mp5kpdw 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;
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. 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? 6. Originally Posted by hk_mp5kpdw 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. Originally Posted by hk_mp5kpdw 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... Popular pages Recent additions 