Make a drawing as user input

Asked

Viewed 92 times

1

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

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

I did the following until the moment:

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 against top bars and do not know how to continue. I would like to know the best way to continue.

  • just to confirm, in this first example, n=5 correct ?

  • Yes, Rovann. n = 5 and a drawing of height 2n = 10.

1 answer

0


Starting at the top:

Any line can be formed from this pattern: (espaço)(\)(espaço)(*)(espaço)(/)(espaço) and the 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 a number qtd and a symbol and this symbol will be printed qtd times on the screen. For example desenha(5, '*'):

The result will be: *****

The first line of your example (\ * /)can be represented in the form

(0,espaço)(1,\)(4,espaço)(1,*)(4,espaço)(1,/)(0,espaço)

and any line by

(n1,espaço)(N2,\)(N3,espaço)(N4,*)(N5,espaço)(N6,/)(N7,espaço)

as N2 and N6 will always be 1 and n1 always equals N7 and N3 always equals N5, we can write in a simplified way

(N1,espaço)(1,\)(N2,espaço)(N3,*)(N2,espaço)(1,/)(N1,espaço)

Now just define how the calculation of N1, N2 and N3 will be done and you will be able to draw the top part. The bottom will be done easily afterwards, just 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 little different, but also not specified as it should be, try to fix it 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;
}
  • 1

    Thanks Lucas! Your answer helped me a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.