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:

• 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.

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

//main body

CALL FindWinningCount
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
// HIGHSCORE=0
// HIGHSCOREPOS
// OVER50= 0

loop while VOTES .hasNext() or OVER50 = 0

// 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
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 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]

TOTAL = TOTAL + 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 AND N != CAND – 1)
TEMP = COUNT[N]
TEMP = N + 1
end if
end loop
CAND[CHECK] = TEMP
CHECK = CHECK + 1
if CHECK == 2
output “Candidate “, CAND , “ and Candidate ”, CAND, “ will get into 2nd election.”
end if
end loop 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]

TOTAL = TOTAL + 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 AND N != CAND – 1)
TEMP = COUNT[N]
TEMP = N + 1
end if
end loop
CAND[CHECK] = TEMP
CHECK = CHECK + 1
if CHECK == 2
output “Candidate “, CAND , ” and ”, “ Candidate ”, CAND, “ will get into 2nd election.”
end if
end loop 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 AND N != CAND – 1)” line, I didn’t see you starting an if-statement beforehand. Kenny says August 10, 2017

The code got changed for some reason, different from the original code.. Pete says August 8, 2017

mport random

#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]

winner = 50
if x > winner: 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. June says August 10, 2017 August 9, 2017

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

N1 += 1
N2 += 1
N3+= 1
N4 += 1
N5 += 1

output NAMES “ vote is “ N1
output NAMES “ vote is “ N2
output NAMES “ vote is “ N3
output NAMES “ vote is “ N4
output NAMES “ vote is “ N5 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

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’

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

else:
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 Willy says August 9, 2017

// 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
// 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()
output temp_name + ” got ” + temp_votes ” number of votes.”
output temp_name + ” is the winner.”
else
// Initialize two placeholders -> compare values
if runners_loop_count runner_up_compB then
runner_up_B = NAMES[runners_loop_count]
else
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 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

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’

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

else:
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 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 … 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!”) Natcha says August 15, 2017

NAME=[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) = 5,

else:
pass

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

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’

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

#vote between two candidates
else:
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