1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Develop an algorithm or write pseudocode

  1. Jan 2, 2012 #1
    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
    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.
     
  2. jcsd
  3. Jan 2, 2012 #2
    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
     
  4. Jan 2, 2012 #3
    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")
     
  5. Jan 2, 2012 #4
    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.
     
  6. Jan 2, 2012 #5
    Oh, so the way I said to display the last part is correct?
     
  7. Jan 2, 2012 #6
    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
     
  8. Jan 2, 2012 #7
    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.
     
  9. Jan 2, 2012 #8
    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?
     
  10. Jan 2, 2012 #9
    I edited my early code so it shows the complete piece of pseudo code
     
  11. Jan 2, 2012 #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?
     
  12. Jan 2, 2012 #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
     
  13. Jan 2, 2012 #12
    Oh! I am going to learn Pascal at school, soon.
     
  14. Jan 2, 2012 #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.
     
  15. Jan 2, 2012 #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.
     
  16. Jan 2, 2012 #15
    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
        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.
     
  17. Jan 2, 2012 #16
    Well I don't really understand you saying I could have used a while loop?
     
  18. Jan 2, 2012 #17
    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 (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?
     
  19. Jan 2, 2012 #18
    MAX = 3 still but if you compare it to theList[3] MAX = 8 I think o_O
     
  20. Jan 2, 2012 #19
    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?
     
  21. Jan 2, 2012 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Develop an algorithm or write pseudocode
  1. Pseudocode problem (Replies: 5)

  2. Pseudocodes help (Replies: 2)

Loading...