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 );
}
----
election-methods mailing list - see http://electorama.com/em for list info

Reply via email to