how do i right align pyramid in C?

28 views Asked by At

I've just started the CS50 course and currently having no coding experience I feel like I am doing 'OK' however I am trying to complete a side task of creating a 'right aligned pyramid' having already completed the left aligned one I thought this would be easy, but I am struggling to get my head around what needs to be done.

can you help - ideally looking for an update on the code and how that affects the outcome, rather than just an answer, as i'd like to understand the change and reasoning.

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

void print_row(int length);

int main(void)
{
int height = get_int("Height: ");
for (int i = 0; i \< height; i++)
{
print_row(i + 1);
}
}

void print_row(int length)
{

for (int g = 0 ; g \< length; g++)

     {

printf(" ");
}

for (int h = 0; h \< length; h++)

     {

printf("#");
}

printf("\\n");
}
1

There are 1 answers

0
NoDakker On

I realize that you were wanting just some directions in the comments and not a full answer, but since I did not seem to convey the thought process here, let me offer up a refactored version of the code for printing a pyramid for a requested height based upon the code you included. Following is the refactored code.

#include <stdio.h>
#include <stdlib.h>

void print_row(int length, int ht)
{
    for (int g = 0 ; g < (ht - length); g++)    /* Print needed spaces for the padding based upon the requested height  */
    {
        printf(" ");
    }

    printf("/");                                /* Print the left leaning slash prior to printing hash character(s)     */

    for (int h = 0; h < (length * 2 - 1); h++)
    {
        printf("#");                            /* Print the correct amount of hash characters based upon height        */
    }

    printf("\\\n");                             /* Finish up with printing the right leaning backslash                  */
}

int main(void)
{
    int height;

    printf("Height: ");                         /* Don't have CS50 installed - so just use a prompt and scanf           */
    scanf("%d", &height);

    for (int i = 0; i < height; i++)
    {
        print_row(i + 1, height);
    }

    return 0;
}

Here are some of the points I was trying to talk about as also noted in the comments.

  • It appears that an attempt is being made to determine the number of spaces with just one factor (length), but it seems that to properly know the space count the height needs to be passed as a value to any printing function.
  • Knowing which level the function is on makes it simpler to also determine when to print the forward slash, then knowing how many hash symbols to print, and then being able to print the backslash.
  • Also, it seemed apparent that some more experience was needed to understand the usage of the backslash as an escape character in the "printf" function, thus needing three backslash characters to print a single backslash followed by the newline combination.

Following was a quick sample test with what I believe is the desired output based upon previous "print a pyramid" questions I have worked on.

craig@Vera:~/C_Programs/Console/Pyramid_CS50/bin/Release$ ./Pyramid_CS50 
Height: 3
  /#\
 /###\
/#####\
craig@Vera:~/C_Programs/Console/Pyramid_CS50/bin/Release$ ./Pyramid_CS50 
Height: 6
     /#\
    /###\
   /#####\
  /#######\
 /#########\
/###########\

The takeaway I see here probably is to delve more into available "C" tutorial literature, either published or online, and get more familiar with things such as the backslash escape combo's.