This is what I've done so far for pset 4 (I've started on main, initPaddle & initBricks)

I have trouble printing out the bricks on the screen from the 2nd row onwards; the window doesn't show any bricks on the screen even with a nested for loop, which I can't seem to get why.
My other problem is the color assignment of the bricks. The result of my code is the 1st 5 bricks on the 1st row are of a different color, while the last 5 bricks are black.
enter image description here
I'm guessing there's something wrong with my nested for loop, but I'm not sure what that is.
Any comments on what needs to improve would be greatly appreciated!
(p.s. Are my problems related to what's shown in GDB when I try debugging it? What does "single stepping until exit from function main ... detaching from fork after child process 8213" mean?)
enter image description here


2 Answers 2


This is a great question because it illustrates a very common mistake.

Here is an excerpt from your code:

for (int i = 0; i < ROWS; i++)
    x = 2;
    for (int j = 0; i < COLS; i++)

You have a loop within a loop. The outer loop uses the variable i as an iterator while the inner loop uses j. However, the inner loop is also (incorrectly) incrementing i - it should increment its own variable j. In your case, the inner loop should look like this:

for (int j = 0; j < COLS; j++)
  • While i'm not yet at this pset, the error @Luke pointed out is readily understood. My comment, then: seems like this is a case to perfectly illustrate need for meaningfully naming variables. I'm familiar with the convention to use 'i' and 'j' to shorthand counter variable names. However, especially in the early going, perhaps long-form variable names (even for counters) are helpful. I encountered this in pset-1, mario program, where parsing rows and columns became confusing.
    – mazal
    Commented Jan 30, 2015 at 22:53

From the posted picture, it seems that you don't increment correctly the index variable of your second loop: that gives you only one brick for each color.

Odds are that you used a wrong variable to position your bricks, i.e. you draw 10 bricks of the same color on the same position,. The resultant output is only one brick of each colour.

Instead, you should use the indexes of both for loops to position your bricks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .