-3

I just started with C programming and have some difficulty implementing a program which is giving a staircase with 'Height' amount of steps.

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

int main(void)

{
  int height;

    do
    {
        height = get_int("Height: ");
    }
    while(height > 8 || height == 0 || height < 0);

  int width = 0;
  int length = height;

  while(width < height)
  {
    printf(" ");
    printf("@");
    for(width = 0; width < height; width++)
      {
        printf("\n");
      }

  }
}

The first lines with the Height are working, but I have difficulties with actually writing a staircase. I wanted something like this or similar to this.

Height: 3
@
 @
  @

I just want to learn how to implement something like this if I face a problem like this in the future. If somebody could help me further I would really appreciate it!

2
  • Stop vandalizing your own question. You have done this twice now.
    – abelenky
    Dec 6, 2021 at 17:02
  • 1
    The duplicated question linked here (How do I check out a remote Git branch?) does not match up with @Hyakkimaru's question? May 3, 2022 at 4:42

3 Answers 3

0

This works:

#include <stdio.h>

int main() {
    // gets height input - replace with your get_int method
    int height;
    printf("Height: ");
    scanf("%i",&height);
    // loop over all the steps: 0 - height
    for (int i = 0; i < height; i++) {
        // add a space i number of times (where i is our current step number and so equal to width)
        // notice that if we take top left as (0,0), we go 1 down and 1 right each time = current step
        for (int j = 0; j < i; j++) {
            printf(" ");
        }
        // finally, after the spaces add the character and newline
        printf("@\n");
    }
    return 0;
}
2
  • Just curious what does the 'scanf 'function do and what is meant with '%d'
    – Hyakkimaru
    Nov 3, 2021 at 21:39
  • It is just grabbing the input. Declaring a variable, then passing its address to the function. This reads the standard inputs and passes its value into the variable at the given address (height in this case). %d is just stating the data type (decimal). I changed it now to %i which is integer Nov 3, 2021 at 21:41
0

I see three issues here:

  1. You're printing newlines (\n) instead of spaces ( ).
  2. Why print the single space character?
  3. You're printing the "@" before (what should be) the spaces.
  4. Print a newline after the spaces and the @.

Also... the staircase's width is always equal to its height; it's just the line you're printing that's advancing... that's a bit confusing.

0
#include <stdio.h>

int main(void)
{
    int height = 5;
    for(int i=0; i<height; printf("%*s\n", ++i, "@"));
}

Output:

Success #stdin #stdout 0s 5572KB
@
 @
  @
   @
    @
4
  • It's a homework question... it's probably better to not just solve it for OP but guide them.
    – einpoklum
    Nov 3, 2021 at 21:32
  • @einpoklum it's not really a homework question, but more like an extra question if you want to learn more. I won't copy it, but just want to know how it's done. So don't worry :)
    – Hyakkimaru
    Nov 3, 2021 at 21:38
  • @abelenky Just curious, what does "%s\n" do. I know that \n is to write a new line, but what about %s.
    – Hyakkimaru
    Nov 3, 2021 at 21:41
  • @Hyakkimaru: You should read about what printf() actually does. You're using it in a degenerate form, and could really just replace your uses of it with puts() calls.
    – einpoklum
    Nov 3, 2021 at 21:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.