Make a drawing according to user input

1

The exercise asks the program to receive an integer n and print a height drawing 2*n as follows:

\    *    /
 \  ***  /
  \*****/
   \***/
    \*/
    /*\
   /***\
  /*****\
 /  ***  \
/    *    \

I've done the following so far:

void arte(int n)
{
    int i, barrasE, aux, espacobarrasE, ebE, espaco;
    aux = n;

    for(i = 1; i <= n; i++)
    {
        if(aux < n) //Espaços impressos antes das contra barras na parte superior.
        {
            espacobarrasE = n - aux;
            for(ebE = 0; ebE < espacobarrasE; ebE++)
                printf(" ");
        }
        for(barrasE = 1; barrasE <= aux; barrasE++) //Desenha as contra barras na diagonal na parte superior.
        {
            printf("\");
            break;
        }
        for(espaco = 1; espaco < n; espaco++)
        {
            printf(" ");
        }
        aux = aux - 1;
        printf("\n");
    }
}

But this only prints the counter bars above and I do not know how to continue. I would like to know the best way to continue.

    
asked by anonymous 06.05.2017 / 20:46

1 answer

0

Starting at the top:

Any line can be formed from this pattern: ( espaço ) ( \ ) ( espaço ) ( * ) ( espaço ) question is how many times each symbol should be drawn on each line.

See this function:

void desenha(int qtd, char simbolo){
    int i;
    for(i=0; i<qtd; i++)
        printf("%c", simbolo);
}

In this function you pass / and a symbol and this symbol will be printed% with% of the screen. For example espaço :

The result will be: qtd

The first line of your example ( qtd ) can be represented as

(0, desenha(5, '*') ) (1, ***** ) (4, \ * / ) (1, espaço ) (4, \ ) %)

and any line by

(n2, espaço ) (n2, * ) (n3, espaço ) (n4, / ) %)

Since n2 and n6 will always be 1 and n1 always equals n7 and n3 always equals n5, we can write in simplified form

(N1, espaço ) (N1, espaço ) (N2, \ ) (N3, espaço ) (N2, * ) %)

Now you only have to define how the N1, N2 and N3 calculation will be done and you will be able to draw the top part. The lower one will be easily made later, just by reversing the order of the calculation.

One way I used to do the calculation was:

N1 = i;
N2 = n - 2*i -1;
N3 = n - 2*abs(i-(n/2));

But try to create your own logic to perform the calculation. Note: In the logic I used, when an even number is passed to the function the drawing comes out a bit differently, but also was not specified as it should be, try to correct that too.

Complete code:

void desenha(int qtd, char simbolo){
    int i;
    for(i=0; i<qtd; i++)
        printf("%c", simbolo);
}


void arte(int n){
    int N1, N2, N3;
    int i;
    //Parte superior
    for(i=0; i<n; i++){
        N1 = i;
        N2 = n - 2*i -1;
        N3 = n - 2*abs(i-(n/2));

        desenha(N1, ' ');
        desenha(1, '\');
        desenha(N2 , ' ');
        desenha(N3 , '*');
        desenha(N2, ' ');
        desenha(1, '/');
        printf("\n");
    }
    //Parte inferior
    for(i=n-1; i>=0; i--){
        N1 = i;
        N2 = n - 2*i -1;
        N3 = n - 2*abs(i-(n/2));

        desenha(N1, ' ');
        desenha(1, '/');
        desenha(N2 , ' ');
        desenha(N3 , '*');
        desenha(N2, ' ');
        desenha(1, '\');
        printf("\n");
    }
}

int main(){
    arte(5);
    return 0;
}
    
07.05.2017 / 11:46