Koren Leslie Cohen

  • About
  • Blog
  • Contact

15 comments

C, Harvard CS50

Mario Pyramid in C

February 22, 2014 by Koren Leslie Cohen

Write a program in C that recreates a half-pyramid using hashes (#) for blocks. Prompt the user for the half-pyramid’s height and align the bottom-left corner of the half-pyramid with the left-hand edge of the terminal window.

/**
 * mario.c
 *
 * Koren Leslie Cohen
 *
 * Creates a Mario Pyramid.
 */

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

int main(void)
{

    // declaring my variables
    int height;
    int all_rows;
    int space;
    int hash;

    // prompts user for integer until integer is 0 to 23
    do
    {
        printf("Welcome to Mario! Please choose a number from 0 to 23:");
        height = GetInt();
    }
    while ((height < 0) || (height > 23));

    // this is the loop that will create the number of rows in the pyramid entered by the user
    for (all_rows = 1; all_rows <= height; all_rows++) 
    {
        for (space = (height - all_rows); space > 0; space--)
        {
            printf(" "); 
        }

        for (hash = 1; hash <= (all_rows + 1); hash++)
        {   
            printf("#"); 
        }

        printf("\n");
    }
    return 0;
}

Result:

mario.c


  • About
  • Latest Posts
Connect
Koren Leslie Cohen
Product manager at Facebook. Former senior product manager at Dollar Shave Club in Los Angeles and software engineer at J.Crew / Madewell in New York City. Recovering trial lawyer.
Connect
Latest posts by Koren Leslie Cohen (see all)
  • PM Career Story - April 28, 2022
  • How to Transition into Product Management - December 26, 2017
  • What I’ve Learned in My First Few Months as a Product Manager - October 14, 2015

Related Posts

Halfway Through Harvard CS50
Breakout Game in C

Share

Facebook Google+ Twitter Pinterest Email

Comments Cancel reply

Your email address will not be published. Required fields are marked *

*

code

  1. Tushar Mittal says

    November 2, 2014 at 3:19 pm

    Thanks, worked like charm.

    Reply
  2. kohl says

    January 12, 2015 at 3:57 pm

    (hash = 1; hash <= (all_rows + 1); hash++)

    if you set int hash equal to 0 instead of one, you don't have to set the limit to all_rows +1

    Reply
  3. mac says

    July 11, 2015 at 7:47 am

    please am new to programming. can some one explain to me the above program and how the for loops work

    Reply
    • That Guy says

      July 29, 2016 at 7:58 pm

      A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

      Reply
  4. Mando says

    September 23, 2015 at 2:22 pm

    For the initialization and conditions of the nested for loops, why subtract height by row? Also what will the original “all_rows” loop do?

    Reply
  5. sarah says

    October 14, 2015 at 2:36 pm

    Thanks for posting this. But please also explain in steps because we are new to these kind of projects,

    Reply
    • That Guy says

      July 29, 2016 at 8:04 pm

      Maybe you should take Intro to computer Science? You take it free here: https://prod-edx-mktg-edit.edx.org/course/introduction-computer-science-harvardx-cs50x

      Reply
  6. Akash says

    November 20, 2015 at 12:09 pm

    can you please explain
    for (hash = 1; hash <= (all_rows + 1); hash++)

    Reply
  7. Lawrence Hardy says

    November 23, 2015 at 4:26 pm

    I am taking this class Harvard CS50 online, I wanted some commentary on my loops. However after reading your code. I would appreciate some information on your Mario For loop.

    Reply
  8. B says

    May 14, 2016 at 11:08 am

    would you consider consulting this violating academic honesty?

    Reply
    • B says

      May 14, 2016 at 12:52 pm

      this class is really hard I feel like i can’t do it without consulting outside sources

      Reply
  9. silvia says

    June 7, 2017 at 3:59 pm

    I regret saying that I gave up developping this code on my own (it’s my first week programming) and copied-pasted yours. Why does it always tell me (in mine) that I have one single error: “mario2.c:7:1: error: expected identifier or ‘(‘ “?? How can I check it?

    Reply
  10. Gautam Bharath says

    May 5, 2020 at 3:47 am

    Hello Koren Leslie Cohen,
    Thank you for this. It is very helpful.

    Reply

Back to Blog

Trackbacks

  1. CS50: Everything you wanted to know about Mario.C and was afraid to ask – Teaching a Goat to Code says:
    November 17, 2016 at 12:19 pm

    […] you betcha I did. The code example I found here was the one that ended up making the most intuitive sense to me. There is no way that I would of […]

    Reply
  2. CS50 Weeks 0-1 – Practical Adulting says:
    June 25, 2018 at 7:53 am

    […] my ability to just make up code, I turned to the internet. Thanks to the lovely Koren Leslie Cohen (http://www.korenlc.com/mario-pyramid-in-c/) I tried out some code and tried to learn some lessons along the […]

    Reply

  • GitHub
  • Instagram
  • LinkedIn
  • RSS
  • Twitter

Looking for something?

Copyright 2023 Koren Leslie Cohen