What happens to the variable i in this algorithm? Sequence inversion algorithm

2

Good evening. I began to study data structure with the support of the book Data Structure and Their 2nd Algorithms (SZWARCFITER / MARKENZON). The first algorithm of the book tries to invert the elements of the sequence in the vector, changing the position of the last element with the first, successively until the last element. The algorithm is this:

algoritmo 1.1: Inversão de uma sequencia
   para i <- 1...[n/2] faça
       temp <- S[i]
       S[i] <- S[n - i + 1]
       S[n -i + 1] <- temp

I wrote the algorithm in Portugol and added a snippet that completes the list.

var


S:VETOR[1..10] de INTEIRO
  i,j,temp,t: INTEIRO
inicio

para i de 1 ate 10 faca
     S[i] <- i
fimpara

para i de 1 ate 10 faca
     escreva(S[i], " ")
fimpara

 Escreval(" ")

 para j de 1 ate 10 faca
      temp <- S[j]
      S[j] <- S[10-j+1]
      t <- S[10-j+1]
      se (j>5) entao
              S[j] <- -j+i
              t <- -j+i
      fimse
      escreva(t, " ")
 fimpara

fimalgoritmo

My question is regarding the i variable. Why does it take the value 11 after printing the list? Before arriving at this code, I used an auxiliary variable k , to list numbers smaller than 6 in reverse order, but I realized that i assumed the value 11 , so I started doing -j + i . Does anyone explain this to me?

    
asked by anonymous 16.06.2016 / 23:40

1 answer

0

Look at what I know about algorithm, portugol and everything else, if you start at 0 or 1 or 2 it does, what matters is that you know what you are doing, so what you can do is: na time you will assign the values of i to another vector, at the same time you assign the inverse already allocates again, it mounted on the main vector.

I'll try to explain with an example:

funcao inversao():inteiro
var
inversor :vetor [0..9] de inteiro // vetor inversor
contador_inversor:inteiro
inicio
escreval (" A ORDEM INVERSA É: ")
contador <- 10
para contador_inversor de 0 ate 12 faca
     inversor[contador_inversor] <- c[contador]
     contador <- contador - 1
fimpara
para contador de 0 ate 12 faca
     c[contador] <- inversor[contador]
     escreva(" ", c[contador])
fimpara
escreval(" ")
fimfuncao

I give you this example for you to take as a base, because it is a project that I did, if it did not work, it tells me that I help you better, but try to interpret from the one that works for me here.

    
17.06.2016 / 19:11