An automated program is required to count the votes received to elect the next union representative for a company. The program must ask how many candidates are registered and the name of each of them, as well as the votes received.
The result of the program will be a matrix that shows the votes received by each candidate, the total of votes received and the percentage that represents that number of votes of the total. In addition, it will present a message with the name of the winning candidate.
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"\nEnter number of candidates: ";
cin>>n;
string names[n];
int votes_received[n];
for(int i=0;i<n;i++){
cout<<"\nEnter details for candidate "<<(i+1)<<endl;
cout<<"\nName: ";
cin>>names[i];
cout<<"\nVotes received: ";
cin>>votes_received[i];
}
double sum=0;
int index;
int winner=votes_received[0];
for(int i=0;i<n;i++){
sum+=votes_received[i];
if (votes_received[i]>winner){
winner=votes_received[i];
index=i;
}
}
cout<<"Name\t\t"<<"Votes received"<<"\t\tPercentage";
for(int i=0;i<n;i++){
cout<<"\n"<<names[i]<<"\t\t"<<votes_received[i]<<"\t\t\t"<<((votes_received[i]/sum)*100)<<"\n";
}
cout<<"\nTotal votes= "<<sum;
cout<<"\nDetails of the winner:\n";
cout<<"\nName: "<<names[index];
cout<<"\nVotes received: "<<votes_received[index];
return 0;
}
Comments
Leave a comment