Develop an algorithm or write pseudocode

  • Thread starter Thread starter lionely
  • Start date Start date
  • Tags Tags
    Algorithm
AI Thread Summary
The discussion centers on developing an algorithm or pseudocode to identify the winning candidate in an election based on vote counts. Participants share initial attempts at pseudocode, highlighting the need to compare vote totals and determine the maximum. Suggestions include using arrays to simplify the process of finding the maximum vote and addressing potential issues such as invalid inputs or ties. The conversation emphasizes the importance of clear logic in pseudocode and the ability to translate it into actual programming languages. Overall, the focus is on refining the algorithm to ensure accurate results in determining the election winner.
lionely
Messages
574
Reaction score
2

Homework Statement



Develop an algorithm or write pseudocode to determine the winning candidate for a constituency in
the national elections. The algorithm must accept as input the names of the four candidates and the
number of votes each candidate receives. The successful candidate is the one who received the most votes.
Print the name of the winner and the number of votes he/she received.


Well, I'm not too sure about how to solve this problem. So I was wondering if anyone could give any hints ?

Attempt:

START
Declare: winning_cand,cand1,cand2,cand3,cand4 ---> string
vote1,vote2,vote3,vote4,winning_vote----> integer
Print("enter four names")
read cand1,cand2,cand3,cand4
Print("enter number of votes")
read vote1,vote2,vote3,vote4

If vote 1<vote2 then
vote 2 = winning_vote
else
vote2<vote3 then
vote3=winning_vote
else
vote3<vote4 then
vote4=winning_vote
endif
endif
endif

Then after that I'm not too sure.
 
Physics news on Phys.org
You could use something like this to decide the wining vote.

if (vote1 > vote2 and vote1> vote 3 and vote1 > vote4) then
vote1=winning_vote
 
Oh I didn't know you could group them like that or use "and" at the end should I just do

Read(''winning_cand,winning_vote")
 
I've never really studied pseudo code in much detail, the technology teacher that first introduced it was like watching paint dry so I've always found it boring but that is pretty much a simplification of how I would do that in C# or C.
 
Oh, so the way I said to display the last part is correct?
 
Sorry I didn't see the last part I would integrate the if clauses to find the winner and the output.

START
Declare: winning_cand,cand1,cand2,cand3,cand4 ---> string
vote1,vote2,vote3,vote4,winning_vote----> integer
Print("enter four names")
read cand1,cand2,cand3,cand4
Print("enter number of votes")
read vote1,vote2,vote3,vote4

if (vote1 > vote 2) and (vote1 > vote3) and (vote1 > vote 4) then
print("The winner is cand1 with vote1")
else
(vote2 > vote 1) and (vote2 > vote3) and (vote2 > vote 4) then
print("The winner is cand2 with vote2")
else
(vote3 > vote 1) and (vote3 > vote2) and (vote3 > vote 4) then
print("The winner is cand3 with vote3")
else
(vote4 > vote 1) and (vote4 > vote2) and (vote4 > vote 3) then
print("The winner is cand4 with vote3")

END

That removes several variables from your problem and shortens the overall code
 
lionely said:
Oh, so the way I said to display the last part is correct?

I think that 'write' sounds like a better way to display something yes?

Also:
Code:
if (vote1 > vote2 and vote1> vote 3 and vote1 > vote4) then
vote1=winning_vote
only tests to see if vote1 is the winning vote. I believe you would have to repeat for the 3 other votes as well.
 
START
Declare: winning_cand,cand1,cand2,cand3,cand4 ---> string
vote1,vote2,vote3,vote4,winning_vote----> integer
Print("enter four names")
read cand1,cand2,cand3,cand4
Print("enter number of votes")
read vote1,vote2,vote3,vote4

if (vote1 > vote2 and vote1> vote 3 and vote1 > vote4) then
vote1=winning_vote
print("The winner is cand1 with vote1")
endif
STOP

So this is it?
 
I edited my early code so it shows the complete piece of pseudo code
 
  • #10
Oh thank you rollcast and Saladsamurai for your time xD.

I don't really understand pseudocode that well do you have any tips to make it easier?
 
  • #11
It probably would be easier if you learn even just the basics of a computer language as at least you can test your code and see if it works.

Then just apply your coding knowledge to the pseudo code problems
 
  • #12
Oh! I am going to learn Pascal at school, soon.
 
  • #13
If you're going to learn Pascal in school then pseudo code should be a breeze once you understand the basics of coding.

I don't know any Pascal but while its a good language to learn with its not a big language for industry or commercial or business applications.
 
  • #14
Yeah So I heard. But it's on your I.T. syllabus for exams. So it's a must in the next grade we'll do C and C++ I think.
 
  • #15
lionely said:
Oh thank you rollcast and Saladsamurai for your time xD.

I don't really understand pseudocode that well do you have any tips to make it easier?

If you want an easy way to do this, notice that you are essentially looking for the maximum of the 4 vote values. A simple way to test for a maximum is ti store all of the values to be tested in an array. You can then loop through all of the values and test to see if the current value is greater than the prior value: (when you see // it just means that is a comment and not a part of the code)

Code:
// Get all of the vote values from user and then test:
For i = 1 To 4
    Read vote[i]
Next i 

// Now loop through array to test for maximum:
winner = 0    // <-- start with zero
For i = 1 To 4
   if vote[1] > winner then
      winner = vote[i]
   end if
Next i

// announce winner:
print "The winner is: " + winner

If this does not make sense, let me know. Also: I stink at pseudocode :wink: I figure, if it conveys the message, then it's fine.
 
  • #16
Well I don't really understand you saying I could have used a while loop?
 
  • #17
lionely said:
Well I don't really understand you saying I could have used a while loop?

Hi lionely :smile: Well, I used a For Loop. If you have not seen this yet, it is basically the same as a While Loop. So rather than writing all of those if statements, you just write one inside of a loop. All you need to do is test each value of vote to see if it is greater than the last value.

Try this example "by hand" :

Take an array (or 'list' if you like) of three numbers:

Code:
theList = [3, 1, 8]

You can access each element of the array by its 'address' or index. So if I write:

Code:
Print theList[2]

I mean "Print the second element in the list called "theList".

That being said, say I have a variable called MAX. First start by setting MAX = 0. Now compare the value of MAX with each of the values in theList[]. If the value in theList[] is greater than MAX, then assign the new value to MAX.

So start by comparing the value in MAX (which is currently zero) to the value of theList[1] which is equal to '3'.

Since 3 > 0, we replace the current value of MAX with the value of theList[1].

So now MAX = 3

Now start over: Compare the current value of MAX (which is 3) to theList[2].

Now what happens?
 
  • #18
MAX = 3 still but if you compare it to theList[3] MAX = 8 I think O.o
 
  • #19
lionely said:
MAX = 3 still but if you compare it to theList[3] MAX = 8 I think O.o

Exactly!

If theList = [ 9, 3, 5, 7] then you can see that MAX will always stay at MAX = 9 since it will always fail the 'greater than' test.

Doe the original 'code' I wrote in post #15 make sense now?
 
  • #20
I think so, it will just take the highest value of the array and that will be the winning vote? So what you did shortens the code a lot.
 
  • #21
lionely said:
I think so, it will just take the highest value of the array and that will be the winning vote? So what you did shortens the code a lot.


Sure does. Also, if you are looking to impress your professor (I am assuming this is HW) you should think about where this "code" can go wrong. I think of 2 ways that the test: Is theList > MAX could fail to give usable results, can you?

Hint: What kind of input could botch the results?
 
  • #22
Hm... If someone entered a value less than 0? It would mess up the results,because I don't see a condition for if it's less than 0. I'm not really sure.
 
  • #23
lionely said:
Hm... If someone entered a value less than 0? It would mess up the results,because I don't see a condition for if it's less than 0. I'm not really sure.

That would be a third case :smile:

What if all of the values were 0? At least one value of vote must be greater than zero for the variable winner to get updated.

What if there is a tie? Will the program notice?

Just some things to think about.
 
  • #24
If the value was 0 wouldn't the program just terminate?
 
  • #25
lionely said:
If the value was 0 wouldn't the program just terminate?

Yes.You are correct. I left out a detail in the pseudocode: we need to keep track of who has the max votes, not just how many the max votes is. The program would output "The winner is: 0"

which just sounds silly.
 
  • #26
So should we change something in post #15?
 
  • #27
rollcast said:
I've never really studied pseudo code in much detail, the technology teacher that first introduced it was like watching paint dry so I've always found it boring but that is pretty much a simplification of how I would do that in C# or C.

Pseudocode isn't a programming language, per se, and I don't believe there is any standard definition for what it entails. The idea with pseudocode is to be able to write the algorithm so that it can eventually be implemented in some actual programming language. If the algorithm is well designed, it can be implemented in pretty much any programming language.
 
Back
Top