What’s wrong with the program?

Asked

Viewed 69 times

0

Algoritmo "semnome"

Var
n1, n2,maior,menor, diferenca: real


Inicio
escreval("diferença do maior pelo menor")
escreva ("numero1 = ")
leia (n1)
escreva ("numero2 = ")
leia (n2)
se (n1>n2) entao
n1 <- maior
n2 <- menor
senao
n1 <- menor
n2 <- maior

fimse
diferenca <- (maior-menor)
escreva (diferenca)

Fimalgoritmo

any result set from zero as a difference

  • 4

    Shouldn’t be maior <- n1 etc.?

1 answer

1

As the bfavaretto commented the error happens by this inversion.

In your conditional structure you set the variable n1 and n2 with the variable maior and menor being that smaller and larger were declared no more had assigned values. .

Try this:

se (n1>n2) entao
   maior <- n1
   menor <- n2
senao
   maior <- n2
   menor <- n1
fimse

Making the passo a passo in Visualg to identify the error: inserir a descrição da imagem aqui

Where the mistake happens: inserir a descrição da imagem aqui


Following your idea I set up a scheme:

Code:

algoritmo "semnome"
// Função :
// Autor :
// Data : 28/04/2018
// Seção de Declarações 
var
n1, n2, diferenca: real

inicio
// Seção de Comandos
escreval("=============================")
escreval("diferença do maior pelo menor")
escreval("=============================")
escreval("")
escreva("Informe o 1º número: ")
leia(n1)
escreva("Informe o 2º número: ")
leia(n2)

limpatela

se (n1 > n2) entao
   diferenca <- (n1 - n2)
   escreval("============================================================")
   escreval("o número ", n1, " é maior que o número ", n2, " e a diferença é ", diferenca)
   escreval("============================================================")
senao
   se (n1 < n2) entao
      diferenca <- (n2 - n1)
      escreval("============================================================")
      escreval("o número ", n2, " é maior que o número ", n1, " e a diferença é ", diferenca)
      escreval("============================================================")
   senao
      escreval("Os números são iguas!")
   fimse
fimse
fimalgoritmo

See working: inserir a descrição da imagem aqui

  • The tutorial found beautiful, but found unnecessary. The resolution before the tutorial, however, does not say what was the problem. The images didn’t render well either. There are texts that you put on the terminal print that would fit better in the running text of the answer, and that would be easier to read (yes, I could not read the texts in the prints). I also found that your sample code is worse architect and more repetitive than the original question.

  • My intention was to show how it is possible to identify a problem using the visualg step by step. I can see the images perfectly from both a smartphone and a monitor. As for the text on the prints has nothing d+ it was my choice to flag. Thank you for the criticism, the important thing is that the author of the question understands and manages to resolve the issue. As for the resolution at the beginning, thank you.

Browser other questions tagged

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