Difficulty with loop (repetition) in portugol

Asked

Viewed 290 times

2

I’ve got this exercise to do:

Make a program that asks the user the amount of power plants energy in a region, after reading this amount of plants, the program requests for each plant what type of power plant (h-hydroelectric T- Electronic E- eolica and what power generated, after reading the data of all plants the program accounts for the total energy generated for each type and evaluates in which flag the region should operate. when more than 20% of the energy generated is from Termoeletrica operates in red flag. when the thermal electric generation is between 10 and 20 % of the region operates under the yellow flag.

But I’m having a hard time. I did this part of the code, but I can’t make the repetition work in a way that repeats until the number of plants entered by the user:

algoritmo "semnome"
// Função :
// Autor :
// Data : 08/05/2015

// Seção de Declarações
var nu,cont :inteiro tipo :caractere

inicio

    cont <- 1
    escreval("digite a qtd de usinas")
    leia (nu)
    repita
        escreval("digite o tipo de cada usina ")
        leia(tipo)
    ate (cont = nu)
    // Seção de Comandos

fimalgoritmo
  • 4

    Hello. Welcome to SOPT. Please read [help] and mainly [Ask]. The purpose of the site is not to do your homework. If you have specific questions about your problem, especially about something you’ve already tried (in this case, post the part of the code you’ve already done!), we’ll certainly help. Otherwise, the question will simply be too specific to be useful to anyone other than you.

  • 1

    after requesting the number of power plants, I am not able to repeat the number of power plants specified by the user, and this part is essential to manage the remaining code .

  • You will notice that I edited your question (you could have done it yourself) to include your sample code. It looks much better than in the comments. Still, as I said, you lack to explain exactly where you are with doubts. So, don’t forget to [Edit] the question to include this type of information.

  • I withdrew my vote to close the question. Please put this explanation of doubt in the question. :)

  • 1

    Use a loop for(for) or while(while), that answer maybe I can help.

1 answer

2

You have a loop to request the type of mills. Your control condition is:

até (cont = nu)

being nu the number of power plants entered by the user. The variable cont is your loop counter, which you correctly initialize with 1 in:

cont <- 1

The problem with your tie is that it never ends, because you does not update the control variable cont. For this, inside the loop, and as the last instruction, increment this variable in 1 unit (to indicate that 1 plant has already been processed):

repita
    [...]
    cont <- cont + 1
até (cont = nu)
  • 1

    ok Thank you very much follow your instructions and now I can perform the loop correctly with the variable control.

Browser other questions tagged

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