Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. the program should then output each candidate’s name, votes received by that candidate, and the percentage of the total votes received by the candidate. the program should also output the winner of the election.

Respuesta :

Answer:

Here is code in c++.

#include<bits/stdc++.h>

using namespace std;

int main()

{

    int n;

    cout<<"Enter the number of candidates:";

    cin>>n;

      // string array to store name of candidates

       string name[n];

       // int array to store vote count of candidates

       int vote_c[n];

       double percent = 0;

       int i = 0, sum = 0, highest =0, Winner = 0;

       //String name;

       for(i = 0; i < n;i++)

       {

          cout<<"last name of Candidate "<<(i+1) <<" :";

          // read name of Candidate

          cin>>name[i];

          cout<<" number of votes received: ";

          // read vote of Candidate

          cin>> vote_c[i];

              if(vote_c[i] > highest)

              {

                  // highest vote

                  highest = vote_c[i];

                  Winner = i;

              }

              // total vote count

           sum +=vote_c[i];

       }

     // printing the output

      cout<<"\nCandidate\tVotes Received\t of TotalVotes\n"<<endl;

       for(i = 0; i < n;i++)

       {

           // % of vote of each Candidate

       percent =(vote_c[i]*100)/double(sum);

           // print the output

          cout<<name[i] <<"\t\t" <<vote_c[i] << "\t\t\t" <<percent<<endl;

       }

       // print the total votes

      cout<<"\nTotal Votes:\t"<< sum<<endl;

      // print the Winner of the Election

       cout<<"Winner of the Election is: " << name[Winner]<<endl;

return 0;

   }

Explanation:

Create a string array "name" to store the name of candidates.Create Integer array "vote_c" to store votes os each candidates.Calculate total votes of all candidates and find the highest vote amongst all candidate and print it.Find % of votes received by each candidate.Print these stats.

Output:

Enter the number of candidates:4                                                                                          

last name of Candidate 1 :sam                                                                                              

number of votes received: 32                                                                                              

last name of Candidate 2 :alex                                                                                            

number of votes received: 43                                                                                              

last name of Candidate 3 :kris                                                                                            

number of votes received: 55                                                                                              

last name of Candidate 4 :liji                                                                                            

number of votes received: 76                                                                                              

                                                                                                                         

Candidate       Votes Received   of TotalVotes                                                                            

                                                                                                                         

sam             32                      15.534                                                                            

alex            43                      20.8738                                                                            

kris            55                      26.699                                                                            

liji            76                      36.8932                                                                            

                                                                                                                         

Total Votes:    206                                                                                                        

Winner of the Election is: liji

ACCESS MORE