Home > Blockchain >  How to draw asterisks triangles side by side with c
How to draw asterisks triangles side by side with c

Time:10-01

I wanted to draw 4 different triangles horizontally side by side the output example

 #include <stdio.h> 
  
 int main(void) 
 {       
     while ( 1 ) 
     { 
         printf( "Enter a number (0 to exit): " ); 
          
         int n; 
          
         if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break; 
          
         putchar( '\n' ); 
         for ( int i = 0; i < n; i   ) 
         { 
             for ( int j = 0; j < i   1; j   ) printf("*"); 
             putchar( '\n' ); 
         } 
         putchar( '\n' ); 
     } 
 }

this code only prints one triangle, any idea how to progress further?

CodePudding user response:

the first diagram you already did it by yourself

  • for the second diagram, change:

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

into

for ( int j = i; j < n; j   ) printf("*");
  • for the third diagram, change:

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

into

for ( int j = 0; j < n; j   )
   if(j >= i)
       printf("*");
   else
       printf(" ");
  • for the fourth diagram, change:

    for ( int i = 0; i < n; i   ) 
    { 
        for ( int j = 0; j < i   1; j   ) printf("*"); 
        putchar( '\n' ); 
    } 
    

into

  for ( int i = n; i >= 0; i-- )
  {
      for ( int j = 0; j < n; j   )
         if(j >= i)
             printf("*");
         else
             printf(" ");
        putchar( '\n' );
  }

and this is a full code:

#include <stdio.h>

int main(void)
{
    while ( 1 )
    {
        printf( "Enter a number (0 to exit): " );

        int n;

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

        putchar( '\n' );
        for ( int i = 0; i < n; i   )
        {
            for ( int j = 0; j < i   1; j   ) printf("*");
            putchar( '\n' );
        }
        putchar( '\n' );

        putchar( '\n' );
        for ( int i = 0; i < n; i   )
        {
            for ( int j = i; j < n; j   ) printf("*");
            putchar( '\n' );
        }
        putchar( '\n' );

        putchar( '\n' );
        for ( int i = 0; i < n; i   )
        {
            for ( int j = 0; j < n; j   )
                if(j >= i)
                    printf("*");
                else
                    printf(" ");
            putchar( '\n' );
        }
        putchar( '\n' );


        putchar( '\n' );
        for ( int i = n; i >= 0; i-- )
        {
            for ( int j = 0; j < n; j   )
                if(j >= i)
                    printf("*");
                else
                    printf(" ");
            putchar( '\n' );
        }
        putchar( '\n' );

    }
}

and this is the output:

Enter a number (0 to exit):10

*
**
***
****
*****
******
*******
********
*********
**********


**********
*********
********
*******
******
*****
****
***
**
*


**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *



         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

CodePudding user response:

One of the other answers has already provided a solution for printing all 4 triangles below each other.

However, in order to print them side by side, as you are asking for, you will have to print all 4 triangles at the same time, for example like this:

#include <stdio.h>

void print_stars( int num_stars )
{
    for ( int i = 0; i < num_stars; i   )
    {
        putchar( '*' );
    }
}

void print_blanks( int num_blanks )
{
    for ( int i = 0; i < num_blanks; i   )
    {
        putchar( ' ' );
    }
}

int main( void )
{
    while ( 1 )
    {
        int n;

        //get input from user
        printf( "Enter a number (0 to exit): " );
        if ( scanf( "%d", &n ) != 1 || n == 0 )
            break;

        //add spacing
        printf( "\n" );

        //draw all n lines
        for ( int i = 0; i < n; i   )
        {
            //print segment of first triangle
            print_stars( i   1 );
            print_blanks( n - i - 1 );

            //print spacing
            printf( "   " );

            //print segment of second triangle
            print_stars( n - i );
            print_blanks( i );

            //print spacing
            printf( "   " );

            //print segment of third triangle
            print_blanks( i );
            print_stars( n - i );

            //print spacing
            printf( "   " );

            //print segment of fourth triangle
            print_blanks( n - i - 1 );
            print_stars( i   1 );

            //end the line
            printf( "\n" );
        }

        //add spacing
        printf( "\n" );
    }
}

This program has the following output:

Enter a number (0 to exit): 1

*   *   *   *

Enter a number (0 to exit): 2

*    **   **    *
**   *     *   **

Enter a number (0 to exit): 3

*     ***   ***     *
**    **     **    **
***   *       *   ***

Enter a number (0 to exit): 4

*      ****   ****      *
**     ***     ***     **
***    **       **    ***
****   *         *   ****

Enter a number (0 to exit): 5

*       *****   *****       *
**      ****     ****      **
***     ***       ***     ***
****    **         **    ****
*****   *           *   *****

Enter a number (0 to exit): 6

*        ******   ******        *
**       *****     *****       **
***      ****       ****      ***
****     ***         ***     ****
*****    **           **    *****
******   *             *   ******

Enter a number (0 to exit): 7

*         *******   *******         *
**        ******     ******        **
***       *****       *****       ***
****      ****         ****      ****
*****     ***           ***     *****
******    **             **    ******
*******   *               *   *******

Enter a number (0 to exit): 8

*          ********   ********          *
**         *******     *******         **
***        ******       ******        ***
****       *****         *****       ****
*****      ****           ****      *****
******     ***             ***     ******
*******    **               **    *******
********   *                 *   ********

Enter a number (0 to exit): 9

*           *********   *********           *
**          ********     ********          **
***         *******       *******         ***
****        ******         ******        ****
*****       *****           *****       *****
******      ****             ****      ******
*******     ***               ***     *******
********    **                 **    ********
*********   *                   *   *********

Enter a number (0 to exit): 0

Note that your terminal/console may be configured to wrap lines if they are too long. Therefore, you may have to change the configuration of your terminal/console so that the maximum line size is sufficiently large, if you want to display large numbers of n.

CodePudding user response:

To avoid giving away too much, here are the rudiments of a tiny program that produced the output shown below. (Yes, this is all the lines of code that are needed.)

If you can manage to fill in the blanks, you might get very good marks on this assignment!

#include <stdio.h>

int main() {
    int n = 7;
    char *p = "********....
    char *pa[4] = { p   ....

    for( int r = 1; r <= n; r   ) {
        for( int t = 0; t < 4; t   ) {
            printf( "%....
            pa[t]  = t%2 ? ....
        }
        putchar( '\n' );
    }

    return 0;
}
*        *******  *******        *
**       ******    ******       **
***      *****      *****      ***
****     ****        ****     ****
*****    ***          ***    *****
******   **            **   ******
*******  *              *  *******

EDIT:
At the recommendation of an SO regular with more SO experience than me, here is the full (but limited) version of that code.

#include <stdio.h>

int main() {
    int n = 7;
    char *p = "**********          **********",
         *pa[] = { p   9, p   10 - n, p   20, p   21 - n };

    for( int r = 1; r <= n; r   ) {
        for( int t = 0; t < 4; t   ) {
            printf( "%.*s  ", n, pa[t] );
            pa[t]  = t%2 ? 1 : -1;
        }
        putchar( '\n' );
    }
    return 0;
}

Obviously, this works in the range of 1-10 stars/spaces. To achieve 1-n, this could be extended to allocate a buffer on the heap, fill it with n stars and n spaces and n stars (as a string), then twiddle the hardcoded initial offsets in pa[] to account for those larger offsets.

EDIT2:
Playing with this idea, one can exploit the printf format specifier to reduce the requirements of the string that is used as raw material. And, while we're at it, factor the function out of main so that it can be iterated independently, this time up to the value of 15 wide/high.

void fun( int n ) {
    char *p = "               ***************"; // 15x' '   15x '*'

    // the 15/30-ish-ness of the following should be apparent
    char *pa[] = { p   15 15-1, p   15 15-n, p   15, p   15 1-n };

    for( int r = 1; r <= n; r   ) {
        for( int t = 0; t < 4; t   ) {
            printf( "%-*.*s  ", n, n, pa[t] );
            pa[t]  = t%2 ? 1 : -1;
        }
        putchar( '\n' );
    }
}

int main() {
    for( int i = 1; i <= 15; i   ) {
        printf( "\n%d:\n", i );
        fun( i );
    }

    return 0;
}

If pointer arithmetic is not understood as yet, two lines of the code above can be changed to use array indexing instead:

//  char *pa[] = { p   15 15-1, p   15 15-n, p   15, p   15 1-n }; // THIS...
    int pa[] = { 15 15-1, 15 15-n, 15, 15 1-n }; // becomes this.

// and

//          printf( "%-*.*s  ", n, n, pa[t] ); // THIS...
            printf( "%-*.*s  ", n, n, &p[ pa[t] ] ); // becomes this.
  •  Tags:  
  • c
  • Related