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
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
ReplyDeletecode 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;
}
Respect and that i have a nifty offer: House Renovation What To Do First residential renovation contractors near me
ReplyDelete