This has been a bit, -a brain-teasing challenge for me for maybe two days-, and actually still is. After trying several times to solve this "logically" or "systematically" and unfortunately failing nearly every time, i finally started to improvise and solve by "trial and error" as using nearly every single piece of code i know throughout the process. Finally, the code executed perfectly.


1- Can someone explain the logic of my code, particularly, the simple mathematical operations used inside the "for loops", and the effect of the loops on them each time they get successfully executed as they meet the conditions ?

2- Is using "trial and error" method as i try to figure out the logic and execute a bug-free code, regarded as stupidity in the process of programming ?

3- Are there any particular steps of understanding the logic of something ?

Thanks in advance a lot.

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

int main(void)
int height,rows,spaces,hashes;

printf("Please input numbers between [1-23]\n");

do {
    height = get_int();
while (height<0 || height>23);

for (rows = 0; rows<height; rows++) {

    for (spaces = rows; spaces<height-1;spaces++) {
        printf(" ");


    for (hashes = 0; hashes<rows+2;hashes++) {



Your code looks good. I think you do understand. I would go try the more mario and see if you can make that work.

  • Thank you for trying to help me out. Okay, i'll try to solve the "more-comfortable" version of the problem and see if i can understand what i'm doing Feb 5 '17 at 10:40

The main logic here is relation between spaces and height so that the highest number spaces is height - 1, I'd suggest using debug50 to see it step by step to understand more

You must log in to answer this question.

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