GithubHelp home page GithubHelp logo

samcan / largest-remainder-calc Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 14 KB

C library implementation of largest remainder method for seat apportionment.

License: MIT License

C 100.00%
voting elections hamilton-method hare-niemeyer-method vinton-s-method

largest-remainder-calc's Introduction

largest-remainder-calc

largest_remainder_calc is a single-file C header library for calculating seat apportionment using the largest remainder method (also known as the Hamilton method, Hare-Niemeyer method, and Vinton's method).

Note that this library only maintains compatibility with C99 and C11. We are not maintaining compatibility with C89.

How to use with example

Download largest_remainder_calc.h into your source code folder.

Include the header file in your source code:

#include "largest_remainder_calc.h"

A fully commented source code example follows:

#include <stdio.h>
#include "largest_remainder_calc.h"

int main() {
  /* Example from https://en.wikipedia.org/wiki/Largest_remainder_method */
  /* We have 10 seats to apportion */
  long num_seats = 10;
  
  /* There were 6 parties to vote for, and of the 100,000 votes
  cast, they received the following number of votes */
  long num_parties = 6;
  long votes[num_parties];
  votes[0] = 47000;
  votes[1] = 16000;
  votes[2] = 15800;
  votes[3] = 12000;
  votes[4] = 6100;
  votes[5] = 3100;
  
  long seats_allocated = 0;
  long results[num_parties];
  seats_allocated = largest_remainder_calc(num_seats, num_parties, votes, HARE_QUOTA, results);
  
  /* Print the results */
  printf("Party\tSeats\n");
  for (int i = 0; i < num_parties; i++) {
    printf("%d\t%4ld\n", i+1, results[i]);
  }
  printf("Total\t%4ld\n", seats_allocated);
  
  return 0;
}

To compile, if the source file is named example.c and we want to compile to program example:

gcc -std=c99 example.c -o example

Running example will provide the below output:

Party	Seats
1	   5
2	   2
3	   1
4	   1
5	   1
6	   0
Total	  10

largest_remainder_calc function definition

long largest_remainder_calc(long n_seats,
                            long n_parties,
                            long votes[],
                            int quota_method,
                            long results[]);

Parameters

  • n_seats is a long specifying the number of seats to be apportioned.
  • n_parties is a long specifying the number of parties voted for. This should be equal to the number of elements in the votes array.
  • votes is a long[] specifying the number of votes for each party. The number of elements in this array should be equal to n_parties.
  • quota_method should be either HARE_QUOTA or DROOP_QUOTA; this determines which method is used for calculating the quota, either Hare's quota or Droop's quota respectively.
  • results is a long[] that is the number of seats apportioned to each party, with the element index being the party's number. You do not need to initialize the array passed in.

Returns

Returns a long with the number of seats allocated. Barring any errors, should be equal to n_seats.

Issues

At this time, the library does not correctly resolve tie situations.

License

largest_remainder_calc is licensed under the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.