0

Ok, just I found this on edX and I have started to work through it. I'm having trouble getting the for loops for Mario in pset1. Everything I get one to work, the other suddenly stops working. Here are the for loops:

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < spaceLength; j++){
        printf("baba ");
    }

    for (int k = 0; k < blockLength; k++){
        printf("#");
    }
    printf("#\n");

    spaceLength--;
    blockLength++;
}

and here is the output:

How high should it be? 8

##
###
####
#####
######
#######
########
#########

Now, before it was doing the spaces, but now it is not. "baba " is there only to make it very visible right now if or where the spaces are.

To what I can see, the order of the for loops is correct and the syntax of the for loops is correct. The first loop just isn't... initializing? Any help would be appreciated.

Andrew

3
  • What's the value ofspaceLength initially?
    – sinister
    Dec 22, 2014 at 4:59
  • What is spaceLength, and blocklength? Hashes? Spaces? Dec 23, 2014 at 17:49
  • int main(void) { int height = 0; int spaceLength = height - 2; int blockLength = 1; I start with the variables and then have a do-while loop before the for-loops.
    – Andrew
    Dec 26, 2014 at 2:13

2 Answers 2

1

First of all, initialize spaceLength and blockLength after the do-while. Otherwise, you end up with -2 spaceLength no matter what the input of user is.

Next, think about spaceLength & blockLength value on each run for each possible input (1, 2, 3 and then some random numbers)

For your program: if input is 2, the spaceLength will be 0 which is equal to j causing the for loop to not run. Work your way from here and you will solve it.

0

What happens if you move: spaceLength--; into the line following printf{"baba ");

Before the curly brace?

You must log in to answer this question.

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