Answer to Question #345903 in C++ for Shiiob

Question #345903

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

1
Expert's answer
2022-05-30T08:16:22-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS