How to build the below algorithm in pseudocode?

Asked

Viewed 81 times

0

Construct an algorithm that can read two vectors of 3 character type elements and ask the user which candidate he wants to vote for and that the result of the operation is stored on other vectors. On-screen demonstration of voting results

My attempt:

Algoritmo "eleicao"

Var

candidato: vetor[1..3] de caractere
resultado: vetor[1..3] de caractere
i: inteiro

Inicio

candidato[1] <- "maria"
candidato[2] <- "joao"
candidato[3] <- "ana"

para i de 1 ate 3 faca
    escreval ("Candidatos")
    escreval ("----------")
    escreval ("maria, joao, ana")
    escreval ()
    escreval ("escolha seu candidato: ")
    leia (resultado[i])
    limpatela
fimpara

Fimalgoritmo
  • 1

    Speak Gabriel, it would be interesting for you to show what you have tried and where you are at the moment, because in the OS we are to help and not to do for you :)

  • Add the code you’ve implemented so far.

  • how I would show the accumulated result?

  • I believe that your vector result should be an integer vector, initialized with zeros and each vote received by the candidate[i] result[i] be increased by 1. At the end result will have the amount of votes received by each candidate. Your loop should be repeated by the total number of voters, not the number of candidates.

1 answer

0


var
Voto: Vetor [1..2] de inteiro
cont: inteiro
Cod: Vetor [1..2] de inteiro
valor1,valor2: inteiro
sair,resultado: caractere

inicio
Escreval ("35 - Fernando")
Cod[1] <- 35
Escreval ("45 - Julia")
Cod[2] <- 45



Cont <- 1
Repita
   Escreval ("Digite o código do candidato desejado:")
   Leia (Voto[cont])

   Se (Voto[cont] = cod[1]) então

      Valor1 <- (Valor1 + 1)

   Fimse

   Se (Voto[cont] = Cod[2]) então

      Valor2 <- (Valor2 + 1)

   Fimse

   Escreval ("Próximo a votar (Caso nenhum, digite SAIR):")
   Leia (sair)



Ate (sair = "SAIR")
Escreval ("Deseja ver o resultado?")
Leia (resultado)

Se (Resultado = "SIM") então
   Escreval ("Total de votos em Fernando:", Valor1)
   Escreval ("Total de votos em Julia:", Valor2)

Fimse

fimalgoritmo

Browser other questions tagged

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