Matrix with strings in Visualg

Asked

Viewed 2,462 times

2

I’m having trouble with the following question:

Create an M[21.10] matrix. Read 10 names (maximum 20 characters) and store in the first row of the matrix. 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 matrix.

inserir a descrição da imagem aqui

My code was as follows:

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’m not being able to distribute the letters in the matrix, and the error in my code says that there’s a do missing in the 5th line from the bottom up, but the do is there.

  • 1

    Haven’t you asked that question already? (I was just reading it)

  • Actually I did this morning too, now I just corrected from "Vector" to "Matrix" in the title. But I asked again this afternoon showing the algorithm, not only asking for the explanation.

  • 1

    Okay, this is not how it should be done. All publications can be edited when you feel like it. As the current publication is better than the other, keep this as it is. The other will be closed.

  • Ah, sorry. I could’ve sworn I deleted the other one.

  • Relax, I’m downloading Visualg to test the code.

  • All right, thanks :)

  • 1

    Related: https://answall.com/questions/206191/algoritmo-para-leitura-scripted https://answall.com/questions/206782/strings-em-matrix

Show 2 more comments

2 answers

5


There are several errors in your algorithm. I have re-read it according to the statement of the exercise.

Try using descriptive names for your variables. The second loop It was backwards and I think the confusion was precisely because of the names of the variables. Even though you’re used to math and have fixed on the mind that i and j are respectively referring to the rows and columns it is best to use more descriptive names.

In the first loop it is possible to leave fixed (hardcoded) the matrix line index as 1, after all, the names will only be placed in the first line, as stated in the statement.

Instead of putting the name typed in the matrix, even if invalid, I created a variable called nome to validate earlier if the name was within the rules, if yes, it goes to the matrix, otherwise the repita continue to perform.

So far you’ve been doing well, but I can’t figure out what the ultimate code block was for.

What I did was scroll through all the columns of the matrix and, for each column, scroll through all the available rows (2 - 21) causing the column to receive 1 character from string which is in 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
  • Thanks for the corrections and tips. At the time of execution is giving the same error that in my algorithm. Says that is missing a knife, but is not not. (fifth row knife from the bottom up)

  • @Jow I edited the answer, had a problem on that same line.

0

I improved the code a little. Now it displays the result.

inserir a descrição da imagem aqui

Browser other questions tagged

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