# What is wrong with my hacker mario.c code for pset1?

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)
{
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!

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.

• 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. Jan 22, 2017 at 16:12
{"ft_lang_label":"__label__en","ft_lang_prob":0.8860686,"math_prob":0.94093573,"size":1186,"snap":"2022-27-2022-33","text_gpt3_token_len":348,"char_repetition_ratio":0.1142132,"word_repetition_ratio":0.041152265,"special_character_ratio":0.34569982,"punctuation_ratio":0.1380597,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9910402,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-05T15:52:22Z\",\"WARC-Record-ID\":\"<urn:uuid:b95ca637-e90d-4b0b-b2b8-11126a075fff>\",\"Content-Length\":\"105080\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:16a7aecb-78ae-4e0f-9426-466a9eb020eb>\",\"WARC-Concurrent-To\":\"<urn:uuid:e9856df8-28e5-4178-b2e6-b3d2e701274e>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://cs50.stackexchange.com/questions/16532/what-is-wrong-with-my-hacker-mario-c-code-for-pset1\",\"WARC-Payload-Digest\":\"sha1:MOYSZUZFMMSKJ4K34I6XE73RW7TANX7Z\",\"WARC-Block-Digest\":\"sha1:VRBD3CO5OCCMRGJOGH6NXSBPZ3LEWNSI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104585887.84_warc_CC-MAIN-20220705144321-20220705174321-00595.warc.gz\"}"}