Question

How could I draw such a figure with 2 for loops and 1 if statement in C?

AAAAA
AAAAB
AAABB
AAAAB
AAAAA

I know I'd have to compare the rows and columns but I don't know how to get the exact result.

It has to work for every number larger than 0 and smaller than 24.

What I did so far is the following:

for (row = 1; row <= n; row++)
{
    for (column = 1; column <= n; column++)
    {
        if ( row >= column )
        {
            printf("A");
        }
        else
        {
            printf("B");
        }
    }
    printf("\n");
}

which gives me this for n = 5:

ABBBB
AABBB
AAABB
AAAAB
AAAAA
Was it helpful?

Solution

here you go:

for (row = 1; row <= 5; row++)
{
    for (column = 1; column <= 5; column++)
    {
        if ( (row < column) && (column > 5 - row + 1) )
        {
            printf("B");
        }
        else
        {
            printf("A");
        }
    }
    printf("\n");
}

demo

explanation :

condition (row < column) 'draws':

ABBBB
AABBB
AAABB
AAAAB
AAAAA

condition (column > 5 - row + 1) 'draws' :

AAAAA
AAAAB
AAABB
AABBB
ABBBB

the combination of these condition is you result.

OTHER TIPS

python code(translated to C below):

for i in range(5):
    for j in range(5):
        if(i-j<0 and i+j>4):
            print("B",end="")
        else:
            print("A",end="")
    print()

C:

for(int i=0;i<5;i++)
{
   for(int j=0;j<5;j++)
   {
       if(i-j<0 && i+j>4) printf("B");
       else printf("A");
   }
   printf("\n");
}

Do you remember x=y and x=-y lines in geometry? Solution is the intersection of the half planes.

Check this out for geometrical idea behind it.

UPDATE: You also indicated that it should work for general cases, so:

int n = SIZE;
for(int i=0;i<n;i++)
{
   for(int j=0;j<n;j++)
   {
       if(i-j<0 && i+j>(2*(n/2)) printf("B");
       else printf("A");
   }
   printf("\n");
}

This is one way; note how I've exploited the symmetry of the output to run the row counter from -2 to +2. My counting col backwards is a little naughty although it makes the if and the abs clearer.

#include "stdio.h"

int main()
{
    for (int row = -2; row <= +2; ++row){
        for (int col = 5; col >= 1; --col){
            if (col > 2 - abs(row)){
                printf("A");
            } else {
                printf("B");
            }
        }
        printf("\n");
    }
}

Note that you'll need at least C99 to compile this due to my placement of int inside the for loops.

Single loop:

for(i=0; i<30; ++i) putch(i%6==5?'\n':i%6-(i/6-2)*(i/6-2)<3?'a':'b');

Following may not be as efficient as others but you can simply generate patterns analyzing the symmetry

                        n=9
                n=7     0
        n=5     0       1
AAAAA   0       1       2
AAAAB   1       2       3
AAABB   2       3       4
AAAAB   1       2       3
AAAAA   0       1       2
                0       1
                        0

So :

void generate_AB(int n, int i)
{
 int j;
   for(j=1;j<=n-i;j++) //n-i no. of As
      printf("A"); 
   for(j=1;j<=i;j++) // i no. of Bs
      printf("B");
      printf("\n");
}

int main()
{
  int i,j,n=5;

    for(i=0;i<=n/2;i++)  // Count 0 to INT(n/2)
        generate_AB(n,i);

    for(i=n/2-1;i>=0;i--) // Count  INT(n/2)-1 to 0
        generate_AB(n,i);

    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top