CS50’s 1st Problem Set – Recreating Mario’s Pyramid in C

After creating a project in Scratch, CS50’s students are asked to recreate Mario’s pyramid in C:

Screen Shot 2020-05-10 at 11.30.14.png

I’m going to be honest. This was harder than expected. My previous assignment with Scratch was really intuitive and seeing such rapid progress encouraged me to play around with the features and go beyond the requirements. To complete this new challenge, students have to work in c. No fancy colors, no puzzles, no hints, just code.

The goal was to develop a program in c that prints out Mario’s pyramid. The program should first prompt the user for input (height of the pyramid) and then use it to create a pyramid made of hashes (which would represent the bricks).

Professor Malan had shown us how to use loops and functions in c, which we would need to use to solve the challenge.

In other to tackle this challenge, we were first asked to create a txt file in c with the pseudocode. What is pseudocode? This is one of the things I’ve learnt from this challenge that will stay with me since it is extremely helpful. Pseudocode are the steps (written in plain English) that are needed to reach a goal. In this case, print the pyramid.

This is my pseudocode:

1. Prompt for height
2. If height does not comply with the requirements (in this case positive numbers between 1 and 8), prompt until requirements are met.  Here both a prompt and validation are needed!
3. Create iteration until the number entered by the user is reached -> repeat several times to reach the height.
4. When reaching the end of the iteration, print hashes and spaces and add a new line

The pseudocode helped me to organize the tasks that I had to program. Then, I got to work.

First, I created a new file in c called mario.c. This is where I would write the functions.

Then I had two add the libraries (#include <stdio.h> and #include <cs50.h>). Libraries are a collection of builtin functions that basically define how to execute the code (a set of definitions for commonly used algorithms, data structures, and mechanisms for input and output). stdio.h belongs to the standard library for the C programming language. cs50.h is a library developed ad-hoc for this course.

#include <stdio.h>
#include <cs50.h>

I learnt that we need to define a library so that the compiler knows what to do with things like prompts ( for example, get_int).

Then, I added the int main void frame, which is the beginning of the function definition:

int main(void)
{
}

So with the libraries and the frame in place, the next step was to define how to prompt the user for a height number.

First, I had to define a variable (“n”, which will be the user’s input) and the type (int = integer). Then, I had to add a statement to define when to accept the user’s input (get_int).  In this case, the requirement was that the pyramid should only be created if the input value is a number between 1 and 8. To do so, we were told to use a “do while” statement. This statement basically creates a loop that keeps executing until the condition (input entered is between 1 and 8) is true:

int n;
do
{
n = get_int("Add some text here to prompt user for value");
}
while (add the requirements here);

Then I added some text for the prompt and the conditions. Here I also learnt how logical operators work (&&||, and ,)

int n;
do
{
n = get_int("Enter the pyramid height (allowed values 1 to 8): ");
}
while (n > 8 || n < 1);

I compiled the code and it seemed to work. But then came the tricky part. How to tell C to create a pyramid with hashes? No mean feat (at least for a newbie like me). So as I do with all the challenges I face, I decided to break down the problem into smaller pieces.

  • We need to print hashes and blank spaces. I would call them “space” and “hash” in the code to better identify them
  • The hashes need to be placed on the right
  • A variable is needed to determine how many rows of hashes and spaces would have to be printed (I would call it “i” for integer)
  • The printing should stop as soon as the integer equals the input value entered by the user
  • After each iteration, the value of the “i = number of rows” variable should be increased by 1
  • We would need three loops: one “main” loop to check whether the number of rows reaches the user input and two “minor” loops to print hashes and spaces if the number of rows < user input

Then I tried to write the three loops:

for (this is the loop to check if number of rows < user input and, if not, increase its value by one after each iteration)
{
for (this is the loop for the spaces)
{
printf(" ");
}

for (this is the loop for the hashes)
{
printf("#");
}

Ok, one step closer. Next problem: How to compare the number of rows (i) with the user input value and increase the value by one. The statement below solves this:

int i = 0; i < n; i++

That basically means: the number of rows (i) variable value is 0. Check if 0 is less than the user input. If so, iterate once and increase the value of “i” by one.

Important! I also learnt that, when setting a value, we should always start with 0 and not with 1.

Finally, I had to define the conditions for the hash and space loops:

I needed some extra help here, since I could not figure out the value for the hash variable (which is actually -1 and not 1):

for (int hash = -1; hash < i; hash++)
{
printf("#");
}

The value of hash is -1. Check if the hash variable is less than the i variable. If so, iterate once (the first iteration will always happen) and increase the value of the hash variable by 1.

The opposite should be done for printing spaces:

for (int space = n - 1; space > i; space--)
{
printf(" ");
}

This means, take the user input and deduct 1. If the number of spaces is greater than the number of rows, Decrease the number of spaces by one.

This is the final definition of the function:

for (int i = 0; i < n; i++)
{

// Print hashes
for (int hash = -1; hash < i; hash++)
{
printf("#");
}
printf("\n");
}

// Print spaces
for (int space = n - 1; space > i; space--)
{
printf(" ");
}

My next challenge: greedy algorithms. I’ll keep you posted!

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