Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C programming The results from the mayor’s race have been reported by each pr

ID: 3811144 • Letter: I

Question

IN C programming

The results from the mayor’s race have been reported by each precinct as follows:

                        Candicate        Candicate        Candicate        Candicate

Precint                   A                     B                     C                     D

    1                       192                   48                   206                  37

    2                       147                   90                   312                  21

    3                       186                  12                   121                  38

    4                        114                  21                   408                  39

Write a program to do the following:

1.) Use symbolic constants

                  #define PRECINTS 4

                  #define   CANDIDATES 4    

2 .) Display the table with appropriate labels for the rows and columns.

3.) Call a function

void printArray(const int votes[][CANDIDATS], int precints, int cands)

to display the votes of each candidate in a table. Write this function.

4.) Compute and display the total number of votes received by each candidate and the percentage of the total votes cast.

5.) If any one candidate received over 50 percent of the votes, the program should display a message declaring that candidate the winner.

Explanation / Answer

#include <stdio.h>
#define PRECINTS 4
#define CANDIDATES 4
void printArray(const int votes[][CANDIDATES], int precints, int cands)
{
    int voteCount[CANDIDATES]= {0,0,0,0};
    int totalVotes = 0;
    int i,j;
    float per;
   for(i= 0;i<PRECINTS;i++)
   {
        for(j=0;j<CANDIDATES;j++)
        {
            voteCount[j]= voteCount[j] + votes[i][j]; //votes for each candidate
            totalVotes = totalVotes + votes[i][j]; //total votes casted
        }
       
        //printf(" Total Votes : %d ",totalVotes);
   }

   for(i=0;i<CANDIDATES;i++)
   {
        per = (double)voteCount[i]/totalVotes*100;   //calculate percentage
        printf(" Candidate :%d ,Total votes :%d percentage : %.2f",i+1,voteCount[i],per);

        if(per > 50)
        printf(" Candidate %d is the winner ",(i+1));
   }
}
int main(void)
{
   int votes[][CANDIDATES] = {{192,48,206,37},{147,90,312,21},{186,12,121,38},{114,21,408,39}};
   int i,j;
   printf(" Candidate Candidate Candidate Candidate");
   printf(" Precint A B C D ");
   for(i=0;i<PRECINTS;i++)
   {
       printf("%d",i+1);
       for(j=0;j<CANDIDATES;j++)
       {
           printf(" %d ",votes[i][j]); //display votes for each candidate
       }
       printf(" ");
   }

   printArray(votes,PRECINTS,CANDIDATES); //call to function


   return 0;
}

Candidate Candidate Candidate Candidate
Precint A B C D
1 192 48 206 37
2 147 90 312 21
3 186 12 121 38
4 114 21 408 39

Candidate :1 ,Total votes :639 percentage : 32.08
Candidate :2 ,Total votes :171 percentage : 8.58
Candidate :3 ,Total votes :1047 percentage : 52.56
Candidate 3 is the winner
Candidate :4 ,Total votes :135 percentage : 6.78

Do ask if any doubt. Please upvote.