voting system


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: voting system

  1. #1
    Join Date
    Aug 2005
    Posts
    10

    voting system

    Australian Voting
    Australian ballots require that the voter rank the candidates in order of choice. Initially only the first choices are counted and if one candidate receives more than 50% of the vote, that candidate is elected. If no candidate receives more than 50%, all candidates tied for the lowest number of votes are eliminated. Ballots ranking these candidates first are recounted in favour of their highest ranked candidate who has not been eliminated. This process continues [that is, the lowest candidate is eliminated and each ballot is counted in favour of its ranked non-eliminated candidate] until one candidate receives more than 50% of the vote or until all candidates are tied.
    Input
    The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

    The first line of input is an integer n <= 20 indicating the number of candidates. The next n lines consist of the names of the candidates in order. Names may be up to 80 characters in length and may contain any printable characters. Up to 1000 lines follow; each contains the contents of a ballot. That is, each contains the numbers from 1 to n in some order. The first number indicates the candidate of first choice; the second number indicates candidate of second choice, and so on.

    Output
    For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

    The Output consists of either a single line containing the name of the winner or several lines containing the names of the candidates who tied.

    Sample Input
    1

    3
    John Doe
    Jane Smith
    Sirhan Sirhan
    1 2 3
    2 1 3
    2 3 1
    1 2 3
    3 1 2

    Output for Sample Input
    John Doe

    Who can helps me solve this problem. Need some guidance only. thks. this is my homework and I can only use Queue to implement.

  2. #2
    Join Date
    Mar 2006
    Location
    England
    Posts
    10

    Post

    Hopefully, some design, in the form of pseudo-code will help. Not sure what u mean by "I can only use Queue to implement"; you'll almost certainly need the List interface and a class that implements it like ArrayList

    Code:
    let listOfVotes = List of instances of Vote (see outline of Class Vote below)
    let CBC = all candidates  // candidates being considered
    while(true) {  // loop exit condition is within loop
    
      // determine vote count for each contestant in CBC
      clear vote count for each candidate
      foreach vote in listOfVotes {
        let candidate c = vote.getTopCandidate()
        // check validity as it is possible to remove all candidates in a particular vote from consideration
        if c is valid then {
          increment vote count for candidate c
        }
      }  
      
      // check whether any candidate has won outright
      foreach candidate {
        if (num votes for candidate / sizeof listOfVotes) > 50% then {
          display candidate as winner
          break
        }
      }
    
      // consider whether all remaining candidates are tied
      let LNV = lowest number of votes for a candidate in CBC
      let CLNV = list of candidates in CBC with LNV votes
      if size of CLNV = sizeof of CBC then {
        display all candidates in CBC as winners
        break
      }
    
      // remove candidates with lowest votes, and loop again
      remove candidates in CLNV from CBC
      foreach vote in listOfVotes {
        foreach candidate c in CLNV {
          call vote.removeChoice(c)
        }
      }
    }
    
    
    Class Vote with methods:
      void set1stChoice(candidate)
      void set2ndChoice(candidate)
      void set3rdChoice(candidate)
      void removeChoice(candidate)  // is ignored if candidate is not in vote
      candidate getTopCandidate()  // e.g. if 1st choice candidate removed, will return 2nd choice candidate

Similar Threads

  1. Replies: 5
    Last Post: 05-27-2008, 12:17 PM
  2. File system API for project
    By aanchs in forum Java
    Replies: 0
    Last Post: 03-10-2006, 10:08 AM
  3. API For shutting, starting win NT,9x OSes
    By Saiful in forum VB Classic
    Replies: 6
    Last Post: 10-15-2000, 04:18 PM
  4. Replies: 0
    Last Post: 08-01-2000, 08:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center