Programming logic (Portugol) (Visualg). How can this variable (MA) show me the exact name of the student who got the highest grade?

Asked

Viewed 39 times

0

The program is working perfectly. My doubt is only about this algorithm, I can not understand how this variable (MA) can show me the highest grade only with the attribution MA <- A. If someone can clarify this question I thank.

Algoritmo "ContadorMaiorNota"

Var

   Q, contador: Inteiro

   N, MAIOR: Real

   A, MA: Caractere

Inicio

   contador <- 1

   Escreval("------------------------")

   Escreval(" ESCOLA ESTUDIOSOS ")

   Escreval("------------------------")

   Escreval("Quanto alunos tem na turma?")

   Leia(Q)

   Enquanto (contador <= Q) faca

      Escreval("Aluno ", contador)

      Escreval("Nome do aluno: ")

      Leia(A)

      Escreval("Nota do ", A,":")

      Leia(N)

      contador <- contador +1

      Se (N > MAIOR) entao

         MAIOR <- N

         MA <- A

      FimSe

   FimEnquanto

   Escreval("O maior aproveitamento foi o de ",MA," com a nota", MAIOR)

FimAlgoritmo

1 answer

0


The algorithm is reading names and grades of several students, and at the same time it receives this information, it already calculates which of these students got the highest grade.

So suppose you’re going to insert 3 students and their respective grades:

Amanda, 9
Beatriz, 10
Carol, 8

The moment they pass through this block, the algorithm is just doing what it proposes:

If (N > LARGER) then
GREATER <- N
MA <- A
Fimse

Where N is the current grade, MAJOR is the highest grade recorded so far (which is initially zero), MA is "Highest Student" (the student who took the highest grade), and A is the current student’s name.

Step by step, he did the following:

  1. Registered Amanda, with note 9. As 9 is higher than the initial 0, enters that condition and defines that until then the highest score is 9 and the Highest Student is Amanda.
  2. Registered Beatriz, with note 10. As 10 is greater than 9, enters the condition and defines that until the moment HIGHEST note is 10, and MA is Beatriz.
  3. He registered Carol, with a grade 8. Since 8 is less than 10, so he did not enter the condition. Soon MAJOR note and MA were not changed (continues Beatriz with grade 10).

This clarified that "MA <- A" is simply to keep the name of the current student, if it has entered the condition that the grade was higher than the previous grade?

  • Thank you! It helped a lot in understanding the conditional that I had not attacked me before.

Browser other questions tagged

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