Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by Miklak ( 15 years ago )
//Program to read in the votes and compute the winner of the Irish 2011 presidential election 
//author: Kamil Orłowski
//date: 6 November 2011

#include<stdio.h>

main()

{                                                              
    int candidates[7];                                         
    int votes[7] = {0};
    float percentage;
    int i;                                                    
    int count_pref1, count_pref2, count_pref3;    
    int pref1, pref2, pref3;                
    char continue_voting;                                     
    int ballot[7][7][7] = {0};                                
    int lowest = 0;
    int highest = 0;
    int total_valid = 0;                                      
    int total_invalid = 0;  
    char *names[7] = { "Mary Davis", "Sean Gallagher", 
                                "Michael D. Higgins", "Martin McGuiness",
                                "Gay Mitchell", "David Norris",
                                "Dana Rosemary Scallon" };    
    
    do
    {
        printf("Assign preference next to each candidate (1 = primary, " 
               "2 = secondary, 3 = tertiary, 0 = blank/no preference)nn");
        
        for(i=0; i<7; i++)
        {
            printf("%s:",names[i]);
            scanf("%d",&candidates;[i]);
        }//end for
        
        count_pref1 = 0;
        count_pref2 = 0;
        count_pref3 = 0;
                
        for(i=0; i<7; i++)
        {
            switch(candidates[i])
            {
                case 0:
                    break;   
                case 1:
                    count_pref1++;
                    pref1 = i;
                    break;
                case 2:
                    count_pref2++;
                    pref2 = i;
                    break;
                case 3:
                    count_pref3++;
                    pref3 = i;
                    break;
                default:
                    count_pref1 = 2;
                    break;
            }//end switch
        }//end for
       
        if((count_pref1 != 1) || (count_pref2 > 1) || (count_pref3 > 1)
                                 || (count_pref3==1 && count_pref2==0))        
        {
            printf("Invalid vote n");
            total_invalid++;
        }
        else
        {
            if(count_pref2 == 0)
            {
                pref2 = pref1;
                pref3 = pref2;
            }//end inner if
            
            if(count_pref3 == 0)
            {
                pref3 = pref2;
            }//end inner if
            
            ballot[pref1][pref2][pref3]++;
            total_valid++;
            votes[pref1]++;
        }//end outer if
        
        do
        {
            printf("nPress 'y' to enter another person's vote; press 'n'"
                    "to finish voting and count the votes ");
            
            scanf("%1s",&continue;_voting);
            
        }while(continue_voting != 'y' && continue_voting != 'n'); //end inner do-while
        
        printf("nn");
        
    }while(continue_voting == 'y'); 
    //end outer do-while
    
    do
    {   
        for(i=0 ; i<7 ; i++)
        {
            if(votes[i]>=0)
            {
                percentage=(float)100*votes[i]/total_valid;
                printf("%s: %d votes (%.2f %)n",names[i],votes[i],percentage);
                
                if(votes[lowest]==-1 || votes[i]<votes[lowest])
                {
                    lowest = i;
                }//end inner if
                
                if(votes[i] > votes[highest])
                {
                    highest = i;
                }//end inner if
            }//end outer if
        }//end for
        
        percentage = (float)100*votes[highest] / total_valid;
        
        if(percentage > 50)
        {
            printf("nnThe winner is %s",names[highest]);
        }
        else
        {
            
            total_valid -= ballot[lowest][lowest][lowest];
            ballot[lowest][lowest][lowest] = 0;
            votes[lowest] = -1;
            
            for(pref1 = 0 ; pref1 < 7 ; pref1++)
            {
                ballot[pref1][pref1][pref1] += ballot[pref1][lowest][lowest];
                ballot[pref1][lowest][lowest] = 0;
                
                for(pref2 = 0 ; pref2 < 7 ; pref2++)
                { 
                    ballot[pref2][pref2][pref2] += ballot[lowest][pref2][pref2];
                    votes[pref2]+= ballot[lowest][pref2][pref2];
                    ballot[lowest][pref2][pref2] = 0;
                    ballot[pref1][pref2][pref2] += ballot[pref1][pref2][lowest];
                    ballot[pref1][pref2][lowest] = 0;
                    
                    for(pref3 = 0 ; pref3 < 7 ; pref3++)
                    {                    
                        ballot[pref2][pref3][pref3] += ballot[lowest][pref2][pref3];
                        votes[pref2]+= ballot[lowest][pref2][pref3];
                        ballot[lowest][pref2][pref3] = 0;
                        ballot[pref1][pref3][pref3] += ballot[pref1][lowest][pref3];
                        ballot[pref1][lowest][pref3] = 0;
                    }//end 3rd for
                }//end 2nd for
            }//end 1st for
            
            printf("nnEliminated candidate: %snn",names[lowest],i);
        }//end if
        
    }while(percentage <= 50); 
    //end do-while
        
    printf("nInvalid votes: %d",total_invalid);
    
    getchar();
    getchar();
}

 

Revise this Paste

Your Name: Code Language: