Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate’s name, the votes received by that candidate. Your program should also output the winner of the election. A sample output is: Candidate Votes Received % of Total Votes Johnson 5000 25.91 Miller 4000 20.73 Duffy 6000 31.09 Robinson 2500 12.95 Ashtony 1800 9.33 Total 19300 The Winner of the Election is Duffy.

Respuesta :

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.