Answer:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string names[5];
int votes[5];
for(int i=0;i<5;i++)
{
cout<<"Enter candidate name"<<endl;
getline(cin,names[i]);
cout<<"Enter candidate votes"<<endl;
cin >> votes[i];
cin.ignore();
}
int total_votes=0;
int max =-1;
int max_val=0;
for(int i=0;i<5;i++)
{
total_votes=total_votes+votes[i];
if(max_val<votes[i])
{
max_val=votes[i];
max=i;
}
}
cout<<"Total votes"<<total_votes<<endl;
for(int i=0;i<5;i++)
{
float per=(votes[i]/total_votes)*100;
cout<<"float per"<<per<<endl;
cout<<" "<<names[i]<<" "<<votes[i]<<" "<<per<<" %" <<endl;
}
cout<<"Winner is "<<names[max]<<" "<<votes[max]<<endl;
return 0;
}
Explanation:
Define a string array of size 5 for names. Define one integer array of size 5 for votes. Take input from user in loop for string array and int for votes.
In another loop find maximum of the list and sum all the votes. Store max votes index in max variable. In another loop display names along with their votes and percentage.
Display winner name and votes using max as index for name and votes array.