An election is contested by five candidates. The candidates are numbered 1 to 5 and a voting is done by marking the candidate number in a ballot paper. Write a C++ program to read the ballot 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 the ballot should be considered as a 'spoilt ballot', and the program should also count the number of spoilt ballots
#include <iostream>
#include <cstdlib> // pseudo-random
#include <ctime> // by time
using namespace std;
int main()
{
srand(time(0)); // for the randomizer
char mode;
int n;
int results[6] = {0,0,0,0,0,0}; // [0] - spoiled, [1]-[5] - candidates
cout<<"Enter the amount of ballots: ";
cin>>n;
int ballotPapers[n];
cout<<"Enter votes manually/auto [M/A]? ";
cin>>mode;
switch(mode){
case 'M': case 'm':
for(int i = 0; i<n; i++){
cout<<"Ballot "<<i+1<<": ";
cin>>ballotPapers[i];
}
case 'A': case 'a':
for(int i = 0; i<n; i++)
ballotPapers[i] = rand()%6;
}
for (int i = 0; i<n; i++){
results[ballotPapers[i]]+=1;
}
cout<<"Spoiled ballots: "<<results[0]<<endl;
for (int i = 1; i<6; i++)
cout<<"Votes for candidate "<<i<<": "<<results[i]<<endl;
return 0;
}
Comments
Leave a comment