Matrix with strings in Visualg

2

I'm having difficulty with the following question:

  

Create an M matrix [21,10]. Read 10 names (maximum 20 characters) and store in the first row of the array. After reading, decompose the names letter by letter and store them in the other lines of the matrix from the second. At the end, write the array.

Mycodelookslikethis:

algoritmo"Exercício 4"
var
   m : vetor[1..21, 1..10] de caractere
   nome : caractere
   i, j : inteiro
inicio
   i := 1
   para j de 1 ate 10 faca
      repita
         escreva("Informe o nome: ")
         leia(m[i, j])
         se compr(m[i, j]) > 20 entao
            escreval("Informe um nome com no máx. 20 caracteres.")
         fimse
      ate compr(m[i, j]) <= 20
      fimrepita
   fimpara
   para i de 2 ate 21 faca
      para j de 1 ate compr(m[1, j]) faca
         escreval(copia(m[1, j], j, 1))
      fimpara
   fimpara
fimalgoritmo

I am not able to distribute the letters in the array, and the error in my code says that there is missing a make in the 5th line from the bottom up, but do it there.

    
asked by anonymous 23.05.2017 / 21:19

1 answer

5

You have several errors in your algorithm. I have refitted all of it as per the exercise statement.

Try to use descriptive names for your variables. The second loop was upside down and I think the confusion was precisely because of the variable names. Even though you are accustomed to math and have fixed in mind that i and j are respectively referring to rows and columns it is better to use more descriptive names.

In the first loop it is possible to leave the index of the array row as 1 , after all, the names will only be placed in the first line, as the statement says.

Instead of putting the name typed in the array, even if invalid, I created a variable called nome to validate before if the name was within the rules, if so, it goes to array, otherwise the repita will continue to run.

Until then you were doing well, but I can not understand the purpose of the final block of code.

What I did was to go through all the columns of the array and, for each column, go through all the available rows (2 - 21) causing the column to receive 1 character from the string that is on the first row of this column.

algoritmo "Exercício 4"
var
   matriz: vetor[1..21, 1..10] de caractere
   nome: caractere
   linha, coluna, i : inteiro

inicio
   para coluna de 1 ate 10 faca
      repita
         escreva("Informe o nome: ")
         leia(nome)
         se compr(nome) > 20 entao
            escreval("Informe um nome com no máx. 20 caracteres.")
         fimse
      ate compr(nome) <= 20
      matriz[1, coluna] := nome
   fimpara

   para coluna de 1 ate 10 faca
      i := 1
      para linha de 2 ate 21 faca
         matriz[linha, coluna] := copia(matriz[1, coluna], i, 1)
         i := i + 1
      fimpara
   fimpara   

fimalgoritmo
    
23.05.2017 / 22:25