An edx.org Harvardx CS Intro Course Homework Problem Question

As the title says, this is for the CS Intro course on edx.org, CS50 (Introduction to Computer Science). It's actually C instead of C++, but I thought I'd ask about it here anyway.

The specs are as follows:
Write, in a file called mario.c in your ~/workspace/pset1 directory, a program that recreates this half-pyramid using hashes (#) for blocks. However, to make things more interesting, first prompt the user for the half-pyramid’s height, a non-negative integer no greater than 23. (The height of the half-pyramid pictured above happens to be 8.) If the user fails to provide a non-negative integer no greater than 23, you should re-prompt for the same again. Then, generate (with the help of printf and one or more loops) the desired half-pyramid. Take care to align the bottom-left corner of your half-pyramid with the left-hand edge of your terminal window, as in the sample output below, wherein underlined text represents some user’s input.
~/workspace/pset1 $ ./mario
height: 8
##
###
####
#####
######
#######
########
#########
Note that the rightmost two columns of blocks must be of the same height. No need to generate the pipe, clouds, numbers, text, or Mario himself.
By contrast, if the user fails to provide a non-negative integer no greater than 23, your program’s output should instead resemble the below, wherein underlined text again represents some user’s input. (Recall that GetInt will handle some, but not all, re-prompting for you.)
~/workspace/pset1 $ ./mario
Height: -2
Height: -1
Height: foo
Retry: bar
Retry: 1
##


The CS50 IDE uses Ubuntu Linux-based OS stuff, so the Terminal from that is also built-in.

This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int height = 0;
    
    // prompt the user for the height of the pyramid as a positive integer no greater than 23
    do
    {
        printf("height: ");
        height = GetInt();
    }
    while (height <= 0 || height > 23 || isalpha(height));
    
    // ouptput tab characters to indent the output, decreasing by one each time through the loop
    // while printing out hash characters to create Mario's half-pyramid
    for (int i = 2; i <= height; i++)
    {
        putchar('#');
        
        for (int j = 29; j > 0; j--)
        {
            putchar('\t');
        }
    }
}


Don't try to run it unless you know how to get to the CS50 IDE, since the CS50 header is built into it internally.

I need to know why the output is all weird. And if someone could help me work through it (on my own - please don't just give me the answer, as that would go against the course's academic honesty policy).

Edit: I was able to get my brother to help me, so I don't need this anymore. Sorry about this.
Last edited on
Topic archived. No new replies allowed.