Home > Enterprise >  Drawing mirrored arrows in C
Drawing mirrored arrows in C

Time:08-03

I am having trouble with drawing this shape in C using asterisk:

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

I know how to make either the right or left arrow, but I don't know how to make them like this. This is the code that I have so far, which makes the left arrow.

#include <stdio.h>

int main() {
    int n,i,j;
    printf("N:");
    scanf("%d", &n);

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

    for(i=n; i>=1; i--){
        for(j=i; j<=n; j  ){
            printf(" ");
        }
        for(j=1; j<i; j  ){
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

I would really appreciate the help.

CodePudding user response:

When trying to draw shapes in ASCII, you have to ask yourself what characters you are printing in what order. Draw it manually (with a keyboard, not on paper eh) if you have to.

In the case of your double arrows:

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

The sequence here starts with star, space x9, star, \n, star x2, space x7, star x2, \n, star x3, space x5, star x3, \n, ...

So here's one possible solution:

#include <stdio.h>

int main(void) {
  for (int i = 0; i < 5; i  ) {
    for (int j = 0; j < i   1; j  ) {
      putchar('*');
    }
    for (int j = 0; j < 9 - (i * 2); j  ) {
      putchar(' ');
    }
    for (int j = 0; j < i   1; j  ) {
      putchar('*');
    }
    putchar('\n');
  }
  for (int i = 4; i > 0; i--) {
    for (int j = 0; j < i; j  ) {
      putchar('*');
    }
    for (int j = 0; j < 11 - (i * 2); j  ) {
      putchar(' ');
    }
    for (int j = 0; j < i; j  ) {
      putchar('*');
    }
    putchar('\n');
  }
}

CodePudding user response:

You need to start by identifying the patterns in the figure.

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

We have a number of stars, a number of spaces, then a number of stars again.

  1. 1 *, 9 ␠, 1 *
  2. 2 *, 7 ␠, 2 *
  3. 3 *, 5 ␠, 3 *
  4. 4 *, 3 ␠, 4 *
  5. 5 *, 1 ␠, 5 *
  6. 4 *, 3 ␠, 4 *
  7. 3 *, 5 ␠, 3 *
  8. 2 *, 7 ␠, 2 *
  9. 1 *, 9 ␠, 1 *

We could also see it as a number of stars, an amount of padding, a space, an amount of padding, then a number of stars.

  1. 1 *, 4 ␠, 1 ␠, 4 ␠, 1 *
  2. 2 *, 3 ␠, 1 ␠, 3 ␠, 2 *
  3. 3 *, 2 ␠, 1 ␠, 2 ␠, 3 *
  4. 4 *, 1 ␠, 1 ␠, 1 ␠, 4 *
  5. 5 *, 0 ␠, 1 ␠, 0 ␠, 5 *
  6. 4 *, 1 ␠, 1 ␠, 1 ␠, 4 *
  7. 3 *, 2 ␠, 1 ␠, 2 ␠, 3 *
  8. 2 *, 3 ␠, 1 ␠, 3 ␠, 2 *
  9. 1 *, 4 ␠, 1 ␠, 4 ␠, 1 *

For the first 5 lines, we print

  1. a number of stars equal to the line number
  2. a number of spaces equal to 5 minus the line number
  3. a single space
  4. a number of spaces equal to 5 minus the line number,
  5. a number of stars equal to the line number

If i is the line number, this simplifies to

  1. i stars
  2. ( 5-i ) * 2 1 spaces
  3. i stars

We can find a similar pattern for the last 4 lines. With this we can achieve the desired result using two loops.

  1. Loop such that i goes from 1 to 5 inclusive,
    1. Print i stars.
    2. Print ( 5-i ) * 2 1 spaces.
    3. Print i stars.
    4. Print a line feed.
  2. Loop such that i goes from 4 to 1 inclusive,
    1. Print i stars.
    2. Print ( 5-i ) * 2 1 spaces.
    3. Print i stars.
    4. Print a line feed.

We could even generalize this to a single loop.

  1. Loop such that j goes from -4 to 4 inclusive,
    1. Let i be 5 minus the absolute value of j.
    2. Print i stars.
    3. Print ( 5-i ) * 2 1 spaces.
    4. Print i stars.
    5. Print a line feed.

Finally, it's best if we avoid using 5 and derived value all over the place.

  1. Loop such that j goes from -(n-1) to (n-1) inclusive,
    1. Let i be n minus the absolute value of j.
    2. Print i stars.
    3. Print ( n-i ) * 2 1 spaces.
    4. Print i stars.
    5. Print a line feed.
#include <stdio.h>

void putcharx( char ch, size_t count ) {
   while ( count-- )
      putchar( ch );
}

int main( void ) {
   int n = 5;
   for ( int j = -(n-1); j <= (n-1);   j ) {
      int i = n - ( j >= 0 ? j : -j );
      putcharx( '*', i );
      putcharx( ' ', ( n-i ) * 2   1 );
      putcharx( '*', i );
      putchar( '\n' );
   }

   return 0;
}

Demo on Compiler Explorer

CodePudding user response:

Function drawing it for any (odd) number.

void drawArrow(unsigned x)
{
    if(x % 2)
        for(unsigned row = 0; row < x; row  )
        {
            for(unsigned col = 0; col <= x   1; col  )
            {
                if((row <= x / 2 || row == x / 2) && (col <= row || col > x - row)) putchar('*');
                else if((row > x / 2) && (col > row   1 || col < x - row)) putchar('*');
                else putchar(' ');

            }
            putchar('\n');
        }
}

int main(void)
{
    drawArrow(19);
}

https://godbolt.org/z/6WK55Kv1a

CodePudding user response:

When you enter 5

pattern will write this

1 * 4 blank 1blank 4 blank 1 * \n

2 * 3 blank 1blank 3 blank 2 * \n

3 * 2 blank 1blank 2 blank 3 * \n

4 * 1 blank 1blank 1 blank 4 * \n

5 * 0 blank 1blank 0 blank 5 *

You can use this code:

#include <stdio.h>

int n;
char ch;
printf("Enter the number of rows and any Character to print the pattern\n");
scanf("%d", &n);
printf("\n");

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

    int space = 2 * n - 2 * i;
    for (int j = 1; j <= space; j  )
    {
        printf(" ");
    }
    printf(" ");
    for (int k = 1; k <= i; k  )
    {
        printf("*");
    }
    printf("\n");
}

for (int i = n - 1; i >= 1; i--)
{
    for (int j = 1; j <= i; j  )
    {
        printf("*");
    }

    int space = 2 * n - 2 * i;
    for (int j = 1; j <= space; j  )
    {
        printf(" ");
    }
    printf(" ");
    for (int k = 1; k <= i; k  )
    {
        printf("*" );
    }
    printf("\n");
}
return 0;

}

  •  Tags:  
  • c
  • Related