I began working on the CS50 course from Harvard a couple of weeks ago. This is a fantastic course, and I have learned a lot already. David Milan is a great instructor and is immensely passionate about the subject. While working through the material, I thought it might be helpful to others if explained my solutions to the problem sets. So, I decided that I would start by showing my solutions to the Mario Less, and Mario More from Pset1.

For Mario Less, I decided to create variables for hold the height for the blocks and the number of spaces and hashes that will be used for each line. The next lines of code show the prompt for the user and sets the condition for the user input. Int this example, the user will be prompted to enter a number between 0 and 23. If the condition is not met, the do loop will execute again, prompting the user to enter a valid height.

The next lines of code implement the nested for loops that are used to construct the blocks in a diagonal manner. The first of the for loops sets the rows. Since the height that the user inputs will determine the number of rows, our incremented value of i will continue until it is equal to the height that the user entered. The next for loop is a little  different. This loop enters the number of spaces per row. As you can see, the number of spaces decrements instead of increments. This is because the number of spaces will decrease by one with each row as it is replaced with a hash. This gives us the diagonal stack of blocks that we are looking to create. The next for loop increments the hashes that will increase with each row. In the first row there will be one hash, the second row two hashes, and so on until the hashes are equal to i + 1. Each time through the main for loop, it will print a carriage return that will start a new row. The final line of this code snippet returns 0, identifying that our function executed successfully. You will see in later examples that, conversely, we will return -1 when there is an error executing our function.

For Mario More, you will notice that I was able to copy most of the code from above. Here, I added one more variable, hashesSecond, that will be used to implement the the second stack of blocks, giving us two diagonal blocks.  I made a small change to the for loop for the hashes, making them increment until they are equal to i. This will make the bottom row of hashes equal to the height. In between the last two for loops, I simply print a space that will give us the separation of the two stacks of blocks that we need. The last step is to create o for loop that will print out the second stack of hashes. Notice that the for loop for hashesSecond is identical to the previous for loop, because we want the stacks to be identical in size.

Now that you have seen my solution, go out and try to solve the problem set on your own. How different are your solutions to the ones that I have shown above? Let me know how you were able to solve the problem set. Remember to share this post if you found it helpful and check out my GitHub account with the link at the bottom of this page. As always…Happy Programming!


  1. Hi. This post helped me understand the problem better but. You have an error on the spaces and hashes formula. The way you currently have it set up if you choose a height of 4 it will print:


    When it should be printing:


    Your formula for spaces should be:

    for (spaces = (height – i) -1; spaces > 0; spaces–)

    This will avoid that extra space you have on every row.

    To have this as per the problems parameters your formula for hashes should be:

    for (hashes = -2; hashes < i; hashes++)

    This will make it so they format as the course intends (with two blocks on top).

    Thank you for all the help. I could not have done this without you.


    1. No worries. I am glad that my post was able to help you understand the problem. Good luck on your CS50 course work. If you find yourself wanting to learn more web development when the course is over, check out my latest posts that are guided tutorials for building interactive web pages. Feel free to reach out if you have any question. Thanks!


  2. This is great! thank you for this, I am just trying to understand this section

    for (spaces = (height – r); spaces > 0; spaces–)

    you have your end statement set to space > 0, struggling to understand this bit and how it fits in?


    1. So, let’s say our user typed in 3 for the height. Now, what we want is 2 spaces followed by a hash, then 1 space followed by two hashes, and finally 3 hashes at the bottom (speaking of the left-hand side of the pyramid only). Since this is nested inside another for loop that is incrementing the i (or the r in your snippet above), we want to decrement our for loop using the spaces–. So, our height is always 3, but the i will increment. The first time through the loop spaces is 3 – 1 = 2. 2 is greater than 0, so spaces will decrement(subtract 1). Thus the next time through loop, we will have 2 – 1 = 1. Still greater than 0, so it decrements again. The next time through it decrements again, 1 – 1 = 0. Since spaces is no longer greater than 0 it exits the for loop. This gives us the 2 spaces at the top of the pyramid, since we went through the loop twice. The next time this for loop executes, i will be 2, so we will only get on space. Then i will be 3, and we will get zero spaces on the bottom row. I hope this makes since. Sorry for the rambling. Keep plugging away!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s