0

I managed to complete the hacker version of the mario.c problem in pset1 using "for" loops. However, when I re-tried the problem using "if" and "else if", I could not achieve similar results. Here is my code:

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    printf("Please enter a number\n");
    int h = GetInt();
    int i = 0;
    if (h > 0)
    {
        for (int x = 1; x <= h; x++)
        {
            if (i <= h-x)
            {
                printf (" ");
                i++;
            }
            else if (h-x < i && i <=h)
            {
                printf ("#");
                i++;
            }
            else if (i == h+1)
            {
                printf ("  ");
                i++;
            }
            else if (h+1< i && i <= h+x)
            {
                printf ("#");
                i++;
            }
            printf ("\n");
        }
    }
}

My logic is as follows: h is the height input from the user, x is the row as measured from the top of the pyramid and i is the block (either whitespace or hex) from the left. The first "if" condition is that a whitespace is added and i is incremented by one when it is less than h-x, until it is equal to h-x+1 when a hex is added. The same logic is followed until both sides of the row are finished, when x then is incremented by one.

However, when I input a value of, say, "5", I obtain 3 whitespaces and 2 hexes only, each one occupying a new line! Could someone explain why this code does not work? Thank you!

1 Answer 1

1

I would pick small numbers for h (e.g., 1 and 2), determine what the expected results should look like and follow the logic of my code using a pencil and a paper.

for example, when h == 1, I should get:

# #

let's see what happens following the logic of your code!

first, if (h < 0) seems to be redundant because the condition of the for loop basically tests for the same thing. that is, both, the if condition and the continuation condition of the for loop are only true when h is at least 1.

next, the first if condition inside the for loop in this case will evaluate to true because h - x would be 0 which is the same value of i at this point, so we would have a space printed out, which we shouldn't. so there is a problem.

unfortunately, this is not the only problem in your code, but following the same approach, you should be able to find and fix all of your problems eventually.

I also have other takes on the code. for example, your variables should have more descriptive name, you should avoid repeating the same operation over and over (e.g., h - x), your code should be easier to understand (e.g., regardless of the correctness of the logic, I shouldn't have to stare at your code for a few minutes to try to understand what h - x < i && i <= h tests for), and so on.

1
  • As an addition to what @Karrem I think that your main problem is that you have confused what if and for can do. for initiates a loop, while if initiates a fork in your code. You can't substitute the one with the other.
    – ChrisG
    Jan 22, 2017 at 16:12

You must log in to answer this question.

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