- This topic is empty.
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
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-less project explained
[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> //since get_int function used int main(void) { int t; do { t = get_int("height"); } while (t < 1 || t > 8); for (int y = t; y > 0; y--) //will take care of changing rows { for (int i = y - 1; i > 0; i--) //will take care of printing blank space as part of loop with no. of blank spaces in each row = height - row no. { printf(" "); } for (int j = 0; j < t - y + 1; j++) //will take care of printing # as part of loop with no. of # = row no. { printf("#"); } printf("\n"); //will take care of changing rows. } }
[/dm_code_snippet]
The assigned values to y, i, and j in loops might need some replacement and might not come in first shot. First check if for say 5 height, you are getting 5 rows. Next, focus on blank space. You might initially replace blank space with . (dot) for better visibility. After getting blank space in correct order, focus on #.[learn_press_profile]