Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that allows the user to enter the last names of five candidates

ID: 3693271 • Letter: W

Question

Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the 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

Design with functions and use arrays.

I have the code but I'm just having trouble with making them functions and doing it instead. I have the code functioning property without functions. If anyone could give me a code that has functions.

Explanation / Answer

#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 enter the 5 candidates followed by the votes they recieved : ";
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;
}


Alternate way:


#include <iostream>
#include<Iomanip>
#include<string>
using namespace std;
int main()
{
int votes[50];
string candidate[50];
double percent[50];
int sum;
int counter;
int index;
int largestscale;
int x;
int i;
cout << "Enter the Candidate's name and then enter the votes" << endl;
counter = 0;
for (counter = 0; counter < 5; counter++)
{
cin >> candidate[counter];
cin >> votes[counter];
}
sum = 0;
for (i = 0; i < 5; i++)
{
sum = sum + votes[i];
}
cout << "Total number of votes is" << sum << endl;
for (x = 0; x < 5; x++)
{
percent[x] = votes[x] * 100.0 / sum;
}
int maxIndex = 0;
for (index = 0; index < 5; index++)
{
if (votes[maxIndex] < votes[index])
maxIndex = index;
largestscale = votes[maxIndex];
}
cout << "Candidate" << " " << "# of votes" << "" << "Percent of vote" << endl;
for (counter =0; counter < 5; counter++)
{
cout << candidate[counter] << votes[counter] << percent[counter] << endl;
}
cout <<"The winner is" << candidate[largestscale] << endl;
cin.ignore().get();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote