Age accountant

Asked

Viewed 444 times

1

Good afternoon guys, all right?

I have an exercise here from the Faculty of Algorithms I, follow the statement below:

Write an algorithm that asks for the age of several people (USE REPEAT). Please report the total number of people under 25 and the total number of people over 50. The program ends when age is negative (Not to be used in counting).

I’ve done the code so far, just the replay. I’m doubtful how to do the age check to inform the totals.

Code to date:

algoritmo "APS05"
var
   idade:inteiro

inicio

      repita
            escreva("Idade: ")
            leia(idade)
      ate (idade<0)

fimalgoritmo

Thanks for your help.

[RESOLUTION]

repita
            escreva("Idade: ")
            leia(idade)

            se (idade>=0) e (idade<25) entao
               contMenos25 <- contMenos25 + 1
            fimse
            se (idade>50) entao
               contMais50 <- contMais50 + 1
            fimse

      ate (idade<0)

      escreval("Menores que 25: ", contMenos25)
      escreval("Maiores que 50: ", contMais50)
  • A small fix in your code: don’t forget to test se (idade >= 0 e idade < 25) to prevent it from including negative ages in the count, as stated in the exercise.

  • But I put the ate (idade<0) doesn’t solve this?

  • Does not solve because if you type negative age will enter the if before testing age < 0.

  • Perfect now then. Thanks again for your correction and explanation.

1 answer

1


You need a variable contadorMenosDe25Anos integer type starting zero and incremented one each time you read an age between 0 and 25 years. Likewise a variable contadorMaisDe50Anos for ages older than 50.

After the repetition just print the value of these variables.

  • It’s almost there. Remember that increments should be done inside the loop and should be in the form contMenos25 = contMenos25 + 1 (which is the same thing as contMenos25++).

  • But I put them inside a if after the repeat? I didn’t understand it well. The variable was created, but I don’t know where I apply it.

  • 1

    Put in a if after the leia(idade).

  • Solved buddy, I will post edit the post with the resolution.

  • Okay, just don’t erase the code from the original question, put the new code under the old. :)

  • Done. I appreciate the patience anyway. As I’m new here in the stack... Should I do something besides post the resolution? Compliment response, something like?

  • If the answer answered you, you can accept it by clicking on the "v" in the upper left corner of the same.

  • And the more points you have, you can also give +1 in any answer you find useful and correct. Success!

Show 3 more comments

Browser other questions tagged

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