Published in

Star Gazers

# Pyramid Algorithm in C — Mario! 🍄

CS50 has been a great experience so far and learning C has also been fun, as I find many similarities with JavaScript, regarding syntax.

One problem set we had to complete was a ‘Mario’ challenge, where we had to print out `#`‘s in the form of a pyramid, just liked the pyramid style obstacles that Mario and his good ole’ buddies come across on their journey. 🎮

Let’s see what this would look like…

The image above represents 2 pyramids back-to-back and below is the `#` equivalent.

`    #  #   ##  ##  ###  ### ####  ####`

Now, during each problem set in CS50, we have a ‘less comfortable’ and ‘more comfortable’ option and through this blog, I shall explain how to tackle both!

## Boiler Plate Code

Firstly, we start off with the main essentials: headers and main function.

`#include <stdio.h> #include <cs50.h> //This is allowing us to access the functions in the CS50 library (created by the staff)int main(void){}`

## What do we want from the user?

One of the requirements is to prompt the user for an input, which is an integer between 1 and 8 (this figure represents the levels of the pyramid). This means we need to consider a condition so we prompt the user again, if they are to type anything else outside of this range. The best way would be a `do while` loop as follows:

`#include <stdio.h> #include <cs50.h>int main(void){  // Create a variable that stores the user's input  int levels;   do  {    //Prompt the user for the level of stairs    levels = get_int("Stair level: ")  }  while (levels > 8 || levels < 1)}`

## Creating the Pyramid

The way we would be creating the pyramid is via a `for` loop. This is so that, no matter what input the user puts in, the `for` loop can automatically create the pyramid without any extra endeavour or coding. We would do this as follows:

`#include <stdio.h> #include <cs50.h>int main(void){  int levels;  do  {    levels = get_int("Stair level: ")  }  while (levels > 8 || levels < 1)  for (int i = 0; i < levels; i+)  {    // IMPORTANT: We create a variable (I called it spaces) that looks after the amount of empty spaces needed on each level. This is necessary as the left hand pyramid is a right-aligned pyramid, meaning it will have empty spaces before any '#'s    int spaces = levels - (i + 1);    // Create an inner loop that prints out the number of empty spaces needed before a '#'    for (int j = 0; j < spaces; j ++)    {      printf(" ");    }    // Now, we take care of printing out the '#'s by creating another variable, that I have called 'stairs'    int stairs = i + 1;        // Again, we are creating another inner loop but this time printing out the '#'s    for (int x = 0; x < stairs; x++)    {      printf("#");    }      // Start a new line at the end of each stair level    printf("\n")   }}`

This is how we would create a right-aligned pyramid! 📶

So, how does it look when running the programme?

`./mario // Starting the programmeStair level: // Prompt for user to type in a figure between 1 and 8Stair level: 10 // If anything else is typed like '10' then the user would be prompted againStair level:Stair level: 3 // Now a valid integer is inputted and this would create a pyramid of 3 levels like so:    #   ##  ###`

That’s the ‘less comfortable’ problem set tackled, but what about the other half of the pyramid like you showed earlier, Milan? What about the ‘more comfortable’ problem set!? 🤷🏽‍♂️

Well, my friend, let me tell you that we only need to add two more things to our current code…😱

## Pitfall

Another requirement to pass the problem set is to have a consistent empty space (the pitfall) between both right and left-aligned pyramids. There needs to be exactly a consistency of two empty spaces.

So how does this look in our code?

`#include <stdio.h> #include <cs50.h>int main(void){  int levels;  do  {    levels = get_int("Stair level: ")  }  while (levels > 8 || levels < 1)  for (int i = 0; i < levels; i+)  {    int spaces = levels - (i + 1);    for (int j = 0; j < spaces; j ++)    {      printf(" ");    }    int stairs = i + 1;        for (int x = 0; x < stairs; x++)    {      printf("#");    }    // After we have printed each stair level, we need to simply just add two empty spaces after the final '#'    printf("  ");     printf("\n");  }}`

## Left-aligned Pyramid

We reach the final piece of the puzzle 🧩. This time we do not need to focus on any empty spaces before or after the `#`‘s and simply print out a `#` by using another `for` loop and the variable `stairs`, that we created before.

`#include <stdio.h> #include <cs50.h>int main(void){  int levels;  do  {    levels = get_int("Stair level: ")  }  while (levels > 8 || levels < 1)  for (int i = 0; i < levels; i+)  {    int spaces = levels - (i + 1);    for (int j = 0; j < spaces; j ++)    {      printf(" ");    }    int stairs = i + 1;        for (int x = 0; x < stairs; x++)    {      printf("#");    }    printf("  ");    // Final inner loop to handle printing the left-aligned pyramid    for (int a = 0; a < stairs; a++)    {      printf("#");    }    printf("\n");  }}`

How will it look if we now run the programme and type in an input of ‘4’? 🤔

`./mario Stair level: 4    #  #   ##  ##  ###  ### ####  ####`

And we are done! 🎉

I hope this helps you in creating pyramids in C (if you ever need to 👀) and gives you clarity on the logic behind such an algorithm!

--

--

## More from Star Gazers

The place that enthusiastic and stargazer to everything.

## Milan Parmar

Software Engineer 👨🏽‍💻 | Full Stack Web Development 💻 | Smartphone Tech Enthusiast📱