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!