Question
Write a program to do the following: a. Display the table with appropriate labels for the rows and columns. b. Compute and display the total
Write a program to do 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 candidate the winner.
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. Run the program once with the data shown and once with candidate C receiving only 108 votes in Precinct 4.
#include 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};
char cand[4]={'A','B','C','D'};
int row_totals[5]; int col_totals[4]; //This is left for you to do //see code below to see how //row_totals were calculated. int total_votes; //use a for loop to calculate //using either row_totals or col_totals float percent[4]; //use a for loop to calculate based on //total_votes and col_totals //be sure to print the totals for the candidates //at the end, you will need to sort the percent array //bringing along the values from the cand array. That is //if a swap is made in the percent array, do the same //swap in the cand array. //then if percent[3] is greater than 50, declare cand[3] the winner //if percent[3] is not greater than 50, declare a run-off //between cand[2] and cand[3]
int row,col; for(row=0;row<=4;row++) {
row_totals[row]=0; for(col=0;col<=3;col++)
{ row_totals[row] += votes[row][col]; } }
printf(" Candidate Candidate Candidate Candidate Total ");
printf(" Precinct A B C D Votes "); for(row=0;row<=4;row++)
{ printf("%6d",row+1); for(col=0;col<=3;col++)
{ printf("%12d",votes[row][col]); }
printf("%11d ",row_totals[row]); }
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started