#### Archived

This topic is now archived and is closed to further replies.

# For loop initilization problem

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

## Recommended Posts

I have an array which is initilized right away: - //Block array for displaying blocks on screen in row/column order UCHAR blockgrid[NUM_BLOCK_ROWS][NUM_BLOCK_COLUMNS] = { {1,1,1,1,1,1,1,1}, {2,2,2,2,2,2,2,2}, {3,3,3,3,3,3,3,3}, {4,4,4,4,4,4,4,4}, {5,5,5,5,5,5,5,5}, {6,6,6,6,6,6,6,6} }; It does what it is supposed to, draw lines of seperate blocks to the surface, this is a breakout clone just so you get an idea of what I''m trying to do. What I need to do now however because of a problem I''ve encountered and because I want to know how to is to initilize this using a FOR loop. So far I have: - void Init_Blocks(void) { for (int row=0; row < NUM_BLOCK_ROWS; row++) { for (int col=0; col < NUM_BLOCK_COLUMNS; col++) { blockgrid[row][col] = 1; } } } Which fills the columns and row with only one color of block. I have then tried changing this to have it put one line of blue blocks and then one line of yellow blocks etc... However after a day of fiddling and swapping and changing the best I can come up with is to have one line of yellow, one line of blue and then one block colored blue on the third line. Plus to boot I think I''m doing it a lame way: - // Initiliaze the blocks - used instead of an array for example void Init_Blocks(void) { for (int col=0; col < NUM_BLOCK_COLUMNS { blockgrid[row][col] = 1; col++; int row=0; blockgrid[row][col] = 2; row++; blockgrid[row][col] = 2; } } 1 represents a blue block 2 a yellow and so forth. Can someone please tell me what I''m doing wrong here. Cheers, Pk

##### Share on other sites
Is this what u want?

for(row = 0; row < NUM_BLOCK_ROW; row++)  for(col = 0; col < NUM_BLOCK_COL; col++)    blockgrid[row][col] = row;

Edited by - DerekSaw on February 22, 2002 7:39:52 AM

##### Share on other sites
Erm... Yes, I had just sussed it out about a minute earlier, although I went for a slightly different approach LOL.

Initiliaze the blocks - used instead of an array for example
void Init_Blocks(void)
{
for (int col=0; col < NUM_BLOCK_COLUMNS; col++)
{
blockgrid[row][col] = 1;
}

int row = 0;
row ++;

for (col=0; col < NUM_BLOCK_COLUMNS; col++)
{
blockgrid[row][col] = 2;
}

row ++;

for (col=0; col < NUM_BLOCK_COLUMNS; col++)
{
blockgrid[row][col] = 3;
}

row ++;

for (col=0; col < NUM_BLOCK_COLUMNS; col++)
{
blockgrid[row][col] = 4;
}

row ++;

for (col=0; col < NUM_BLOCK_COLUMNS; col++)
{
blockgrid[row][col] = 5;
}

row ++;

for (col=0; col < NUM_BLOCK_COLUMNS; col++)
{
blockgrid[row][col] = 6;
}
}

I can''t believe in the end it was that simple (your code). Well chalk it up to another learning experience

Thanks

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 11
• 21
• 12
• 11
• 11
• ### Forum Statistics

• Total Topics
631405
• Total Posts
2999887
×