IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 1: [C] – Data Types, Operators, Conditional Statements, Loops, and Command Line › Mario-more project takeaways
Tagged: mario-more project
- This topic is empty.
-
AuthorPosts
-
August 20, 2022 at 6:55 am #438
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include <stdio.h> #include <cs50.h> int main(void) { int h; do { h = get_int("height"); } while (h < 1 || h > 8); for (int y = 0; y < h; y++) { for (int i = h - y - 1; i > 0; i--) { printf(" "); } for (int j = 0; j < y + 1; j++) { printf("#"); } printf(" "); for (int j = 0; j < y + 1; j++) { printf("#"); } printf("\n"); } }
[/dm_code_snippet]
Mario-more project is almost similar to Mario-less. There is perhaps no reason why one cannot handle Mario-more project if completed Mario-less.
Here are the key takeaways:
- Forming nested loop (for loop preferably) that utilizes the height (h) of the pyramid and no. of rows (y) to relate characters (blank space, #) will lead to the solution.
- First focus on blank space in first column. For better visibility while working, you can use dot or any other character instead of blank space. With nested for loop and new line, check if the next row displays what is intended.
- Once you find a formula with for loop that correctly displays blank spaces, next focus on # (bricks).
- Putting two blank spaces on each row is constant and do not need for loop like blank spaces and # earlier.
- Next, paste the for loop that you formed for #. You are done as after #, there will be automatically blank spaces.
The Mario project should help create any shape that is a result of rows and columns. Once a pattern discovered, creating a suitable loop helps achieve the goal. If no pattern is there which can be expressed in terms of relationship between columns and rows, then of course, it negates the use of loop, thereby making the task more manual. In the Mario problem, the pyramid can be expressed in terms of a pattern which is a function of two characters: blank space and #. For each subsequent row, there is a fixed increase/decrease in blank space/#. The important thing here is not a particular shape such as square or pyramid but a pattern that can be expressed in terms of a functional relationship between rows and columns.
[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.