An election is contested by 5 candidates.Candidates are numbered 1 to 5 %26amp; voting is done by marking the candidate number on the ballot paper.Write a program to read the ballots %26amp; count votes cast for each candidate "using an array variable count".If number read is outside range 1 to 5 then ballot should be considered as "spoiled ballot" %26amp; the program should count that also.
I couldnt get the following c++ program:?
Do something like this:
const int CANDIDATES = 5;
const int EXIT = -1;
int votes[CANDIDATES];
int totalVotes = 0;
int spoiledVotes = 0;
int currentVote = 0;
// Initialize the array with zero's
for(int i = 0; i %26lt; CANDIDATES; i++)
votes[i] = 0;
do
{
cout %26lt;%26lt; "Enter current ballot (1-5 or -1 to exit): ";
cin %26gt;%26gt; currentVote;
if(currentVote %26gt;= 1 %26amp;%26amp; currentVote %26lt;= CANDIDATES)
{
votes[currentVote - 1]++;
totalVotes++;
}
else if(currentVote != EXIT)
{
spoiledVotes++;
}
}while(currentVote != EXIT);
// I hope this helps!!
Reply:You must use the array to store each vote and then you must read each vote and count the amount of votes.
Then you must check if the amount of votes received exceeds how many could have been entered.
What you can do is when they are entering their vote,check whether the array index has exceeded its limit and then increment an integer to store this number.
Make sure you dont allow the user to enter a vote in an index out of range,as this could cause a whole mess of memory problems.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment