---
# System prepended metadata

title: Mario

---

<h1><a data-id="" href="#mario">Mario</a></h1>

<a data-id="" id="getting-started" style="top: 0px;"></a><h2><a data-id="" href="#getting-started">Getting Started</a></h2>

<p>Open <a href="https://code.cs50.io/">VS Code</a>.</p>

<p>Start by clicking inside your terminal window, then execute <code class="language-plaintext highlighter-rouge">cd</code> by itself. You should find that its “prompt” resembles the below.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$
</code></pre></div></div>

<p>Click inside of that terminal window and then execute</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>wget https://cdn.cs50.net/2021/fall/psets/1/mario-less.zip
</code></pre></div></div>

<p>followed by Enter in order to download a ZIP called <code class="language-plaintext highlighter-rouge">mario-less.zip</code> in your codespace. Take care not to overlook the space between <code class="language-plaintext highlighter-rouge">wget</code> and the following URL, or any other character for that matter!</p>

<p>Now execute</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>unzip mario-less.zip
</code></pre></div></div>

<p>to create a folder called <code class="language-plaintext highlighter-rouge">mario-less</code>. You no longer need the ZIP file, so you can execute</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>rm mario-less.zip
</code></pre></div></div>

<p>and respond with “y” followed by Enter at the prompt to remove the ZIP file you downloaded.</p>

<p>Now type</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd mario-less
</code></pre></div></div>

<p>followed by Enter to move yourself into (i.e., open) that directory. Your prompt should now resemble the below.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>mario-less/ $
</code></pre></div></div>

<p>If all was successful, you should execute</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ls
</code></pre></div></div>

<p>and see a file named <code class="language-plaintext highlighter-rouge">mario.c</code>. Executing <code class="language-plaintext highlighter-rouge">code mario.c</code> should open the file where you will type your code for this problem set. If not, retrace your steps and see if you can determine where you went wrong!</p>

<a data-id="" id="world-1-1" style="top: 0px;"></a><h2><a data-id="" href="#world-1-1">World 1-1</a></h2>

<p>Toward the end of World 1-1 in Nintendo’s Super Mario Brothers, Mario must ascend right-aligned pyramid of blocks, a la the below.</p>

<p><img src="pyramid.png" alt="screenshot of Mario jumping up a right-aligned pyramid"></p>

<p>Let’s recreate that pyramid in C, albeit in text, using hashes (<code class="language-plaintext highlighter-rouge">#</code>) for bricks, a la the below. Each hash is a bit taller than it is wide, so the pyramid itself will also be taller than it is wide.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>       #
      ##
     ###
    ####
   #####
  ######
 #######
########
</code></pre></div></div>

<p>The program we’ll write will be called <code class="language-plaintext highlighter-rouge">mario</code>. And let’s allow the user to decide just how tall the pyramid should be by first prompting them for a positive integer between, say, 1 and 8, inclusive.</p>

<p>Here’s how the program might work if the user inputs <code class="language-plaintext highlighter-rouge">8</code> when prompted:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ ./mario
Height: 8
       #
      ##
     ###
    ####
   #####
  ######
 #######
########
</code></pre></div></div>

<p>Here’s how the program might work if the user inputs <code class="language-plaintext highlighter-rouge">4</code> when prompted:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ ./mario
Height: 4
   #
  ##
 ###
####
</code></pre></div></div>

<p>Here’s how the program might work if the user inputs <code class="language-plaintext highlighter-rouge">2</code> when prompted:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ ./mario
Height: 2
 #
##
</code></pre></div></div>

<p>And here’s how the program might work if the user inputs <code class="language-plaintext highlighter-rouge">1</code> when prompted:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ ./mario
Height: 1
#
</code></pre></div></div>

<p>If the user doesn’t, in fact, input a positive integer between 1 and 8, inclusive, when prompted, the program should re-prompt the user until they cooperate:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ ./mario
Height: -1
Height: 0
Height: 42
Height: 50
Height: 4
   #
  ##
 ###
####
</code></pre></div></div>

<p>How to begin? Let’s approach this problem one step at a time.</p>

<a data-id="" id="walkthrough" style="top: 0px;"></a><h2><a data-id="" href="#walkthrough">Walkthrough</a></h2>

<div class="ratio ratio-16x9" data-video=""><iframe allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" class="border" data-video="" src="https://www.youtube.com/embed/NAs4FIWkJ4s?modestbranding=0&amp;rel=0&amp;showinfo=0" scrolling="no" id="iFrameResizer0" style="overflow: hidden;"></iframe></div>

<a data-id="" id="pseudocode" style="top: 0px;"></a><h2><a data-id="" href="#pseudocode">Pseudocode</a></h2>

<p>First, execute</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd
</code></pre></div></div>

<p>to ensure you’re in your codespace’s default directory.</p>

<p>Then, execute</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd mario-less
</code></pre></div></div>

<p>to change to your <code class="language-plaintext highlighter-rouge">mario-less</code> directory.</p>

<p>Then, execute</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>code pseudocode.txt
</code></pre></div></div>

<p>to open the file called <code class="language-plaintext highlighter-rouge">pseudocode.txt</code> inside that directory.</p>

<p>Write in <code class="language-plaintext highlighter-rouge">pseudocode.txt</code> some pseudocode that implements this program, even if not (yet!) sure how to write it in code. There’s no one right way to write pseudocode, but short English sentences suffice. Recall how we wrote <a href="https://docs.google.com/presentation/d/1X3AMSenwZGSE6WxGpzoALAfMg2hmh1LYIJp3N2a1EYI/edit#slide=id.g41907da2bc_0_265">pseudocode for finding someone in a phone book</a>. Odds are your pseudocode will use (or imply using!) one or more functions, conditionals, Boolean expressions, loops, and/or variables.</p>

<details><summary>Spoiler</summary><p>There’s more than one way to do this, so here’s just one!</p>

<ol>
  <li>Prompt user for height</li>
  <li>If height is less than 1 or greater than 8 (or not an integer at all), go back one step</li>
  <li>Iterate from 1 through height:
    <ol>
      <li>On iteration <em>i</em>, print <em>i</em> hashes and then a newline</li>
    </ol>
  </li>
</ol>

<p>It’s okay to edit your own after seeing this pseudocode here, but don’t simply copy/paste ours into your own!</p></details>

<a data-id="" id="prompting-for-input" style="top: 0px;"></a><h2><a data-id="" href="#prompting-for-input">Prompting for Input</a></h2>

<p>Whatever your pseudocode, let’s first write only the C code that prompts (and re-prompts, as needed) the user for input. Open the file called <code class="language-plaintext highlighter-rouge">mario.c</code> inside of your <code class="language-plaintext highlighter-rouge">mario</code> directory. (Remember how?)</p>

<p>Now, modify <code class="language-plaintext highlighter-rouge">mario.c</code> in such a way that it prompts the user for the pyramid’s height, storing their input in a variable, re-prompting the user again and again as needed if their input is not a positive integer between 1 and 8, inclusive. Then, simply print the value of that variable, thereby confirming (for yourself) that you’ve indeed stored the user’s input successfully, a la the below.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ ./mario
Height: -1
Height: 0
Height: 42
Height: 50
Height: 4
Stored: 4
</code></pre></div></div>

<details><summary>Hints</summary><ul class="fa-ul">
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>Recall that you can compile your program with <code class="language-plaintext highlighter-rouge">make</code>.</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>Recall that you can print an <code class="language-plaintext highlighter-rouge">int</code> with <code class="language-plaintext highlighter-rouge">printf</code> using <code class="language-plaintext highlighter-rouge">%i</code>.</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>Recall that you can get an integer from the user with <code class="language-plaintext highlighter-rouge">get_int</code>.</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>Recall that <code class="language-plaintext highlighter-rouge">get_int</code> is declared in <code class="language-plaintext highlighter-rouge">cs50.h</code>.</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>Recall that we prompted the user for a positive integer in lecture using a <code class="language-plaintext highlighter-rouge">do while</code> loop in <a href="https://cdn.cs50.net/2021/fall/lectures/1/src1/coins2.c"><code class="language-plaintext highlighter-rouge">mario.c</code></a>.</li>
</ul></details>

<a data-id="" id="building-the-opposite" style="top: 0px;"></a><h2><a data-id="" href="#building-the-opposite">Building the Opposite</a></h2>

<p>Now that your program is (hopefully!) accepting input as prescribed, it’s time for another step.</p>

<p>It turns out it’s a bit easier to build a left-aligned pyramid than right-, a la the below.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#
##
###
####
#####
######
#######
########
</code></pre></div></div>

<p>So let’s build a left-aligned pyramid first and then, once that’s working, right-align it instead!</p>

<p>Modify <code class="language-plaintext highlighter-rouge">mario.c</code> at right such that it no longer simply prints the user’s input but instead prints a left-aligned pyramid of that height.</p>

<details><summary>Hints</summary><ul class="fa-ul">
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>Keep in mind that a hash is just a character like any other, so you can print it with <code class="language-plaintext highlighter-rouge">printf</code>.</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>Just as Scratch has a <a href="https://docs.google.com/presentation/d/1mRIN6EDK92NJJlazpFfBNKhxrAQUUxJOJW0UH7knS0g/edit#slide=id.gee4e5a99f9_0_313">repeat</a> block, so does C have a <a href="https://docs.google.com/presentation/d/1mRIN6EDK92NJJlazpFfBNKhxrAQUUxJOJW0UH7knS0g/edit#slide=id.gee4e5a99f9_0_313"><code class="language-plaintext highlighter-rouge">for</code></a> loop, via which you can iterate some number times. Perhaps on each iteration, <em>i</em>, you could print that many hashes?</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>
    <p>You can actually “nest” loops, iterating with one variable (e.g., <code class="language-plaintext highlighter-rouge">i</code>) in the “outer” loop and another (e.g., <code class="language-plaintext highlighter-rouge">j</code>) in the “inner” loop. For instance, here’s how you might print a square of height and width <code class="language-plaintext highlighter-rouge">n</code>, below. Of course, it’s not a square that you want to print!</p>

    <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  for (int i = 0; i &lt; n; i++)
  {
      for (int j = 0; j &lt; n; j++)
      {
          printf("#");
      }
      printf("\n");
  }
</code></pre></div>    </div>
  </li>
</ul></details>

<a data-id="" id="right-aligning-with-dots" style="top: 0px;"></a><h2><a data-id="" href="#right-aligning-with-dots">Right-Aligning with Dots</a></h2>

<p>Let’s now right-align that pyramid by pushing its hashes to the right by prefixing them with dots (i.e., periods), a la the below.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>.......#
......##
.....###
....####
...#####
..######
.#######
########
</code></pre></div></div>

<p>Modify <code class="language-plaintext highlighter-rouge">mario.c</code> in such a way that it does exactly that!</p>

<details><summary>Hint</summary><p>Notice how the number of dots needed on each line is the “opposite” of the number of that line’s hashes. For a pyramid of height 8, like the above, the first line has but 1 hash and thus 7 dots. The bottom line, meanwhile, has 8 hashes and thus 0 dots. Via what formula (or arithmetic, really) could you print that many dots?</p></details>

<a data-id="" id="how-to-test-your-code" style="top: 0px;"></a><h3><a data-id="" href="#how-to-test-your-code">How to Test Your Code</a></h3>

<p>Does your code work as prescribed when you input</p>

<ul class="fa-ul">
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><code class="language-plaintext highlighter-rouge">-1</code> (or other negative numbers)?</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><code class="language-plaintext highlighter-rouge">0</code>?</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><code class="language-plaintext highlighter-rouge">1</code> through <code class="language-plaintext highlighter-rouge">8</code>?</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span><code class="language-plaintext highlighter-rouge">9</code> or other positive numbers?</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>letters or words?</li>
  <li data-marker="*"><span class="fa-li"><i class="fas fa-square"></i></span>no input at all, when you only hit Enter?</li>
</ul>

<a data-id="" id="removing-the-dots" style="top: 0px;"></a><h2><a data-id="" href="#removing-the-dots">Removing the Dots</a></h2>

<p>All that remains now is a finishing flourish! Modify <code class="language-plaintext highlighter-rouge">mario.c</code> in such a way that it prints spaces instead of those dots!</p>

<a data-id="" id="how-to-test-your-code-1" style="top: 0px;"></a><h3><a data-id="" href="#how-to-test-your-code-1">How to Test Your Code</a></h3>

<p>Execute the below to evaluate the correctness of your code using <code class="language-plaintext highlighter-rouge">check50</code>. But be sure to compile and test it yourself as well!</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>check50 cs50/problems/2022/x/mario/less
</code></pre></div></div>

<p>Execute the below to evaluate the style of your code using <code class="language-plaintext highlighter-rouge">style50</code>.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>style50 mario.c
</code></pre></div></div>

<details><summary>Hint</summary><p>A space is just a press of your space bar, just as a period is just a press of its key! Just remember that <code class="language-plaintext highlighter-rouge">printf</code> requires that you surround both with double quotes!</p></details>

<a data-id="" id="how-to-submit" style="top: 0px;"></a><h2><a data-id="" href="#how-to-submit">How to Submit</a></h2>

<p>In your terminal, execute the below to submit your work.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>submit50 cs50/problems/2022/x/mario/less
</code></pre></div></div>


</main>