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.


Implementation of the above problem in C++ language:
#include<iostream.h>
int main()
{
int i,can[5],ballot,count[5];
char ch;
for(i=0;i<=5;i++)
{
count[i]=0;
}
do
{
cout<<"\nEnter the ballot number :";
cin>>ballot;
switch(ballot)
{
case 1: count[1]++;
break;
case 2: count[2]++;
break;
case 3: count[3]++;
break;
case 4: count[4]++;
break;
case 5: count[5]++;
break;
default: count[0]++;
}
cout<<" \nWant to vote again\n";
cin>> ch;
}
while(ch=='y');
for(i=1;i<=5;i++)
{
cout<<"\nNo:of votes for candidate "<< i <<"="<<count[i];

}
cout<<"\n Sploit ballots are "<<count[0];
return 0;
}


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

1 comments:

  1. there is one error in this program which gives strange output i have solved this error using your program only. i had not add any more variables or functions in this code but i had modified it

    code is:

    #include
    using namespace std;
    int main()
    {
    int i,can,ballot,count[5];
    char ch;
    for(i=0;i<=5;i++)
    {
    count[i]=0;
    }
    do
    {
    cout<<"\nEnter the ballot number :";
    cin>>ballot;
    can = 0;
    switch(ballot)
    {
    case 1: count[0]++;
    break;
    case 2: count[1]++;
    break;
    case 3: count[2]++;
    break;
    case 4: count[3]++;
    break;
    case 5: count[4]++;
    break;
    default: can++;
    }
    cout<<" \nWant to vote again\n";
    cin>> ch;
    }
    while(ch=='y');
    for(i=0;i<5;i++)
    {
    cout<<"\nNo:of votes for candidate "<< i+1 <<"="<<count[i];

    }
    cout<<"\n Sploit ballots are "<<can;
    return 0;
    }

    ReplyDelete