Help Portugol(ascending order)

Asked

Viewed 994 times

0

The code below should sort the numbers typed in ascending order, but when typed 5,3 and 8 respectively, the program displays them out of order.

program {

funcao inicio()
{
    //variaveis
    inteiro i
    real a, b, c
    real menor=0.0, meio=0.0, maior=0.0

    leia(i)
    leia(a)
    leia(b)
    leia(c)

    se(i==1){
        se(a>b e b>c)
        maior = a
        meio = b
        menor = c
    }


         senao se(a>c e c>b){
         maior = a
         meio = c
         menor = b
    }


        senao se(b>a e a>c){
         maior = b
         meio = a
         menor = c
         }


        senao se(b>c e c>a){
         maior = b
         meio = c
         menor = a
         }


          senao se(c>a e a>b){
         maior = c
         meio = a
         menor = b
         }

     senao se(c>b e b>a){
         maior = c
         meio = b
         menor = a
         }

       escreva("\nmenor = ", menor)
       escreva("\nmeio = ", meio)
       escreva("\nmaior = ", maior) 

}

}

  • pq there is the variable i and when we should enter the se i==1?

  • The problem proposed by the teacher has several conditions, such as i==2, i==3, but my difficulty is only in the ascending order.

1 answer

1


You have made a small error of attention. See this excerpt from your code:

se(i==1){
        se(a>b e b>c) //Falta a chave de abertura aqui
        maior = a
        meio = b
        menor = c
    }

You opened the keys to parole se(i==1){ correctly, however you have not opened the keys of the next conditional structure, and as variable b ( value 3) is not greater than the variable c ( valid 8) you never entered this parole, mistakenly leaving the code!

To fix insert an opening key just after the se(a>b e b>c) .

Your code will look like this:

    se(i==1){
            se(a>b e b>c){ //Codigo corrigido
            maior = a
            meio = b
            menor = c
        }
...
  • understood, if I want to add another conditional, for example, if (i==2) for descending order, I follow the same model?

  • just reverse the signals, but I’ll do a simulation here, the answer helped you?

  • quite, I thought about the possibility of just reversing the signals even, but I’m not getting any feedback, the program ends.

  • @Joãorosa got it here:( I’ll do it in the first if for example) your current code is like this: se(a>b e b>c){ put it like this: se(b>a e c>b) note that I have not changed the operator > and yes I switched the operating!a>b turned b>a do this in all your code! and after this change the variable menor for maior without exchanging assignment! ie: maior = a meio = c menor = b will stay: menor = a meio = c maior = b

Browser other questions tagged

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