Problem Description
An election is contested by 5 candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a program to read the ballots and count the votes cast for each candidate using an array variable count. In case a number read is outside the range 1 to 5 ballot should be considered as a spoilt ballot and the program should also count the number of spoilt ballotsCODING ARENA::
#include<stdio.h>
int main()
{
int count[6],mark,bn,i;
for(i=0;i<=5;i++)
{
count[i]=0;
}
scanf("%d",&bn);
for(i=0;i<=bn-1;i++)
{
scanf("%d",&mark);
if(mark==1)
{
++count[0];
}
else if(mark==2)
{
++count[1];
}
else if(mark==3)
{
++count[2];
}
else if(mark==4)
{
++count[3];
}
else if(mark==5)
{
++count[4];
}
else
{
++count[5];
}
}
printf("Candidate 1=%d\n",count[0]);
printf("Candidate 2=%d\n",count[1]);
printf("Candidate 3=%d\n",count[2]);
printf("Candidate 4=%d\n",count[3]);
printf("Candidate 5=%d\n",count[4]);
printf("Spoil Ballot=%d",count[5]);
return 0;
}
Test Case 1
Input (stdin)6 6 7 8 9 8 8
Expected OutputCandidate 1=0 Candidate 2=0 Candidate 3=0 Candidate 4=0 Candidate 5=0 Spoil Ballot=6
Test Case 2
Input (stdin)23 1 1 1 1 1 2 2 2 2 2 3 3 3 3 4 4 4 5 5 7 9 7 8
Expected OutputCandidate 1=5 Candidate 2=5 Candidate 3=4 Candidate 4=3 Candidate 5=2 Spoil Ballot=4
No comments:
Post a Comment