[EM] simulator sample code
Brian Olson
bql at bolson.org
Thu Dec 14 20:45:47 PST 2006
Someone asked for sample code of what a simple election method looks
like in my simulator. Really I ought to package it up and make it
conveniently downloadable, but until then...
C++
http://bolson.org/voting/sim_one_seat/OneVotePickOne.cpp
and this in plain old C would be easy for me to translate:
void run_election_c( int* winnerArray, float** ratings, int
numVoters, int numChoices ) {
/* for example, implementing pick-one/plurality */
int* counts = (int*)malloc( sizeof(int*) * numChoices );
int i, v;
for ( i = 0; i < numChoices; i++ ) { counts[i] = 0; }
for ( v = 0; v < numVoters; v++ ) {
/* for each voter, find the highest rated choice and vote for it */
int maxi = 0;
float tmax = ratings[v][0];
for ( i = 1; i < numChoices; i++ ) {
if ( ratings[v][i] > tmax ) {
tmax = ratings[v][i];
maxi = i;
}
}
counts[maxi]++;
}
{
int maxi = 0;
int max = counts[0];
for ( i = 1; i < numChoices; i++ ) {
if ( counts[i] > max ) {
max = counts[i];
maxi = i;
}
}
/* winnerArary[0] receives the _index_ of the winner. */
winnerArray[0] = maxi;
/* can optionally sort the indecies of all the choices into the
outbound argument "winnerArray", but really only the winner at
position zero matters */
}
free( counts );
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.electorama.com/pipermail/election-methods-electorama.com/attachments/20061214/6c78c2ce/attachment-0002.htm>
More information about the Election-Methods
mailing list