draw a square with repeat loops in portugol

Asked

Viewed 43 times

0

Hello! I am learning Portuguese for academic purposes and I have a little problem... I need to draw a square with asterisks but I am not able to draw the bottom and the right, it is coming out like thissaída this is my code

programa
{
    
    funcao inicio()
    {   
        inteiro contador,asteriscos, coluna = 0
        leia(asteriscos)
            se(asteriscos < 2){
                escreva("*")
            }senao{
                para(contador = 0; contador < asteriscos; contador++){
                    escreva("*")
                    escreva(" ")
            }enquanto(coluna < contador){
                escreva("\n*")
                coluna++
            }
}
}
}

should be leaving like this(in case the number was 4) inserir a descrição da imagem aqui

  • Ever tried to make a table test? It’ll make it easier to understand what’s going on

  • worst of all, I’m using the IDE tool to do it, but I’m still killing myself on this little show.

1 answer

0

programa
{
    funcao desenhaQuadrado(inteiro valor, cadeia simbolo){
        inteiro linha, coluna

        para(linha = 1; linha <= valor; linha++){
            se(linha == 1 ou linha == valor){
                para(coluna = 1; coluna <= valor; coluna++){
                    escreva(simbolo, " ")
                }

            }
            senao{
                para(coluna = 1; coluna <= valor; coluna++){
                    se(coluna == 1 ou coluna == valor){
                        escreva(simbolo, " ")
                    }
                    senao{
                        escreva("  ")
                    }
                }
            }
            escreva("\n")
        }
        
    }
    
    funcao inicio()
    {
        escreva("Desenha quadrado na  tela\n-----------------------\n")
        
        inteiro valor
        cadeia simbolo

        escreva("Qual tamanho seu quadrado terá? ")
        leia(valor)

        escreva("\nQual simbolo usar para desenhar seu quadrado? ")
        leia(simbolo)

        escreva("\nUm quadrado de " + valor + " linha(s) e " + valor + " coluna(s)\n" )

        desenhaQuadrado(valor, simbolo)
    }
}

obeservations: The use of the function is not mandatory. I used it here because I took advantage of an exercise I had already done and used function. These variables are passed in the function desenhaQuadrado(valor, simbolo)

Let’s go to the explanation: the user determines the size of the square through an input that is saved in the value variable and also determines the symbol with which the square will be drawn, this value is saved in the symbol variable of the string type.

These variables are passed in the function desenhaQuadrado(valor, simbolo). Within the function I declared two variables : row and column that will serve to draw the square through a loop. To construct the square it is necessary to have a loop that controls the number of rows and within that loop there will be another repeating structure responsible for the columns.

Example: Let’s say user type 3 and symbol is *. It will be a square of 3 rows and 3 columns. For this example we will disregard the use of decision structures within the loop. The drawn functionThis is so:

funcao desenhaQuadrado(inteiro valor, cadeia simbolo){
        inteiro linha, coluna

        para(linha = 1; linha <= valor; linha++){
            para(coluna = 1; coluna <= valor; coluna++){
                escreva(simbolo, " ")
            }

            escreva("\n")
        }
        
    }


first execution

value = 3

symbol = *

starts first loop

line = 1

begins second loop

column = 1

output : *

second loop

column = 2

output = * *

second loop

column = 3

output = * * *

second loop

column = 4 (column is greater than value then loop to)

end of the second loop

line breaker

end of first execution

second execution

value = 3

symbol = *

starts first loop

line = 2

begins second loop

column = 1

output : * * *
         *

second loop

column = 2

output = * * *
         * *

second loop

column = 3

output = * * *
         * * *

second loop

column = 4 (column is greater than value then loop to)

end of the second loop

line breaker

end of second execution

third execution

value = 3

symbol = *

starts first loop

line = 3

begins second loop

column = 1

output : * * *
         * * *
         * 

second loop

column = 2

output = * * *
         * * *
         * *

second loop

column = 3

output = * * *
         * * *
         * * *

second loop

column = 4 (column is greater than value then loop to)

end of the second loop

line breaker

end of the third execution

Fourth execution

value = 3

symbol = *

first loop

line = 4 (loop to then line is greater than value)

End of the fourth execution

output = * * *
         * * *
         * * *

the use of repeating structures

these structures are used to decide the following:

on the first and last line : all columns shall be filled with the symbol *.

on the other lines: only the first and last columns are marked as a symbol *.

That way the output would be :

output = * * *
         *   *
         * * *

hope I’ve helped.

Browser other questions tagged

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