Public Group

# throwing me for loops.

This topic is 2172 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So, I'm trying to code this thing to make a square get divided up into little squares, and number each unit of measurement (one character) after dividing it. Right now it's essentially numbering the whole square as 1, then creating a dividing line and numbering everything after that as 2, then another dividing line and rewriting everything after that as 3... so now there are 3 columns... then it's supposed to go back through each column and rewrite parts of them until eventually you get something like this

1 2 3

4 5 6

7 8 9

or you could expand it to do any number you wanted (any size square).

This is the code:

#include "stdafx.h"
#include "iostream"

int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;

int totalDivisions = 3;

int mapSize = totalDivisions * totalDivisions;
int mapDivider[180][180]; // This creates the array where the leaflets are stored.
bool horiz = false;

//Loop through and decide where the 'leaves' divide, assigning every node in a leaf the same number 1-= through (tD*tD)

//give every node a "1"
for(int a = 0; a < mapSize; a++)
{
for(int b = 0; b < mapSize; b++)
{
mapDivider[a][b] = 1;
}
}

if(!horiz)
{
//divides across one axis, giving each horizontal area a number starting with 2 and climbing.
int lastDivide = 0;
for(int i = 2; i <= totalDivisions; i++)
{
//choose a division line
lastDivide += (mapSize/totalDivisions);

//assign a number to everything above that division line. Overwrite the previous number.
for(int x = 0; x < mapSize; x++)
{
for(int y = lastDivide; y < mapSize; y++)
{
mapDivider[x][y] = i;
}
}
}

//Now that leaf across one axis has a number 1 through tD, divide each leaf tD number of times.
lastDivide = 0; //resets last division point
for (int bigleaf = 1; bigleaf <= totalDivisions; bigleaf++) //Which leaf you are currently dividing into tD number of sections
{
for(int eachDivide = 0; eachDivide < (totalDivisions - 1); eachDivide++) //Divide each leaf into tD minileafs.
{
//choose a new dividsion line.
lastDivide += mapSize/totalDivisions;

//Go through every node to the end, changing only if it has matches the current set.
//Reassign every number above the lastDivide line, rewriting previous numbers.
for(int x = lastDivide; x < mapSize; x++) // go through every x on the map, starting at the divider
{
for(int y = 0; y < mapSize; y++) //change every number above the divider...
{
if(mapDivider[x][y] == bigleaf + (eachDivide * totalDivisions)) //only if it's in the leaf we're working with.
{
mapDivider[x][y] += totalDivisions; // add tD it each time >_>;
}
}
}
}
}
}

for(int i = 0; i < mapSize; i++)
{
for(int j = 0; j < mapSize; j++)
{
cout << mapDivider[i][j] << " ";
}
cout << endl;
}
}


I hope that displays well... I copied it from VSC++2010 and it looks like the tabbing is HUGE.

Anyway, the DESIRED output is

1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
7 7 7 8 8 8 9 9 9
7 7 7 8 8 8 9 9 9
7 7 7 8 8 8 9 9 9


BUT, I am getting this instead:

1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
4 4 4 2 2 2 3 3 3
4 4 4 2 2 2 3 3 3
4 4 4 2 2 2 3 3 3
7 7 7 2 2 2 3 3 3
7 7 7 2 2 2 3 3 3
7 7 7 2 2 2 3 3 3


I've tried a few things... once I neded up with just three columns of 7, 8, and 9. I was expecting it to do that though.

I don't understand why it's not going to the 2 and 3 leaves and dividing those the way it did the 1st column.

The reason I'm doing it this way, btw, is because this is going to expand into something more where the dividing lines are random, so you get n hallways of random length, and in each hallway, n rooms of random depth. But all rooms within a hallway should have the same width, and all hallways should have the same total depth.

So I will essentially just make lastDivide random (within a range that is close to mapSize/totalDivisions, without making any hallway too small).

##### Share on other sites

Ignore some of the weird stuff... like the 180x180. It's a remnant from other stuff I was doing.

EDIT: Sorry for the double post. I'm used to another forum where double posting is OK. Can't figure out how to delete this post.

Edited by nesseggman

##### Share on other sites

Write down what your loops are doing for the entire process with those numbers (or follow step by step in a debugger).

In your "bigleaf" loops, the lastDivide values are "3, 6, 9, 12, 15, 18".

##### Share on other sites

Oh, man. I got it now. Where I was expecting the problem was not at all where the problem actually was.

I am not good at debugging, I forgot to try something like that. Thanks <3

• 12
• 9
• 17
• 13
• 11