How to solve this "syntax error" [closed]
want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 3 years ago .
improve this questionI have the following question and can't think of a solution:
Make an algorithm that reads the duration time of an event in seconds and shows it expressed in hours, minutes and seconds.
My Code:
algoritmo "Duração de Evento"
var
segundos, sec, horas, minutos : real
inicio
escreva("Quantos segundos o Evento dura?")
leia(segundos)
horas<-segundos/3600
minutos<-(segundos%3600)/60
sec<-(segundos%3600)%60
escreva(horas,":",minutos,":",segundos)
fimalgoritmo
I always get: error in syntax
2 answers
Your Code:
algoritmo "Duração de Evento"
var
segundos, sec, horas, minutos : real
inicio
escreva("Quantos segundos o Evento dura?")
leia(segundos)
horas<-segundos/3600
minutos<-(segundos%3600)/60
sec<-(segundos%3600)%60
escreva(horas,":",minutos,":",segundos)
fimalgoritmo
I managed to realize in your code that the variables are as real, so it will not be feasible to use the % operator to do the rest of the division. Using the variables as integer you can do like this:
algoritmo "Duração_de_Evento"
var
segundos, sec, horas, minutos : inteiro
inicio
escreval("Quantos segundos o Evento dura?")
leia(segundos)
horas<-segundos\3600
minutos<-(segundos%3600)\60
sec<-(segundos%3600)%60
escreval(horas,":",minutos,":",segundos)
fimalgoritmo
EXTRA
Now You may be wondering why the need to change the variable type from real
to inteiro
?
the operators \
(division) and %
(rest) are used for divisions between values integers, which should always result in an integer quotient and an integer remainder.
The operator mod
or %
requires a variable of the integer type. In your case, you set the variable "seconds" to real, so you need to reset it to integer. The code would look like this:
algoritmo "Duração de Evento"
var
sec, horas, minutos : real
segundos : inteiro
inicio
escreva("Quantos segundos o Evento dura?")
leia(segundos)
horas<-segundos/3600
minutos<-(segundos%3600)/60
sec<-(segundos%3600)%60
escreva(horas,":",minutos,":",segundos)
fimalgoritmo
I believe that this code does not correctly meet the proposal of the question, but the Syntax Error no longer occurs.
@ Edit:in this case, one of the correct solutions to your question would be:
algoritmo "Segundos em hora, minutos e segundos"
var
TS, S, M, H: Inteiro // TS - Tempo em Segundos, S - Segundos, M - Minutos, H - Horas
inicio
EscrevaL("Informe o tempo em segundos: ")
Leia(TS)
H <- TS div 3600
M <- (TS - (H * 3600)) div 60
S <- TS - (H * 3600) - (M * 60)
EscrevaL("---------")
EscrevaL(" Horário ")
EscrevaL("---------")
Escreva(H," :", M," :", S)
fimalgoritmo