Naked Science Forum

Non Life Sciences => Geek Speak => Topic started by: kelly_26 on 10/01/2012 16:35:02

Title: help with this algorithm
Post by: kelly_26 on 10/01/2012 16:35:02
hello i just started algorithms and i need help writing a program for this question.
Question: develop an algorithm or write a pseudo-code to determine the winning candidate for a constituency 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 an the number of votes he/she receives.

than you
Title: Re: help with this algorithm
Post by: CliffordK on 10/01/2012 19:10:32
You'll have to write your own algorithm which is generally like a computer program, but with a very lax rule set.

Your overall control structure for the algorithm is up to you.  Most people recommend breaking down the program into smaller components, with each component performing a single function, or a small number of functions.

You will need some kind of a ballot.  Probably with names, and positions on the ballot (A, B, C, etc).
You will likely keep the "counts" in an array, although you could also use some sort of linked list.  Will you allow "write-ins"?  Sometimes just the number of "write-ins" are counted, and then a recount is forced if they are the majority, otherwise only the overall number of write-ins are reported without naming individuals.

You would have some kind of a loop, or perhaps a recursive structure to process all the ballots.  Then, some kind of a loop termination instruction. 

Once finished counting, you would likely choose to order the array by votes...  or perhaps just figure out which candidate had the majority.  Anyway, report all the candidates and votes, as well as reporting the winner.

You may post your answer for critique if you wish.
The exercise really is not to create flawless code, but rather to analyze the steps in program design.
Title: Re: help with this algorithm
Post by: RD on 11/01/2012 03:38:31
You should get extra marks for including an algorithm to sort the results, e.g. ... https://en.wikipedia.org/wiki/Bubble_sort

Your program should cover all eventualities, e.g. a tie , (unlikely, but possible, in reality).
Title: Re: help with this algorithm
Post by: CliffordK on 11/01/2012 04:34:50
You should get extra marks for including an algorithm to sort the results, e.g. ... https://en.wikipedia.org/wiki/Bubble_sort
Bubble Sort?
Real Programmers use recursive QuickSort Routines ;)
http://en.wikipedia.org/wiki/Quicksort

Actually, if you expect, say a half a dozen elements...  Then you would likely choose the simplest sort method.  While quicksort is quick for thousands of elements, it may carry too much overhead for a very short list.

I.E.  You could simply pick out the highest, print it out.
Then pick out the next highest, and print it out.
So-on until you get to the one with the least votes.

For 6 candidates, it would just take 6 passes through the list...
Or.. more accurately 5+4+3+2+1 comparisons.

Your program should be flexible enough to take anywhere from no candidates up to, say 20 candidates.  More?

ZERO CANDIDATES????
Hmmm
Title: Re: help with this algorithm
Post by: nicephotog on 12/01/2012 00:10:58
Throw the results onto an int array, get the array length , place the length as the constraint to be less than in a for loop, compare the first element to the second in a while inside the for-loop constrained with less than the number in the array , if the for loops element[n] is less than another then swap them and terminate the while, to move to the next element[n] of the for.
elements[length-1] should be a pointer
are you using printf("%d", elements[length-1]) or System.out.println("results - "+winner+" "+elements[elements.length-1])
or another language?