Question: modify this code (C++) with using dynamic arrays. it must ask the user for the number of candiates and then create the apporpirte arrays to
modify this code (C++) with using dynamic arrays. it must ask the user for the number of candiates and then create the apporpirte arrays to hold it in
#include
using namespace std;
int sumVotes(int list[], int size); int winnerIndex(int list[], int size);
int main() { string candidates[5]; int votes[5] = { 0 }; int totalVotes; int i;
cout << fixed << showpoint; cout << setprecision(2); cout << "Enter candidate's name and the votes received by " << "the candidate." << endl;
for (i = 0; i < 5; i++) cin >> candidates[i] >> votes[i];
totalVotes = sumVotes(votes, 5);
cout << "Candidate Votes Received % of Total Votes" << endl; for (i = 0; i < 5; i++) cout << left << setw(10) << candidates[i] << right << " " << setw(10) << votes[i] << " " << setw(15) << (static_cast
cout << "Total " << totalVotes << endl;
cout << "The Winner of the Election is " << candidates[winnerIndex(votes, 5)] << "." << endl;
return 0; }
int sumVotes(int list[], int size) { int sum = 0;
for (int i = 0; i < size; i++) sum = sum + list[i];
return sum; }
int winnerIndex(int list[], int size) { int winInd = 0;
for (int i = 0; i < size; i++) if (list[i] > list[winInd]) winInd = i;
return winInd; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
