lundi 17 octobre 2016

Drawing inverted triangle varying base length

I am sorry if this is such a basic question, but i've been working on this problem for hours. I can print an inverted triangle based on how many rows the user wants. however, how can i edit this code to draw an inverted triangle according to the input base length? e.g input base length= 5

  *****
   ***
    *

instead of drawing 5 rows of *. I somehow cant come up with a formula without messing up the pattern. this is the code im trying to edit,

 #include<stdio.h>
  int main()
{
  int rows, i, j, space;

  printf("Enter the number of rows: ");
  scanf("%d",&rows);

  for(i=rows; i>=1; --i)
  {
    for(space=0; space < rows-i; ++space)
        printf(" ");

    for(j=i; j <= 2*i-1; ++j)
        printf("*");

    for(j=0; j < i-1; ++j)
        printf("*");

    printf("\n");
    }

   return 0;
   }

I dont need the full answer, just some advice on how to edit it accordingly. thank you!

Aucun commentaire:

Enregistrer un commentaire