How to solve this "Error in syntax"

Asked

Viewed 760 times

1

I have the following question and cannot think of a solution:

Make an algorithm that reads the duration 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

    Which syntax error and where? Which Portugol IDE is using?

  • The syntax error is linked to the code grammar, this being stated, as mentioned in the comment above, without making explicit the IDE you are using, there is no way to respond. However, every IDE (unless mistaken) has documentation in the correct form of the syntax,

  • This question is being debated at the goal: https://pt.meta.stackoverflow.com/q/6499/132

2 answers

0

The operator mod or % requires an integer type variable. In your case, you set the "seconds" variable 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.

@Edition: 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

0


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 was able to understand in your code that the variables are real, so it’s not feasible to use the % operator to do the rest of the division. Using the variables as integer you can do so:

  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 change of the variable type of real for inteiro?
Operators \(division) and %(rest) are used for divisions between integer values, which must always result in an integer quotient and an integer rest.

Browser other questions tagged

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