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?