m3k3r1
m3k3r1

Reputation: 15

Inverted number pyramid

I'm trying to do an inverted triangle with numbers using C. I think the number part of the code is right, but the spacing isn't working.

#include <stdio.h>
//Declare function
int triangle(int n);

//Main
int main(void){
  int height;
  do{
    printf("height:");
    scanf("%d", &height);
  }while (height < 1 && height > 9);
  triangle(height);}

//Function
int triangle(int n)
{
  int x,j,linhas, spaces;

//Number of lines
  for(linhas = 0; linhas < n; linhas++){

//Print spaces NOT FUC****** WORKING!!!
    for(spaces =0; spaces < (linhas + 1); spaces ++){
      printf(" ");}
//Fill in the numbers
    do{
    //Increasing part till n
      for (x = 0; x < n; x++){
        printf(" %d ", (x+1));}
//Decreasin part from n
      for (j = 0; j < (n-1) ; j++){
        printf(" %d ", ((n-1) -j));}
//New line after each line
      printf("\n");
      n--;
    }while(n > 0);}}

the output is :

 height:5
      1  2  3  4  5  4  3  2  1 
     1  2  3  4  3  2  1 
     1  2  3  2  1 
     1  2  1 
     1 

Anybody can help on the spacing the output should be:

 height:5
      1  2  3  4  5  4  3  2  1 
         1  2  3  4  3  2  1 
            1  2  3  2  1 
               1  2  1 
                  1 

Upvotes: 1

Views: 465

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

My five cents.:)

You can output the first digit separatly using symbol * as the field width in the format specifier and an appropriate number.

For example

printf( "%*u", 2 * i + 1, j );
         ^^^ 

Here is a demonstrative program

#include <stdio.h>

int main( void )
{
    while ( 1 )
    {
        printf( "Enter the height of the pyramid (0- exit): " );

        unsigned int n;

        if ( scanf( "%u", &n ) != 1 || n == 0 ) break;

        printf( "\n" );

        for ( unsigned int i = 0; i < n; i++ )
        {
            unsigned int j = 1;
            printf( "%*u", 2 * i + 1, j );
            while ( !( n - i < j + 1 ) ) printf( " %u", ++j );
            while ( --j != 0 ) printf( " %u", j );
            printf( "\n" );
        }        
    }

    return 0;
}

If to enter sequentially

9 8 7 6 5 4 3 2 1 0

then the program output is

Enter the height of the pyramid (0- exit): 9

1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
        1 2 3 4 5 4 3 2 1
          1 2 3 4 3 2 1
            1 2 3 2 1
              1 2 1
                1
Enter the height of the pyramid (0- exit): 8

1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 6 5 4 3 2 1
    1 2 3 4 5 6 5 4 3 2 1
      1 2 3 4 5 4 3 2 1
        1 2 3 4 3 2 1
          1 2 3 2 1
            1 2 1
              1
Enter the height of the pyramid (0- exit): 7

1 2 3 4 5 6 7 6 5 4 3 2 1
  1 2 3 4 5 6 5 4 3 2 1
    1 2 3 4 5 4 3 2 1
      1 2 3 4 3 2 1
        1 2 3 2 1
          1 2 1
            1
Enter the height of the pyramid (0- exit): 6

1 2 3 4 5 6 5 4 3 2 1
  1 2 3 4 5 4 3 2 1
    1 2 3 4 3 2 1
      1 2 3 2 1
        1 2 1
          1
Enter the height of the pyramid (0- exit): 5

1 2 3 4 5 4 3 2 1
  1 2 3 4 3 2 1
    1 2 3 2 1
      1 2 1
        1
Enter the height of the pyramid (0- exit): 4

1 2 3 4 3 2 1
  1 2 3 2 1
    1 2 1
      1
Enter the height of the pyramid (0- exit): 3

1 2 3 2 1
  1 2 1
    1
Enter the height of the pyramid (0- exit): 2

1 2 1
  1
Enter the height of the pyramid (0- exit): 1

1
Enter the height of the pyramid (0- exit): 0

Upvotes: 1

OznOg
OznOg

Reputation: 4722

you should try (keeping most of your code)

//Function
int triangle (int n)
{
  int x, j, linhas, spaces;
  int orig_n = n;
//Number of lines
  for (linhas = 0; linhas < n; linhas++)
    {

      do
        {
          for (spaces = 0; spaces < (orig_n - n) * 3; spaces++)
            {
              printf (" ");
            }
//Fill in the numbers
          //Increasing part till n
          for (x = 0; x < n; x++)
            {
              printf (" %d ", (x + 1));
            }
//Decreasin part from n
          for (j = 0; j < (n - 1); j++)
            {
              printf (" %d ", ((n - 1) - j));
            }
//New line after each line
          printf ("\n");
          n--;
        }
      while (n > 0);
    }
}

Upvotes: 0

GustavoR.
GustavoR.

Reputation: 158

So, the problem are in do loop. You decrease the count (variable n), stay in loop until n>0 and the 'spaces for' do one time. Try doing like this.

int triangle(int n)
{
    int x,j,linhas, spaces, m;
    //Number of lines
    m=n;
    for(linhas = 0; linhas < n; linhas++){
        for(spaces =0; spaces < linhas; spaces ++){
            printf("   ");
        }
        //do{
            for (x = 0; x < m; x++){
                printf(" %d ", (x+1));
            }
            for (j = 0; j < (m-1) ; j++){
                printf(" %d ", ((m-1) -j));
            }
            printf("\n");
            m--;
        //}while(n > 0);
    }
}

Upvotes: 2

anatolyg
anatolyg

Reputation: 28241

Your code is messed up. It should have 2 nested loops, like this:

while (n > 0)
{
    for (...) // print decreasing
    {
    }
    for (...) // print increasing
    {
    }
}

Then you stuff your spaces inside the 1st nesting layer:

while (n > 0)
{
    for (...) // print spaces
    {
    }
    for (...) // print decreasing
    {
    }
    for (...) // print increasing
    {
    }
}

However, you have 3 nested layers!

for (linhas ...) // useless code - you should remove it!
{
    for (...) // this code is misplaced
    {
    }
    while (n > 0)
    {
        for (...) // print decreasing
        {
        }
        for (...) // print increasing
        {
        }
    }
}

The outer loop does just on iteration (because on the second iteration n is 0, and it exits). This code is unneeded and confusing - remove it, and then you will see where to place your code that prints spaces.

Upvotes: 1

Related Questions