Mayor’s Race The results from the mayor's race have been reported by each precinct as follows:
Candidate Candidate Candidate Candidate Precinct A B C D 1 192 48 206 37 2 147 90 312 21 3 186 12 121 38 4 114 21 408 39 5 267 13 382 29
Write a program to do the following:
a. Read the raw vote totals from a data file that contains one row for each precinct.
b. Display the table with appropriate headings for the rows and columns.
c. Compute and display the total number of votes received by each candidate and the percent of the total votes cast.
d. If any one candidate received over 50% of the votes, the program should print a message declaring that candidate the winner.
e. If no candidate received 50% of the votes, the program should print a message declaring a run-off between the two candidates receiving the highest number of votes; the two candidates should be identified by their letter names.
f. For testing, run the program with the above data, and also with another data file where Candidate C receives only 108 votes in precinct 4.
Answers
Answer:
#include<stdio.h>
int main()
{
int a[5],b[5],c[5],d[5];
int i,j,k,l;
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
scanf("%d",&b[i]);
for(i=0;i<5;i++)
scanf("%d",&c[i]);
for(i=0;i<5;i++)
scanf("%d",&d[i]);
printf("\n\n");
printf(" \tCandidate\tCandidate\tCandidate\tCandidate\n");
printf("Precinct\tA\tB\t\tC\t\tD\n");
printf("1\t\t%d\t%d\t\t%d\t\t%d\n",a[0],b[0],c[0],d[0]);
printf("2\t\t%d\t%d\t\t%d\t\t%d\n",a[1],b[1],c[1],d[1]);
printf("3\t\t%d\t%d\t\t%d\t\t%d\n",a[2],b[2],c[2],d[2]);
printf("4\t\t%d\t%d\t\t%d\t\t%d\n",a[3],b[3],c[3],d[3]);
printf("5\t\t%d\t%d\t\t%d\t\t%d\n",a[4],b[4],c[4],d[4]);
int as=0,bs=0,cs=0,ds=0;
for(i=0;i<5;i++)
{
as=as+a[i];
bs=bs+b[i];
cs=cs+c[i];
ds=ds+d[i];
}
int tot=as+bs+cs+ds;
printf("total votes of a is %d\ntotal votes of b is %d\ntotal votes of c is %d\ntotal votes of d is %d\n\n\n ",as,bs,cs,ds);
printf("total votes are %d\n\n\n",tot);
int pa,pb,pc,pd;
pa=(as*100)/tot;
pb=(bs*100)/tot;
pc=(cs*100)/tot;
pd=(ds*100)/tot;
printf("percent of a is %d\n",pa);
printf("percent of b is %d\n",pb);
printf("percent of c is %d\n",pc);
printf("percent of d is %d\n\n",pd);
if(pa>50)
printf("a is winner\n");
if(pb>50)
printf("b is winner\n");
if(pc>50)
printf("c is winner\n");
if(pd>50)
printf("d is winner\n");
}
Explanation:
Answer:
#include<stdio.h>
void RunAllParts(int votes[5][4])
{
int i=0,j=0;
printf(" \t\tCandidate\tCandidate\tCandidate\tCandidate\n");
printf("Precinct\t A\t\t B\t\t C\t\t D\n");
// Part A Display the table with appropriate labels for the rows and columns.
for (i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(j==0)
printf("%d\t\t", i+1);
printf("%d\t\t",votes[i][j]);
}
printf("\n");
}
//Part B Compute and display the total number of votes received by each candidate and the percentage of the total votes cast.
printf("\n\n************************************************************************\n");
int sum[6]={0}, cumalativeSum=0;
char candidateName[] = "ABCD";
for (i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
sum[i] += votes[j][i];
cumalativeSum += votes[j][i];
}
}
j=0;
for (i=0; i<4; i++)
{
printf("\nTotal votes of Candidate %c is %d and Percentage is %.2f ", candidateName[j++], sum[i], (float)sum[i]/cumalativeSum);
}
int maxValueIndices[2]= {
4, 5
};
/* Part C If any one candidate received over 50 percent of the votes[, the program should display a
message declaring that candidate the winner.*/
printf("\n\n************************************************************************\n");
int winnerFound = 0;
float per = 0.0;
// printf("\ncummalative sum %d\n", cumalativeSum);
for (i=0; i<4; i++)
{
per = (float)sum[i]/cumalativeSum;
if( per > 0.5 )
{
printf("\nCandidate %c is a winner having percentage %.2f", candidateName[i], per);
winnerFound = 1;
}
// logic to find 2 largest element indexes in an array.
if(sum[i]>sum[maxValueIndices[0]]){
maxValueIndices[1]= maxValueIndices[0];
maxValueIndices[0] = i;
}
else if(sum[i] > sum[maxValueIndices[1]] && sum[i] != sum[maxValueIndices[0]])
{
maxValueIndices[1] = i;
}
}
/*Part D If no candidate received 50 percent of the votes[, the program should display a message declaring
a runoff between the two candidates receiving the highest number of votes[; the two candidates
should be identified by their letter names.(e.g. candidates ‘A’ and ‘C’ received highest votes[)*/
if(winnerFound == 0){
printf("\nCandidate %c got the Highest votes %d", candidateName[maxValueIndices[0]], sum[maxValueIndices[0]]);
printf("\nCandidate %c got the Second Highest votes %d", candidateName[maxValueIndices[1]], sum[maxValueIndices[1]]);
}
}
int main()
{
int votes[5][4]= {
{192,48,206,37},
{147,90,312,21},
{186,12,121,38},
{114,21,408,39},
{267,13,382,29}
};
RunAllParts(votes);
/* Part E Run the program once with the data shown and once with candidate C receiving only 108 votes in
Precinct */
printf("\n\n************************************************************************\n");
printf("\nRunning Code with candidate C receiving only 108 votes\n\n");
int Newvotes[5][4]= {
{192,48,10,37},
{147,90,20,21},
{186,12,30,38},
{114,21,40,39},
{267,13,8,29}
};
RunAllParts(Newvotes);
}
Explanation: