CS50 Mario (less comfortable) Solution

By Suraj Chaudhary

Are you looking for the solution of Harvard’s CS50 PSet 1? Do you want the CS50 Mario (less comfortable) solution?

If so you’re in the right place.

In this article, I am going to share with you how you can solve the Mario-less problem.

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.

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-less problem in CS50.

Our goal is to print a right-aligned pyramid like this

It would appear in the terminal as:

$ ./mario
Height: 5
    #
   ##
  ###
 ####
#####
$

The things to keep in mind:

  • Height cannot be negative.
  • Height should range from 1-8.
  • Pyramid should be right aligned.

To start building 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 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 that I used 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 go row-wise. First, we will build the top row with only one hash (#) and the rest just blank spaces. Then, we will build the second row and it will have two hashes (#). The pyramid should look something like this:

       #
      ##
     ###
    ####
   #####
  ######
 #######
########

Alright! So to begin, we will first build one row with only one hash (#). To do so, we will use the height entered by the user in the following manner:

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

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

The above code will print only one line of 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 (#).

Then 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.

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 only 1 hash (#).

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.

So how to solve the CS50 Mario (less comfortable) problem and find the solution? Here’s my entire code to do so:

#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 row
    for (int i = 1; i < (n + 1); i++)
    {
        // For each brick
        for (int j = 1; j < (n + 1); j++)
        {
            if (j <= (n - i))
            {
                //Print blank spaces
                printf(" ");
            }
            else
            {
                // Print the brick
                printf("#");
            }
        }

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

OR

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

int main(void)
{
    // take input
    int height;
    do
    {
        height = get_int("Enter height: ");
    }
    while (height < 1 || height > 8);

    // overall height
    for (int i = 0; i < height; i++)
    {
        // for individual rows
        for (int j = 1; j <= height; j++)
        {
            if (j < (height - i))
            {
                printf(" ");
            }
            else
            {
                printf("#");
            }
        }
        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-less 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.

Also Read

  1. CS50: How To Study At Harvard For FREE (and get a free certificate)
  2. How to use Windows 11 on Mac for Free
  3. How To Get A Domain Name For FREE
  4. How To Use Microsoft Office Apps For FREE Legally (Mac & PC)

Suraj Chaudhary is a writer, developer, founder, and a constant learner. He shares lessons and resource to living a fuller life every week. On this blog, he shares helpful guides and helpful articles that help his 70,000+ monthly readers find answers, solve problems, and meet their curious needs.

Leave a Comment

Slide to prove you're not a bot/spammer *