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

MAKE A C PROGRAM. Company ballots require that voters rank all the candidates in

ID: 3732789 • Letter: M

Question

MAKE A C PROGRAM.

Company ballots require that voters rank all the candidates in order of choice.

Fixed values (Constants)

Candidate ID                        Candidate Name

John Doe

Jane Smith

Jay Roberts

Input

Up to 1,000 lines, each containing the contents of a ballot. Each ballot contains the numbers from 1 to 3 in some order. The rst number indicates the candidate ID of rst choice; the second number indicates candidate ID of second choice, and the third number indicates candidate ID of third choice. The input will be terminated by a line containing three zeros, which should not be processed. Conflicting inputs are allowed (e.g 3 3 1)

Output

The program will ignore ballots with conflicting inputs (e.g. 1 2 2). The output of each test case consists of the total point earned by each candidate (1 point for First Choice, 2 points for Second Choice, 3 points for Third Choice). Then either a single line containing the name of the winner or several lines containing the names of all candidates who are tied. The output of each two consecutive cases are separated by a blank line.

Sample Input

1 2 3

2 1 3

2 3 1

1 2 3

3 1 2

1 3 2

0 0 0

Sample Output

John Doe= 10pts

Jane Smith= 12pts

Jay Roberts= 14pts

Winner: John Doe

Explanation / Answer

#include<stdio.h>

char cand[3][30] ={"John Doe","Jane Smith","Jay Roberts"}; // CANDIDATE Name
int main() {
int i=0;
char vote[10];
char val[3]=" ";
int pref[3];
int tot[3]={0,0,0};
int rank_arr[1000][3] = {
{1,2,3},
{2,1,3},
{2,3,1},
{1,2,3},
{3,1,2},
{1,3,2},
{0,0,0}
};
int tmp = 0;
int valid=0;
int j;

// Scan each vote
for (j=0; j<1000; j++)
{ //end scan if 000 found
if(rank_arr[j][0] == 0 &&
rank_arr[j][1] == 0 &&
rank_arr[j][2] == 0 )
break;

// discard vote if have same rank
if(rank_arr[j][0] == rank_arr[j][1] ||
rank_arr[j][1] == rank_arr[j][2] ||
rank_arr[j][2] == rank_arr[j][0] )
continue;
tot[0]+=rank_arr[j][0];
tot[1]+=rank_arr[j][1];
tot[2]+=rank_arr[j][2];
valid++;
}
//printf("Number of vote = %d Number of valid vote = %d ",j,valid);
int min=1000;
int winner_indx=0;
for(int i=0;i<3;i++)
{
if(tot[i]<min)
{
min=tot[i];
winner_indx=i;
}
printf("%s = %d pts ", cand[i],tot[i]);

}
printf("Winner %s with %d pts",cand[winner_indx],min);

}

OUTPUT is

John Doe = 10 pts
Jane Smith = 12 pts
Jay Roberts = 14 pts
Winner John Doe with 10 pts