In: Computer Science
this is data structures with C++ language, please do "#1 & a" separately and please send me copyable file
1. 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. Assume a user enters a candidate's name only once and assume that no two candidates receive the same number of votes. Your program should output the winner of the election.
a. Modify your work, above question solution as follows:
replace the int array with a double vector
the winner gets at most 20% of the loser's assets
the loser withdraws from the game if its assets are less than 1.00. In this case the winner gets all of the loser's assets.
#include <iostream>
#include <string>
using namespace std;
int findWinner(int votes[]);
void printResults(string candidates[], int votes[]);
double calculatePercentage(int votes[], int vote);
const int NUMBER_OF_CANDIDATES = 5;
int main()
{
string candidates[NUMBER_OF_CANDIDATES];
int votes[NUMBER_OF_CANDIDATES];
cout << "Please input the 5 candidates followed by the votes
they recieved i.e. Smith 5000: ";
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
cin >> candidates[i] >> votes[i];
}
printResults(candidates, votes);
cout << "And the winner is: " <<
candidates[findWinner(votes)] << endl;
return 0;
}
double calculatePercentage(int votes[], int vote){
int sumOfVotes = 0;
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
sumOfVotes += votes[i];
}
double percentage = static_cast<double>(vote) /
sumOfVotes;
return percentage*100;
}
void printResults(string candidates[], int votes[]){
cout << "Name:" << setw(15) << "Votes:" <<
setw(15) << "Percentage:" << endl;
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
cout << candidates[i] << setw(15) << votes[i]
<< setw(15);
int percentage = calculatePercentage(votes, votes[i]);
cout << percentage << "%" << endl;
}
}
int findWinner(int votes[]){
int winner = 0;
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
if (votes[i] > winner)
winner = i;
}
return winner;
}