Thursday, September 13, 2018

BALLOTS


  • 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 ballots
  • CODING ARENA
  • #include <stdio.h>
    int main()
      int c[6],m,bn,i;
      for(i=0;i<=5;i++)
      {
        c[i]=0;
      }
      scanf("%d",&bn);
      for(i=0;i<=bn-1;i++)
      {
        scanf("%d",&m);
      if(m==1)
      ++c[0];
      else if(m==2)
      ++c[1];
      else if(m==3)
      ++c[2];
      else if(m==4)
      ++c[3];
      else if(m==5)
      ++c[4];
      else
      ++c[5];
     }
      printf("Candidate 1=%d",c[0]);
      printf("\nCandidate 2=%d",c[1]);
      printf("\nCandidate 3=%d",c[2]);
      printf("\nCandidate 4=%d",c[3]);
      printf("\nCandidate 5=%d",c[4]);
      printf("\nSpoil Ballot=%d",c[5]);
      
      return 0;
    }
  • Test Case 1

    Input (stdin)
    15
    
    1 2 3 4 5 6 7 1 1 1 1 1 1 1 1
    
    
    Expected Output
    Candidate 1=9
    
    Candidate 2=1
    
    Candidate 3=1
    
    Candidate 4=1
    
    Candidate 5=1
    
    Spoil Ballot=2
  • 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 6 7 8 9
    
    
    Expected Output
    Candidate 1=5
    
    Candidate 2=5
    
    Candidate 3=4
    
    Candidate 4=3
    
    Candidate 5=2
    
    Spoil Ballot=4

No comments:

Post a Comment