DEV Community

Cover image for CS50 Mario Problem set1 Solution
cesaruseche18
cesaruseche18

Posted on • Updated on

CS50 Mario Problem set1 Solution

I graduated a few months back from the 4Geeks Academy Full Stack Development Bootcamp, it was a great experience.

I have to say that attending a bootcamp helped me improve my coding skills quite a lot even though I had 10 months of previous experience as a Front End developer at the company I currently work for, I was able to gain a lot of knowledge in Back End development as well as Front End development.

After graduating the bootcamp and building a few Full Stack apps and decided to learn more about computer science, and I decided to enroll in the CS50 Computer Science Course from Harvard. I have to say that the classes are amazing the assignments are quite challenging and very interesting. I have to say that I'm learning quite a lot.

When I was doing the first week Lab and Problem set I decided that I will share my solutions on my blog and here I'm sharing and explaining my solution for the Mario Problem set #1.

Mario Problem set 1

Recreate the Mario Pyramid in C, albeit in text, using hashes (#) for bricks, a la the below. Each hash is a bit taller than it is wide, so the pyramid itself is also be taller than it is wide.

My Solution

It is good practice to create a pseudocode.txt file before you start coding any solution. This will help you to visualize and think for the solution of the problem before jumping to your IDE. Here I'm sharing my pseudocode.txt file.

1 - First I need to ask the user to input the height 

2 - The user needs to input a height between 1 and 8. No less than 1 or greater than 8

3 - If the user input it's not between 1 and 8 it reprompts the user to step one and ask the user to input a new height 

4 - The program iterates from 1 through the height that it was input by the user

4.1 - For loop to create the height of the row

4.2  // for loop create the width of the pyramid

4.3 - On that iteration i%\n prints the integer hashes on a new line 

Enter fullscreen mode Exit fullscreen mode

Create a mario directory and inside create a mario.c file then write only the C code that prompts (and re-prompts, as needed) the user for input. The user must input a value between 1 and 8.

If the user inputs a value less than 1 or greater than 8 the user must be re-prompt until it inputs a value between 1 and 8. For this we will create a "do while loop".

int main(void)
{
    int height;

    //Asking the user to input a Height
    do
    {
    height = get_int("Height: \n");
    }

     // The user can only input a Height between 1 and 8
    while 
    ((height < 1) || (height > 8)); 
}
Enter fullscreen mode Exit fullscreen mode

Then you will need to do a for loop for the rows that will iterate from 1 through 8 depending on the input value from the user, and inside of the brackets you will need to create another for loop for the columns of the pyramid that will determine the width of the pyramid.

  // for loop to create the height of the row
    for (int row = 0; row < height; row++)
    {
        // for loop create the width of the pyramid
        for (int column = 0; column < height; column++)
        {
          // if else statement 
        }
    }
Enter fullscreen mode Exit fullscreen mode

Finally, you will need to create and if else statement and print the result that will do the following:

  • If row plus column is greater than or equal than height -1 space print the hashes (to create the hashes)

  • Else add blank spaces to create the Pyramid form.

 {
            // if row plus column is greater than or equal than height -1 space print the hashes
            if (row + column >= height - 1) 
                printf("#");

            // adding spaces
            else 
            printf(" ");
 }
        printf("\n");
Enter fullscreen mode Exit fullscreen mode

Here I'm leaving my full solution.

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

int main(void)
{
    int height;

    //Asking the user to input a Height
    do
    {
    height = get_int("Height: \n");
    }

     // The user can only input a Height between 1 and 8
    while 
    ((height < 1) || (height > 8)); 

    // for loop to create the height of the row
    for (int row = 0; row < height; row++)
    {
        // for loop create the width of the piramid
        for (int column = 0; column < height; column++)
        {
            // if row plus column is greater than or equal than height -1 space print the hashes
            if (row + column >= height - 1) 
                printf("#");

            // adding spaces
            else 
            printf(" ");
        }
        printf("\n");
    }
}
Enter fullscreen mode Exit fullscreen mode

I hope this solution will help you to understand better the problem and how to solve it.

I will encourage anyone who is thinking about starting your coding career to check out the Harvard CS50 program certificate, it will give you a great tools to start your career and also the assignments and projects are very challenging and interesting.

Follow me on Github & Connect with me on LinkedIn

https://github.com/cesareuseche
https://www.linkedin.com/in/cesar-useche-profile/

Top comments (0)