Create the Mario pyramid using C

Wellington Martins dos Santos
4 min readJul 7, 2020

Computer Science 50 — CS50 Harvard University

The main goal here is to create a program that prints out the pyramid on the right side.

On the Youtube channel, CS50 Made easy there is a step-by-step helpful and didactic which I will summarize here in this post.

Understanding the problem

First, enter a number to set how big the pyramid will be.

Height: 1 to 8 — Attention, negative numbers are not possible or bigger than 8. Set a function, do-while loop;

int main(void) //the same idea when clicking in the scratch button

{

int height, row, column, space;

do

{

height = get_int(“HEIGHT: “);

}

// in this line the program will check if it is true

while (height < 1 || height > 8);

Create the pyramid using the for a loop — Set the rows, the columns.

Remember that, the pyramid should be on the right side, therefore, it is necessary to fill up with spaces to take the hashes (#) to the other side of the screen.

for (row = 0; row < height; row++)

{

~~~~for (space = 0; space < height — row — 1;space++)

~~~~{

~~~~~~~~printf(“ “);

~~~~}

~~~~for(column=0; column <= row; column++)

~~~~{

~~~~~~~~printf(“#”);

~~~~}

printf(“\n”);

}

The logic behind the for loop row:

As long as the row is less than height print a new line.

for (row = 0; row < height; row++)

printf(“\n”);

Row 0 is less than 4 so, print one new line.

Row 1 is less than 4 so, print one new line.

Row 2 is less than 4 so, print one new line.

Row 3 is less than 4 so, print one new line.

Row 4 is EQUAL to 4 so, stop here.

Well, in the exercise, it is not required only rows but also hashes. How to do that?

The logic behind the for loop column:

Another loop, nested to the row loop.

~~~~for(column=0; WHAT KIND OF CONDITION SHOULD BE HERE? ; column++)

~~~~{

~~~~~~~~printf(“#”);

~~~~}

Column > Height? — When a column is less than height print #.

The outcome with the Column>Height condition

Height: 4

Column 0 is less than Height, so print #

Column 1 is less than Height, so print #

Column 2 is less than Height, so print #

Column 3 is less than Height, so print #

Column 4 EQUAL to Height, so stop it.

Seems to work, but it’s not a pyramid so a block.

What about Column ≤ Height?

Column 0 is equal to or less than the height (4) — Less #

Column 1 is equal to or less than the height (4) — Less ##

Column 2 is equal to or less than the height (4) — Less ###

Column 3 is equal to or less than the height (4) — Less ####

Column 4 is equal to or less than the height (4) — Equal, STOP IT!

When for loop is checking the condition Column ≤ Height, the formula will be the number of columns equivalent to the number of # +1.

Accordingly to the CS50 Made easy the logic for this loop is different than mine. Once the column loop is nested in the row loop, every time row increases, the column is initialized to 0. For example:

First loop

Row= 0

Column = 0

Column ≤ Row? Yes, so print #

First loop

Row= 0

Column = 1

Column ≤ Row? No, so stop and go to the next loop which initializes in the next row

Second loop

Row= 1

Column = 0

Column ≤ Row? Yes, so, print #

Second loop

Row= 1

Column = 1

Column ≤ Row? Yes, so, print another # — RESULT: ##

Second loop

Row= 1

Column = 2

Column ≤ Row? No, so stop it.

As it is possible to figure it out, there is a pattern that will repeat until reaching the Height number input into the program.

The output for column and row for loop

The logic behind the for loop space:

To convert a pyramid left align to right align is required paces.

How to add spaces to align the pyramid into the right?

The relationship between Height, Spaces, and Rows is:

Height 4, Row 0, 3 Spaces

Height 4, Row 1, 2 Spaces

Height 4, Row 2, 1 Space

Height 4, Row 3, 0 Space.

Height-Row-1 = Number of spaces

Bringing it to the code world, the spaces loop needs come before hashes. As shown in the image, the spaces are before hashes.

for(space = 0; space < height-row-1; space++)

{

prinft(“ ”);

}

There we go!

The output

I hope you understand it!

--

--

Wellington Martins dos Santos

Sports Scientist, Physical Trainer so far but I still wanna be a Fitness Funcional Athlete and programmer — 27 years old