Your Solutions

Some good solutions, before I make further comment I want you guys to comment on at least 2 other solutions Some examples ( I like .... ) I don't understand why you did...  It would be easier to read if you indented, wrong use of permitted pseudo code, its wrong because. Also consider if the solution efficient .

An election is held in school to elect a president for the student council. There are five candidates, each one represented by a number from

1 – 5. Their names are stored in a corresponding array NAMES.

The students vote electronically and the numbers are entered into the collection VOTES. If any candidate gets more than 50% of the votes, that person is elected, otherwise there is a second vote between the two candidates with the highest number of votes.

Write an algorithm in pseudo code to do the following:

  • read the values from the collection VOTES
  • count the number of votes for each candidate
  • output the result
  • state the name of the winner or of the two candidates in the next round.

You should use the data structures given and may introduce any others that you think are useful for an efficient solution.

// init array case some candidates receive no votes

Loop N from 0 to 5
   RESULTS[N]=0
End loop

//main body

CALL FindWinningCount
CALL ProcessVotes
// output all votes
Loop N from 0 to 5
    output NAMES[N] : "received ": RESULTS[N]
end loop


If OVER50 then
    Output "winner is : NAMES[HIGHSCOREPOS]
else
// output 2 with highest votes for next round
   CALL FindOtherCandidate
   Output NAMES[HIGHSCOREPOS] " and " NAMES[HIGHSCOREPOS2] " Will proceed to next round
end if

// end main body

// sub process votes and store highest and maybe winner
ProcessVotes
// HIGHSCORE=0
// HIGHSCOREPOS
// OVER50= 0

VOTES.resetNext() // start at beginning

loop while VOTES .hasNext() or OVER50 = 0

  STUDENTVOTED = VOTES .getNext()
  // store votes in array results need convert candidate number to pos in array so reduce by 1
  RESULTS [STUDENTVOTED-1] = RESULTS [STUDENTVOTED-1] + 1
  If RESULTS [STUDENTVOTED-1] > HIGHSCORE
  HIGHSCOREPOS = STUDENTVOTED-1
  HIGHSCORE = RESULTS [STUDENTVOTED-1]
end if
If RESULTS [STUDENTVOTED-1] > HALFVOTES then
  OVEVER50 = 1
end if

end loop

// sub to count the total number of students that voted so can check for winner

FindWinningCount
VOTES.resetNext() // start at beginning
loop while VOTES .hasNext()
   TOTALNUMVOTES = TOTALNUMVOTES +1
end loop
// find number to win
HALFVOTES = TOTALNUMVOTES / 2 // assume can we use standard operators

// find the second candidate
FindOtherCandidate
HIGHSCOREPOS2 = 0
Loop N from 0 to 5
  SCORE = RESULTS[N]
  If SCORE > HIGHSCOREPOS2 and N ≠ HIGHSCOREPOS then
     HIGHSCOREPOS2 = N
     HIGHSCORE2 = SCORE
end if
end loop

// find the second candidate
FindOtherCandidate
HIGHSCOREPOS2 = 0
Loop N from 0 to 5
  SCORE = RESULTS[N]
  If SCORE > HIGHSCOREPOS2 and N ≠ HIGHSCOREPOS then
     HIGHSCOREPOS2 = N
     HIGHSCORE2 = SCORE
end if
end loop

Leave a Comment:

14 comments
Kenny says August 8, 2017

NAMES = [1,2,3,4,5]
VOTES=[Election.getVote()] // [1,5,3,2,4,5,1 …] Assuming the votes number represent the candidate name?

COUNT = [0,0,0,0,0]
TOTAL = 0
CHECK = 0
CAND = [0,0]
TEMP = [0,0]

loop while VOTES.hasNext()
TOTAL = TOTAL + 1
COUNT[VOTES.getNext() – 1] = COUNT[VOTES.getNext() – 1] + 1
end loop

loop C from 0 to 4
output “Candidate “, C+1 , “ have ”, COUNT[C], “ votes.”
end loop

loop while (CHECK TOTAL div 2)
output “Candidate ”, N+1, “ win the election.”
CHECK = 2
else if (COUNT[N] > TEMP[0] AND N != CAND[0] – 1)
TEMP[0] = COUNT[N]
TEMP[1] = N + 1
end if
end loop
CAND[CHECK] = TEMP[1]
CHECK = CHECK + 1
if CHECK == 2
output “Candidate “, CAND[0] , “ and Candidate ”, CAND[1], “ will get into 2nd election.”
end if
end loop

Reply
    Kenny says August 10, 2017

    *Correction:
    NAMES = [1,2,3,4,5]
    VOTES=[Election.getVote()] // [1,5,3,2,4,5,1 …] Assuming the votes number represent the candidate name?
    COUNT = [0,0,0,0,0]
    TOTAL = 0
    CHECK = 0
    CAND = [0,0]
    TEMP = [0,0]

    loop while VOTES.hasNext()
    TOTAL = TOTAL + 1
    COUNT[VOTES.getNext() – 1] = COUNT[VOTES.getNext() – 1] + 1
    end loop

    loop C from 0 to 4
    output “Candidate “, C+1 , ” have ”, COUNT[C], “ votes.”
    end loop

    loop while (CHECK TOTAL div 2)
    output “Candidate ”, N+1, “ win the election.”
    CHECK = 2
    else if (COUNT[N] > TEMP[0] AND N != CAND[0] – 1)
    TEMP[0] = COUNT[N]
    TEMP[1] = N + 1
    end if
    end loop
    CAND[CHECK] = TEMP[1]
    CHECK = CHECK + 1
    if CHECK == 2
    output “Candidate “, CAND[0] , ” and ”, “ Candidate ”, CAND[1], “ will get into 2nd election.”
    end if
    end loop

    Reply
    Willy says August 10, 2017

    I thought their actual names are what is stored in the NAMES array, not their representative number (aren’t those are used only in VOTES?). If it’s so, then you might have to edit your “loop C” part to include the candidate’s names.

    I’m confused about your “loop while (CHECK TOTAL div 2)” line, aren’t CHECK and TOTAL two separate variables that you’ve previously defined & modified? Then are the two multiplied? added?
    Also, the “else if (COUNT[N] > TEMP[0] AND N != CAND[0] – 1)” line, I didn’t see you starting an if-statement beforehand.

    Reply
      Kenny says August 10, 2017

      The code got changed for some reason, different from the original code..

      Reply
Pete says August 8, 2017

mport random

votes = 100
#list
NAMES = [“John”, “David”, “Martha”, “Jessica”, “Howard”]

John = 10
David = 15
Martha = 50
Jessica = 20
Howard = 5
print (NAMES)

VOTES = [John , David, Martha, Jessica, Howard]
print (VOTES)

while VOTES:
winner = 50
for x in VOTES:
if x > winner:
print (VOTES)

Reply
    Willy says August 10, 2017

    I think the votes for each candidate is already “given”, since we’re asked to read it from VOTES; the amount or how many there are are not needed, I think, since we can just keep looping through the array until there’s nothing in it (loop while VOTES.hasNext; something like that).
    So instead of you setting the votes yourself, I think you’re supposed to read it the votes (ex. [1, 5, 3, 4, 2, 1, 2, 4, 1, …]) and count how many each candidate got.

    Reply
      June says August 10, 2017

      Oh, votes were already set… RIP

      Reply
Sanan Amnartsitthichai (Jia Bao) says August 9, 2017

NAMES = [“1”, “2”, “3”, “4”, “5”]
VOTES = [random votes]
N1 = 0
N2 = 0
N3 = 0
N4 = 0
N5 = 0

loop while VOTES.hasNext()
MYSTACK.push( VOTES.getNext() )
if VOTES[] = NAMES[0]
N1 += 1
else if VOTES[] = NAMES[1]
N2 += 1
else if VOTES[] = NAMES[2]
N3+= 1
else if VOTES[] = NAMES[3]
N4 += 1
else if VOTES[] = NAMES[4]
N5 += 1

output NAMES[0] “ vote is “ N1
output NAMES[1] “ vote is “ N2
output NAMES[2] “ vote is “ N3
output NAMES[3] “ vote is “ N4
output NAMES[4] “ vote is “ N5

Reply
June says August 9, 2017

NAMES = [1:A,2:B,3:C,4:D,5:E]

a=0
b=0
c=0
d=0
e=0

if user input = 1,
a += (a+1)
elif user input = 2,
b += (b+1)
elif user input = 3,
c += (c+1)
elif user input = 4,
d += (d+1)
elif user input = 5,
e += (e+1)

else:
pass

VOTES = [A:a,B:b,C:c,D:d,E:e]

print VOTES

print ‘Candidate A:’ + ‘A’ + ‘votes’
print ‘Candidate B:’ + ‘B’ + ‘votes’
print ‘Candidate C:’ + ‘C’ + ‘votes’
print ‘Candidate D:’ + ‘D’ + ‘votes’
print ‘Candidate E:’ + ‘E’ + ‘votes’

For x in len(VOTES)
while x>1:
if len(x) 0.5:
print(‘Candidate’ + ‘SORTED_VOTES(0)’ + ‘has been elected’)

else:
SecondVote += SORTED_VOTES(0), SORTED_VOTES(1)
if user input = 1:
SecondVote(0) += SecondVote(0)+1
elif user input = 2:
SecondVote(1) += SecondVote(1)+1

elif SecondVote(0) > SecondVote(1):
print (SecondVote(0) wins)
elif SecondVote(1) > SecondVote(0):
print SecondVote(1) wins

else:
pass

Reply
Willy says August 9, 2017

// Assume VOTES already contains the votes for each candidate?
// Initialize counting
VOTE_COUNT = [0, 0, 0, 0, 0]
totalvote = 0
runners_loop_count = 0
runner_up_compA = 0
runner_up_compB = 0
runner_up_A = 0
runner_up_B = 0

// Loop through VOTES, count the numbers
loop while VOTES.hasNext()
temp_vote = VOTES.getNext()
// Check who is voted & store it
VOTE_COUNT[temp_vote – 1] = VOTE_COUNT[temp_vote – 1] + temp_vote
totalvote = totalvote + 1
end loop
VOTE_COUNT.resetNext()
// Output VOTE_COUNT
loop while NAMES.hasNext()
runners_loop_count = runners_loop_count + 1
temp_name = NAMES.getNext()
temp_votes = VOTE_COUNT.getNext()
output temp_name + ” got ” + temp_votes ” number of votes.”
if temp_votes > totalvote/2 then
output temp_name + ” is the winner.”
else
// Initialize two placeholders -> compare values
if runners_loop_count runner_up_compB then
runner_up_compB = temp_votes
runner_up_B = NAMES[runners_loop_count]
else
runner_up_compA = temp_votes
runner_up_A = = NAMES[runners_loop_count]
end if
end if
if runners_loop_count = 5 then
output runner_up_A + ” with votes of ” + runner_up_compA + “and ” + runner_up_B + ” with votes of ” + runner_up_compB + ” are to vote again next round.”
end if
end if
end loop

Reply
June says August 9, 2017

*recommenting as i don’t think it came up before

NAMES = [1:A,2:B,3:C,4:D,5:E]

a=0
b=0
c=0
d=0
e=0

if user input = 1,
a += (a+1)
elif user input = 2,
b += (b+1)
elif user input = 3,
c += (c+1)
elif user input = 4,
d += (d+1)
elif user input = 5,
e += (e+1)

else:
pass

VOTES = [A:a,B:b,C:c,D:d,E:e]

print VOTES

print ‘Candidate A:’ + ‘A’ + ‘votes’
print ‘Candidate B:’ + ‘B’ + ‘votes’
print ‘Candidate C:’ + ‘C’ + ‘votes’
print ‘Candidate D:’ + ‘D’ + ‘votes’
print ‘Candidate E:’ + ‘E’ + ‘votes’

For x in len(VOTES)
while x>1:
if len(x) 0.5:
print(‘Candidate’ + ‘SORTED_VOTES(0)’ + ‘has been elected’)

else:
SecondVote += SORTED_VOTES(0), SORTED_VOTES(1)
if user input = 1:
SecondVote(0) += SecondVote(0)+1
elif user input = 2:
SecondVote(1) += SecondVote(1)+1

elif SecondVote(0) > SecondVote(1):
print (SecondVote(0) wins)
elif SecondVote(1) > SecondVote(0):
print SecondVote(1) wins

else:
pass

Reply
Eamonn says August 10, 2017

Well done Willy your comments to the others are correct and helpful. My advice to others would be to retry with the given fact that the names are stored in names array [“Bob” “Mark”….] so a vote 1 in the votes collection equates to a vote for Bob position zero in the array ( array starts from zero. ) Willy can your double check the pseudo code that outputs the 2 runner ups. I a have pasted in my solution above ( I prefer to breakdown the problem into functions/procedures giving them names that are meaningful ( You do need to take this approach, but it may help …

Reply
Jimmy Noriya says August 11, 2017

a = 0
b = 0
c = 0
d = 0
e = 0

if userinput = 1
a = a + 1
elif userinput = 2
b = b + 1
elif userinput = 3
c = c + 1
elif userinput = 4
d = d + 1
elif userinput = 5
e = e + 1

Names= [a,b,c,d,e]

Print Names

count = 0

while count = 4
percent = Names[count]/(a + b + c + d + e)
If percent >= 0.5
end loop
else:
pass
count += 1

print(int(percent) + ” is the winner!”)

Reply
Natcha says August 15, 2017

NAME=[1:A, 2:B, 3:C, 4:D, 5:E]

#count votes
a=0
b=0
c=0
d=0
e=0

if user input = 1,
a += (a+1)
elif user input = 2,
b += (b+1)
elif user input = 3,
c += (c+1)
elif user input = 4,
d += (d+1) = 5,

else:
pass

#print votes

VOTES = [A:a, B:b, C:c, D:d, E:e]

print VOTES

print ‘Candidate A:’ + ‘has’ + ‘A’ + ‘votes’
print ‘Candidate B:’ + ‘has’ + ‘B’ + ‘votes’
print ‘Candidate C:’ + ‘has’ + ‘C’ + ‘votes’
print ‘Candidate D:’ + ‘has’ + ‘D’ + ‘votes’
print ‘Candidate E:’ + ‘has’ + ‘E’ + ‘votes’

for x in len(VOTES)
while x>1:
if len(x) 0.5:
print(‘Candidate’ + ‘SORTED_VOTES(0)’ + ‘is the elected president.’)

#vote between two candidates
else:
Second_Vote += SORTED_VOTES(0),
SORTED_VOTES(1)
if user input = 1:
Second_Vote(0) += Second_Vote(0)+1

elif user input = 2:
Second_Vote(1) += Second_Vote(1)+1

elif Second_Vote(0) > Second_Vote(1)+1
print(SecondVote(0) wins)
elif Second_Vote(1) > Second_Vote(0)
print (SecondVote(1) win)

else:
pass

Reply
Add Your Reply