0

I've been trying to do the less comfortable version of mario.c for a while, here is what I've been able to put together so far:

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

int main(void)
{
// declare height variable for scope purposes
int height;

// do while loop ensures code is run atleast once
do
{
    height = get_int("Enter the height of your pyramid: ");
}
// if height is less than 0 or greater than 23 the user will be prompted for input again
while (height < 0 || height > 23);

// for every row
for (int row = 0; row < height; row++)
{

    // print the amount of spaces
    for (int spaces = row; spaces < height - 1; spaces++)
    {
        printf(" ");
    }

    // print out the hashes
    for (int hashes = row; hashes < height + 1; hashes++)
    {
        printf("#");
    }
    printf("\n");
}

} Everything was looking good until I got to the hashes. Right now, my pyramid is printing the correct number of spaces and hashes but the hashes are printed upside down. So if your input was 5, the pyramid would look like this:

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

The hashes are arranged upside down. (The upper two hashes are printed at the bottom, etc.) but the whitespace is just fine. Can anyone tell me what I am doing wrong?

1

Think about the logic of printing hashes. First, forget about the spaces when printing hashes. Next, think about these questions:

  1. How many hashes are supposed to be on the first line of the pyramid?
  2. Does the number of hashes on the first line change if a pyramid has a height of 1 or 5 or 23?
  3. Does a line always have one more hash than the line before?

With all that in mind, why does the code above make the number of hashes depend on the row number as a starting point and why does it depend on the height of the pyramid at all?

for (int hashes = row; hashes < height + 1; hashes++)

If the spaces are right, don't tinker with that part of the code. Just work with the part that prints the hashes. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • for (int hashes = 0; hashes <= row + 1; hashes++) ////// The code on the left worked but I didn't really "solve" the problem I just messed around until I found the solution due to luck. Can you tell me why the code I posted in the comment worked when I replaced it with the line you told me to revise? Thanks for the help btw :) – AJ72311 Dec 25 '18 at 1:22
  • The code in the comment works because its exactly correct. Simply put, the number of hashes is the line number plus 1. (at least for the easy version.) Well, actually, the code in your comment is almost, but not quite perfect. I'll let you figure out why. ;-) – Cliff B Dec 25 '18 at 14:47
0

That looks way too complicated.

Here's a simple version I whipped up:

#include <stdio.h>

int main(void) {
    int height = 5;
    char blocks[height];
    memset(blocks,'#',height);

    for(int i=0; i<height; ++i)
    {
        printf("%*.*s\n", height, i+1, blocks );
    }
    return 0;
}

Output:

Success #stdin #stdout 0s 9424KB
    #
   ##
  ###
 ####
#####
| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .