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.
Ever tried to make a table test? It’ll make it easier to understand what’s going on
– Rafael Tavares
worst of all, I’m using the IDE tool to do it, but I’m still killing myself on this little show.
– Anon