I'm stuck on the mario problem in pset1! How should I start going about printing the half pyramid? I already figured the user input part...
2 Answers
To get you started, here is some code for a loop that prints out 5 lines of text:
for (int i = 0, n = 5; i < n; i++)
{
printf("row #%d\n", i);
}
This will print out:
row #0
row #1
row #2
row #3
row #4
Once you have this working you can modify the loop with the additional code needed for mario.
- Modify the loop so that instead of repeating only 5 times, it repeats the number of times entered by the user.
- Modify the body of the loop so it prints the a number of spaces, followed by a number of hashes. You may need other loops inside this loop.
Tips:
- Indent your code correctly so that you can see which blocks contain which code.
- Re-compile and re-run your program after each change that you make. By doing this you will see what effect each line of code has. It will also help you backtrack more easily if things go wrong.
Good luck!
-
Thank you so much, I think this is all I needed to get started.– Danny C.Commented Apr 5, 2015 at 19:03
Hi and welcome to the SE community, and great job figuring out the user input part, so congrats!
In order to figure out how to draw the half-pyramid, take a pen and paper and start drwaing them by hand on the paper, soon enough, you'll be able to figure out a pattern in them, for example, this is the half pyramid when user enters 4(example 1):
##
###
####
#####
When user enters 2(2nd example):
##
###
Now, look closely( at first and throughout my answer, I take a look at the first pyramid, but to verify if the relations you find are true for other halfpyramids as well, like the 2nd example, try it in the 2nd example) , what do you see, except, of course, some hashes.
You see a pattern, and that pattern exists for both spaces and hashes differently. If you need hint of the pattern in the spaces, look again, you observe that in the 1st line, there are 3 spaces, in the 2nd, the number decreases to 2, in the 3rd this decreases to 1, and then ultimately, to 0, now observe whats so special about there being 3 spaces in first line, find the relation of this 3 to the USER INPUT(4, in this case) and behold, you now know how many spaces to print in which line.
You may repeat this same idea to find relation of number of hashes(#) to the USER INPUT, easy right, so now, get to work!
I haven't given you the precise answers simply because you asked for hints and not the final answers, hence, if you think you need more help to figure it out, let me know.
-
-
I believe the example pyramids provided above actually depict heights of 4 and 2 respectively (as opposed to 5 and 3, as listed).– Jon SCommented Aug 11, 2014 at 8:02
-
Why can't you use %*s where * indicates indentation that is right justified = height the user inputs? For example, in my code, printf("%*s", height, '#') . However, I can't get enough hashes printed. My output at the moment looks like: ----## -----# -----# -----# Commented Oct 19, 2015 at 18:01