0

I've been working on this problem for days and I can't figure out how to print the right side of the pyramid. I"m able to print the left side, preceded by a space (denoted by "y) and one hash.

I've tried so many different versions of my for loop and different forms of arithmetic to try and print the right side -- what am I doing wrong for the right side??

#include <cs50.h>
#include <stdio.h>
int get_height(string prompt);

int main(void)
{
 int i = get_height("enter a number between 1 and 8: ");   

        for(int m = 0; m <= i; ++m) //adds gap
    {
        printf("# y");  


       for(int p = 0; p <= m; ++p) //prints left side
         {
            printf("#");
         }

        printf("\n");
     }
    }


// get input function
int get_height(string prompt)

{
    int n;
    do
    {
        n = get_int("%s", prompt);
    }
    while (n <= 1 || n >= 9 );
    return n;
}

This is what it's returning:

# y#
# y##
# y###
# y####
# y#####
1
  • Okay thank you for the tips! I started over from scratch and I think I am getting somewhere - this was very helpful, thanks!
    – mollie8
    Sep 14, 2019 at 21:46

1 Answer 1

1

I don't think you've quite grasped the assignment. The code is to print out the left side of the pyramid so that it is right-justified. That means that it is supposed to have leading spaces before the #s are printed - not just one space on every line. Each line will have one less leading space than the line before.

In reviewing your code, I did not see any code to handle this. You need to implement this first. Once you have this right, along with the printing of hashes on each line for the left side of the pyramid, it is then a simple task to add a space and to print the correct number of hashes on each line. (it's the same code that prints the hashes on the left side.)

As an aid, try printing x instead of space so that you can see exactly what the code is doing.

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

You must log in to answer this question.

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