#include <stdio.h>
int main()
{
    FILE * f;
    f = fopen("votes.txt","r");
    char city[6];
    fgets(city,6,f);
    free(city);
    char** names;
    char ** cities;
    int ** votes;
    names = (char**)calloc(4 , sizeof(char*));
    votes = (int**)calloc(5 , sizeof(int*));
    cities= (char**)calloc(5 , sizeof(char*));
    int i,j;
    for(i=0;i<4;i++)
    {
        names[i] = (char*)calloc(12 , sizeof(char));
        fgets(names[i],12,f);
    }
    for(i = 0;i < 5; i++)
    {
        cities[i] = (char*)calloc(15 , sizeof(char));
        votes[i] = (int*)calloc(4 , sizeof(int));
        fgets(cities[i],15,f);
        for(j = 0; j < 4; j++)
        {
            fscanf(f,"%d\n",&votes[i][j]);
        }
    }
    close(f);
    printf("\t\t");
    for(i=0; i < 4; i++)
    {
        j = 0;
        while(names[i][j]!='\n')
        {
            printf("%c",names[i][j]);
            j++;
        }
        printf("\t");
    }
    printf("\n");
    for(i=0; i < 5; i++)
    {
        j = 0;
        while(cities[i][j]!='\n')
        {
            printf("%c",cities[i][j]);
            j++;
        }
        printf("\t");
        if(i>2)
            printf("\t");
        for(j = 0; j < 4; j++)
        {
            printf("%d\t", votes[i][j]);
        }
        printf("\n");
    }
    int sm = 0,gsm = 0;
    for(i=0;i<4;i++)
    {
        for(j=0;j<5;j++)
        {
            gsm+=votes[j][i];
        }
    }
    printf("Sum : \t\t");
    for(i=0;i<4;i++)
    {
        sm = 0;
        for(j=0;j<5;j++)
        {
            sm+=votes[j][i];
        }
        printf("%d\t",sm);
    }
    printf("\n");
    printf("Sum (percents): ");
    float mx1 = 0, mx2 = 0;
    char *name = (char*) calloc(12 , sizeof(char));
    char *name1 = (char*) calloc(12 , sizeof(char));
    char *name2 = (char*) calloc(12 , sizeof(char));
    for(i=0;i<4;i++)
    {
        sm = 0;
        for(j=0;j<5;j++)
        {
            sm+=votes[j][i];
        }
        if((float)((1.0*sm)/gsm) > mx1)
        {
            mx2 = mx1;
            name2 = name1;
            mx1 = (float)((1.0*sm)/gsm);
            name1 = names[i];
        }
        else if((float)((1.0*sm)/gsm) > mx2)
        {
            mx2 = (float)((1.0*sm)/gsm);
            name2 = names[i];
        }
        printf("%d%%\t",(int)((float)((1.0*sm)/gsm)*100));
    }
    printf("\n\n");
    for(i=0;i<5;i++)
    {
        int mx = -1;
        for(j=0;j<4;j++)
        {
            if(votes[i][j] > mx)
            {
                mx = votes[i][j];
                name = names[j];
            }
        }
        j = 0;
        while(cities[i][j]!='\n')
        {
            printf("%c",cities[i][j]);
            j++;
        }
        j = 0;
        printf(":  \t");
        while(name[j]!='\n')
        {
            printf("%c",name[j]);
            j++;
        }
        printf("\n");
    }
    printf("\n");
    if(mx1>0.5)
    {
        printf("Winner is \t");
        j = 0;
        while(name1[j]!='\n')
        {
            printf("%c",name1[j]);
            j++;
        }
    }
    else
    {
        printf("To next round: \t");
        j = 0;
        while(name1[j]!='\n')
        {
            printf("%c",name1[j]);
            j++;
        }
        printf("\tand\t");
        j = 0;
        while(name2[j]!='\n')
        {
            printf("%c",name2[j]);
            j++;
        }
    }
    printf("\n");
    free(name1);
    free(name2);
    free(name);
    for(i = 0;i < 5; i++)
    {
        free(cities[i]);
        free(votes[i]);
    }
    for(i=0;i<4;i++)
    {
        free(names[i]);
    }
    free(cities);
    free(votes);
    free(names);
    return 0;
}                            
                                                 
                
Comments