Develop an algorithm or write pseudocode

  • Thread starter lionely
  • Start date
  • Tags
    Algorithm
In summary, the algorithm to determine the winning candidate for a constituency in the national elections is to 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.
  • #1
lionely
576
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
  • #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
 
  • #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")
 
  • #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.
 
  • #5
Oh, so the way I said to display the last part is correct?
 
  • #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
 
  • #7
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.
 
  • #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?
 
  • #9
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.
 

1. What is an algorithm?

An algorithm is a set of step-by-step instructions or rules that are followed to solve a problem or complete a task. It is a fundamental concept in computer science and is used to design and analyze computer programs.

2. How do I develop an algorithm?

Developing an algorithm involves breaking down a problem into smaller, more manageable steps. These steps should be clear and specific, with a defined input and output. It is important to consider edge cases and potential errors in the algorithm design process.

3. What is pseudocode?

Pseudocode is a simplified programming language that is used to describe the logic of an algorithm. It is not specific to any programming language and uses plain English to outline the steps of an algorithm. Pseudocode can help to plan out an algorithm before writing actual code.

4. How do I write pseudocode?

Writing pseudocode involves using a combination of programming language syntax and plain English to describe the steps of an algorithm. It should be easy to understand and follow, and should accurately represent the logic and structure of the algorithm.

5. Why is developing an algorithm important?

Developing an algorithm is important because it helps to solve complex problems in an efficient and systematic way. Algorithms are the building blocks of computer programs and are used in various fields such as data analysis, artificial intelligence, and software development.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
45
Views
16K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
7K
  • Programming and Computer Science
Replies
5
Views
4K
Back
Top