The Winner of the Election is Duffy. *******************************************
ID: 3708960 • Letter: T
Question
The Winner of the Election is Duffy.
************************************************
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int sumVotes(int list[], int size);
int winnerIndex(int list[], int size);
int main()
{
// declare array varaibles
int totalVotes;
int i;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Enter candidate's name and the votes received by "
<<"the candidate." << endl;
// load data into arrays
// set totalVotes
cout << "Candidate Votes Received % of Total Votes" << endl;
// output data
cout << "Total " << totalVotes << endl;
// output winner
return 0;
}
int sumVotes(int list[], int size)
{
// function code
}
int winnerIndex(int list[], int size)
{
// function code
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int sumVotes(int list[], int size);
int winnerIndex(int list[], int size);
int main()
{
// declare array varaibles
int totalVotes;
int i;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Enter candidate's name and the votes received by "
<<"the candidate." << endl;
// load data into arrays
string names[5];
int votes[5];
int size = 5;
for(i = 0; i < size; ++i) {
cout << "Name: ";
cin >> names[i];
cout << "Votes: ";
cin >> votes[i];
}
// set totalVotes
totalVotes = sumVotes(votes, size);
int winner = winnerIndex(votes, size);
cout << "Candidate Votes Received % of Total Votes" << endl;
for(i = 0; i < size; ++i) {
cout << names[i] << " " << votes[i] << " " << (votes[i]*100.0)/(totalVotes) << endl;
}
cout << "Total " << totalVotes << endl;
cout << "The Winner of the Election is " << names[winner] << "." << endl;
// output winner
return 0;
}
int sumVotes(int list[], int size)
{
// function code
int total = 0;
for(int i = 0; i < size; ++i) {
total += list[i];
}
return total;
}
int winnerIndex(int list[], int size)
{
// function code
int ind = 0;
for(int i = 0; i < size; ++i) {
if(list[i] > list[ind]) {
ind = i;
}
}
return ind;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.