64 740
Assignments Done
99,3%
Successfully Done
In September 2018

Answer to Question #64042 in C for tina

Question #64042
Write a C program to do the following tasks:

For Example, the results of the elections have been reported for each state as follows:
City
Clinton
Trump
James
Robert
New York
187
52
166
297
California
37
70
212
27
Pennsylvania
20
179
112
45
Florida
244
121
308
19
Texas
251
33
212
35

Write a program to do the following:
• Print the table with appropriate labels for the rows and columns.
• Compute and display the total number of votes received by each candidate and the percentage from the total votes.
• For each city, write the candidate name that has the highest number of votes.
• If any one candidate received over 50% of the votes, the program should display a message declaring that candidate the winner.
• If no candidate received over 50% of the votes, a second round should be done between the top two highest of the candidates. Display a message that mention the names of the top two candidates.
• The data should be read from a text file votes.txt.

Expert's answer
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions