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 #include #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(votes[i]) / static_cast(totalVotes)) * 100 << endl;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!