Please use detailed comments I want to understand The result
Please use detailed comments I want to understand!
The results from the mayor\'s race have been reported by each precinct as follows:
Write a program that does the following:
Write a program that does the following:
a. Display the table with appropriate labels for the rows and columns.
b. Compute and display the total number of votes received by each candidate and the percentage of the total votes cast.
c. If any one candidate received over 50 percent of the votes, the program should display a message declaring that the candidate the winner.
d. If no candidate received 50 percent of the votes, the program should display a message declaring a runoof between the two candidates receiving the highest number of votes; the two candidates should be identified by their letter names.
e. Run the program once with the data shown and once with candidate C receiving only 108 votes in Precinct 4.
| Precint | Candidate A | Candidate B | Candidate C | Candidate 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 |
Solution
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int candidate_a_votes, candidate_b_votes, candidate_c_votes, candidate_d_votes;
}
prec;
prec precinct[5];
int i;
int candidate_a_total, candidate_b_total, candidate_c_total, candidate_d_total;
int total_vote_cast;
float a_percentage, b_percentage, c_percentage, d_percentage;
int DisplayReport();
int TotalVotes();
float PercentOfTotal();
main()
{
for (i = 0; i < 5; i++)
{
printf(\"Enter precinct %i\'s number of votes for candidate A: \", i);
scanf(\"%d\", &precinct[i].candidate_a_votes);
printf(\"Enter precinct %i\'s number of votes for candidate B: \", i);
scanf(\"%d\", &precinct[i].candidate_b_votes);
printf(\"Enter precinct %i\'s number of votes for candidate C: \", i);
scanf(\"%d\", &precinct[i].candidate_c_votes);
printf(\"Enter precinct %i\'s number of votes for candidate D: \", i);
scanf(\"%d\", &precinct[i].candidate_d_votes);
}
DisplayReport();
TotalVotes();
PercentOfTotal();
printf(\"\ \");
system(\"pause\");
}
int DisplayReport()
{
printf(\"\ MAYORS RACE RESULTS\ \");
printf(\"Precinct No.\\tCandidate A\\tCandidate B\\tCandidate C\\tCandidate D\\t\");
for (i = 0; i < 5; i++)
printf(\"%d\\t\\t%d\\t\\t%d\\t\\t%d\\t\\t%d\\t\\t\ \", i, precinct[i].candidate_a_votes,precinct[i].candidate_b_votes,\\precinct[i].candidate_c_votes, precinct[i].candidate_d_votes);
}
int TotalVotes()
{
candidate_a_total = precinct[0].candidate_a_votes + precinct[1].candidate_a_votes +precinct[2].candidate_a_votes \\+ precinct[3].candidate_a_votes + precinct[4].candidate_a_votes;candidate_b_total = precinct[0].candidate_b_votes + precinct[1].candidate_b_votes +precinct[2].candidate_b_votes \\+ precinct[3].candidate_b_votes + precinct[4].candidate_b_votes;candidate_c_total = precinct[0].candidate_c_votes + precinct[1].candidate_c_votesprecinct[2].candidate_c_votes \\+ precinct[3].candidate_c_votes + precinct[4].candidate_c_votes;candidate_d_total = precinct[0].candidate_d_votes +precinct[1].candidate_d_votes + precinct[2].candidate_d_votes \\+ precinct[3].candidate_d_votes +precinct[4].candidate_d_votes;total_vote_cast = candidate_a_total + candidate_b_total +candidate_c_total + candidate_d_total;
printf(\"\ TOTAL VOTES:\ \");
printf(\"Candidate A has %d total votes\ \", candidate_a_total);
printf(\"Candidate B has %d total votes\ \", candidate_b_total);
printf(\"Candidate C has %d total votes\ \", candidate_c_total);
printf(\"Candidate D has %d total votes\ \", candidate_d_total);
printf(\"Total voters: %d\ \", total_vote_cast);
}
float PercentOfTotal()
{
candidate_a_total = precinct[0].candidate_a_votes + precinct[1].candidate_a_votes +precinct[2].candidate_a_votes \\+ precinct[3].candidate_a_votes + precinct[4].candidate_a_votes;candidate_b_total = precinct[0].candidate_b_votes + precinct[1].candidate_b_votes +precinct[2].candidate_b_votes \\+ precinct[3].candidate_b_votes + precinct[4].candidate_b_votes;candidate_c_total = precinct[0].candidate_c_votes + precinct[1].candidate_c_votes +precinct[2].candidate_c_votes \\+ precinct[3].candidate_c_votes + precinct[4].candidate_c_votes;candidate_d_total = precinct[0].candidate_d_votes + precinct[1].candidate_d_votes +precinct[2].candidate_d_votes \\+ precinct[3].candidate_d_votes + precinct[4].candidate_d_votes;
total_vote_cast = candidate_a_total + candidate_b_total + candidate_c_total + candidate_d_total;
a_percentage = (candidate_a_total / (float) total_vote_cast) * 100;
b_percentage = (candidate_b_total / (float) total_vote_cast) * 100;
c_percentage = (candidate_c_total / (float) total_vote_cast) * 100;
d_percentage = (candidate_d_total / (float) total_vote_cast) * 100;
printf(\"\ TOTAL PERCENT:\ \");
printf(\"Candidate A has %f percent of all the votes.\ \", a_percentage);
printf(\"Candidate B has %f percent of all the votes.\ \", b_percentage);
printf(\"Candidate C has %f percent of all the votes.\ \", c_percentage);
printf(\"Candidate D has %f percent of all the votes.\ \", d_percentage);
printf(\"\ WINNER:\ \");
if (a_percentage >= 50)
printf(\"Candidate A is the winner!!\ \");
else if (b_percentage >= 50)
printf(\"Candidate B is the winner!!\ \");
else if (c_percentage >= 50)
printf(\"Candidate C is the winner!!\ \");
else if (d_percentage >= 50)
printf(\"Candidate D is the winner!!\ \");
two candidates who received the most votes*/
else if (a_percentage < 50 && b_percentage < 50 && c_percentage < 50 && d_percentage < 50) {
if (a_percentage && b_percentage > c_percentage && d_percentage)
printf(\"It would seem candidates A and B are in a run-off here...\ \");
else if (a_percentage && c_percentage > b_percentage && d_percentage)
printf(\"It would seem candidates A and C are in a run-off here...\ \");
else if (a_percentage && d_percentage > b_percentage && c_percentage)
printf(\"It would seem candidates A and D are in a run-off here...\ \");
else if (b_percentage && c_percentage > a_percentage && d_percentage)
printf(\"It would seem candidates B and C are in a run-off here...\ \");
else if (b_percentage && d_percentage > a_percentage && c_percentage)
printf(\"It would seem candidates B and D are in a run-off here...\ \");
else if (c_percentage && d_percentage > a_percentage && b_percentage)
printf(\"It would seem candidates C and D are in a run-off here...\ \");
}
}



