# Develop an algorithm or write pseudocode

1. Jan 2, 2012

### lionely

1. The problem statement, all variables and given/known data

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
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")

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.

2. Jan 2, 2012

### rollcast

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

3. Jan 2, 2012

### lionely

Oh I didn't know you could group them like that or use "and" at the end should I just do

4. Jan 2, 2012

### rollcast

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.

5. Jan 2, 2012

### lionely

Oh, so the way I said to display the last part is correct?

6. Jan 2, 2012

### rollcast

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")

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

7. Jan 2, 2012

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

Also:
Code (Text):
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.

8. Jan 2, 2012

### lionely

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

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?

9. Jan 2, 2012

### rollcast

I edited my early code so it shows the complete piece of pseudo code

10. Jan 2, 2012

### lionely

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

11. Jan 2, 2012

### rollcast

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. Jan 2, 2012

### lionely

Oh! I am going to learn Pascal at school, soon.

13. Jan 2, 2012

### rollcast

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. Jan 2, 2012

### lionely

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. Jan 2, 2012

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 (Text):

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

// Now loop through array to test for maximum:
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 I figure, if it conveys the message, then it's fine.

16. Jan 2, 2012

### lionely

Well I don't really understand you saying I could have used a while loop?

17. Jan 2, 2012

Hi lionely 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 (Text):
theList = [3, 1, 8]
You can access each element of the array by its 'address' or index. So if I write:

Code (Text):
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. Jan 2, 2012

### lionely

MAX = 3 still but if you compare it to theList[3] MAX = 8 I think

19. Jan 2, 2012

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. Jan 2, 2012

### lionely

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.