# CS50 pset-1 itsa Mario - make a half-pyramid using parameters from user input

I'm taking Harvard's introduction to computer science (CS50) course online, and I've written a program that satisfies a coding challenge but I don't understand what I did or how I can improve it. I'm a very green programmer.

The challenge is to get an int from the user between 0 and 23 and use that to set the line height of a half-pyramid drawn in the terminal using hashes, ala the ramp at the ends of the levels in Super Mario Bros:

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


After about 6 hours of banging my head against the wall I came up with the following:

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

int main()
{
int height;

printf("How high do you want your half-pyramid?\n");
height = GetInt();

//verify that height is an int between 0 and 23

while (height > 23 || height < 0)

{
printf("Invalid entry:\nChoose a number between 0 and 23\n");
height = GetInt();
}

// draw the pyramid

for (int line = 0; line < height; line++)
{
// draw spaces
for (int space = line + 2; space < height + 1; space++)
{
printf(" ");
}

// draw hashes
for (int hash = line + height + 1; height < hash + 1; hash--)
{
printf("#");
}
printf("\n");
}

return 0;
}


I understand the part that I've commented as "verify that height is an int between 0 and 23," but everything in the "draw the pyramid" section was arrived at by brute force. Just making one thing that kind of looked like it worked and then tweaking it over and over again until it did what I needed it to do. Maybe this is how everyone codes, but I hope not.

Can anyone give me some insight in fairly plain language about what the code here is doing, some ideas about how it might be improved, or just some other ways to go about it?

• Welcome to Code Review! At one time or another we have all had to "hack" some code until it worked, usually before knowing what the good techniques/methods are. I hope you get some good answers! – Phrancis Mar 12 '16 at 18:16

### Organized coding

A line of a pyramid is made by x spaces and y hashes. The number of spaces is given by the total line length minus the number of hashes. The number of hashes increases from 2 to 5, the max line length is 5

static int LINE_LEN = 5;
for (int hashes = 2; hashes <= LINE_LEN; hashes++) {
print_repeated_char(' ', LINE_LEN - hashes);
print_repeated_char('#', hashes);
printf("\n");
}


Also note the print_repeated_char function avoids repetition.

• I've tried to rebuild the program substituting your code snippet but at compile I get this error: error: implicit declaration of function 'print_repeated_char' is invalid in C99 [-Werror,-Wimplicit-function-declaration] Any ideas why? Is there a library I need to add? – RobertE Mar 13 '16 at 2:45
• @RobertE I meant it was on You to write that function. Just use a for loop to call printf repeadately on the char. – Caridorc Mar 13 '16 at 9:16
• Sorry but I have no idea what you mean. This is my 3rd program. I tried to create a variable char print_repeated_char and got a different error: called object type 'char' is not a function or function pointer. If what you've written above is not the complete code for whatever it is you're explaining I don't know whatever it is that's needed to fill in the blanks. – RobertE Mar 13 '16 at 16:47
• @RobertE Google: "how to define functions in C++" – Caridorc Mar 13 '16 at 17:08
• Ok i did that and read about 20 pages of info that made no sense to me. Again, this is the third program I've written. The first was a "hello, world" and the second was one which takes a user input and multiplies it, returning the product. I honestly have no idea where you're trying to lead me with the above snippet. I can see what it's supposed to do, kind of, but how to implement it is beyond me. – RobertE Mar 13 '16 at 17:41