# CS50 PSet 1: Mario-more Solution

When you start taking CS50, Hello, Mario (less and more), cash, and/or credit are some of the first questions that you will have to submit. For the Mario problem, you can either choose less comfortable or more comfortable. In this article, I am going to share with you how you can solve the Mario-more problem. Remember though about the academic honesty policy: you cannot copy a code from the internet, otherwise, your certificate can be revoked even years after you take the class. Use this only to understand the problem and the way to solve it. Here’s how to solve the Mario-more problem in CS50.

Our goal is to print a double pyramid like this

It would appear in the terminal as

``````\$ ./mario
Height: 8
#  #
##  ##
###  ###
####  ####
#####  #####
######  ######
#######  #######
########  ########``````

Things to keep in mind:

• Height cannot be negative.
• Height should range from 1-8.
• First half of the pyramid should be right aligned, and the other half left aligned.

To build the pyramid, we have to do the following things.

1. Prompt the user for the height of the pyramid, and store it in a variable int.
2. Check if it’s a vallid height. If not, promt again.
3. Use the height to print the pyramid one line after another.

First, we will prompt the user for the height of the pyramid and check if it’s a valid input. To do so, we will use a do/while loop. The reason we are using this loop is to prompt the user for the height first and then check its validity. Here’s the code you can use for it:

``````int height;

// Get height
do
{
height = get_int("Height: ");
}
while (height < 1 || height > 8);``````

As you can see, I first declared an integer called height. Then I prompted the user for the height and stored it using `get_int()`function. After we have a height from the user, we will check if it’s a valid height. We will do so using the while() function such as `while(height < 1 || height > 8);`.

If the user enters a number less than 1 or greater than 8, the loop will prompt the user again for the height. We don’t have to worry about if the user enters a character, symbol, or string. If the user does so, the condition  `while()` will not be satisfied and the loop will prompt the user again. After we have a valid height from the user, we can use it to build the pyramid.

To build the pyramid, we will proceed row-wise. First, we will build the top row in the following order: 7 spaces ( ), 1 hash (#), 2 spaces, and 1 hash (#). In the next line, we will have the following order: 6 spaces ( ), 2 hashes (#), 2 spaces, and 2 hashes (#). The order will continue till the desired number of rows is printed. The pyramid should look like this:

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

Alright!

To begin, we will build the first row with only 2 hashes (#) and 2 spaces between them. To do so, we will use the height entered by the user in the following manner:

``````// For each new row
for (int i = 1; i < (height + 1); i++)
{
// For each row's bricks
for (int j = 1; j < (height + 1); j++)
{
if (j <= (height - i))
{
//Print blank spaces
printf(" ");
}
else
{
// Print the brick
printf("#");
}
}

printf("  ");

for (int k = height; k >= 1; k--)
{
if (k <= (height - i))
{
// Skip for no character at all

}
else
{
// Print the bricks
printf("#");
}
}

// Go to next line
printf("\n");
}``````

The above code will print the pyramid. It will first create a loop that starts from 1. We will begin the loop by 1 so that we can use it to print just as many hashes (#).

For the first half of the pyramid, within the loop, the program will check if j (which begins from 1) is less than or equal to (height-i), where i is the number of the current row. If we are on the first row, it will print all spaces till there is only one space remaining to print the hash. After the first half is printed, it will print two spaces, and then it will print another half of the pyramid.

For example:

In the first row, it will check `if (j <= (height - i))`, i.e. if (4 <= (5-1)). This will be true till the 4th position. Till the loop reaches fourth place. Then the condition will be false and the function in the `else` section will execute, and hence print 1 hash (#), then it will print two blank spaces ( ). Then it will print another hash (#) and move to the next row. Remember: there are no blank spaces after the second half of the pyramid.

After each row, a new line will be printed, and then the condition will be checked again. This process will continue for “height” number of times. This will print the right-aligned pyramid (first half), 2 spaces, and a left-aligned pyramid (second half).

Here’s my entire code:

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

void build(int n);

int main(void)
{
int height;

// Get height
do
{
height = get_int("Height: ");
}
while (height < 1 || height > 8);

// Call build function to build the wall
build(height);
}

void build(int n)
{
// For each new row
for (int i = 1; i < (n + 1); i++)
{
// For each row's bricks
for (int j = 1; j < (n + 1); j++)
{
if (j <= (n - i))
{
//Print blank spaces
printf(" ");
}
else
{
// Print the brick
printf("#");
}
}

printf("  ");

for (int k = n; k >= 1; k--)
{
if (k <= (n - i))
{
// Skip for no character at all

}
else
{
// Print the bricks
printf("#");
}
}

// Go to next line
printf("\n");
}
}``````

I solved the problem by calling a function `build()`. I passed height as an integer to the function to build the pyramid.

This is how you can solve the Mario-more problem of pset1, CS50. If you have any confusion, feel free to drop a comment down below. In the meantime, you can read these articles. 