Question: write this code using dynamic arrays instead of static arrays #include #include #include using namespace std; const int NUM _ CANDIDATES = 5 ; void

write this code using dynamic arrays instead of static arrays #include
#include
#include
using namespace std;
const int NUM_CANDIDATES =5;
void readCandidateData(string[],int[]);
int sumVotes(int[]);
int determineWinner(string[],int[]);
void printResults(string[],int[],double[]);
int main()
{
string lastNames[NUM_CANDIDATES];
int votes[NUM_CANDIDATES];
double percentages[NUM_CANDIDATES];
readCandidateData (lastNames , votes);
int totalVotes = sumVotes(votes);
for (int i =0; i < NUM_CANDIDATES; ++i)
{
percentages[i]=(static_cast (votes[i])/ totalVotes)*100;
}
printResults ( lastNames , votes , percentages);
int winnerIndex = determineWinner ( lastNames , votes);
cout << "The winner of the election is "<< lastNames[winnerIndex]<<"."<< endl;
return 0;
}
void readCandidateData(string lastNames[], int votes[])
{
for( int i =0 ; i < NUM_CANDIDATES; ++i )
{
cout << "Candidate "<<(i +1)<<" last name: " ;
cin >> lastNames[i] ;
cout << "Votes recieved: "<< lastNames[i]<<": " ;
cin >> votes[i] ;
}
}
int sumVotes( int votes[])
{
int total =0 ;
for ( int i =0 ; i < NUM_CANDIDATES ; ++i)
{
total += votes[i] ;
}
return total ;
}
int determineWinner ( string lastNames[], int votes[])
{
int maxIndex =0 ;
for ( int i =1 ; i < NUM_CANDIDATES ; ++i)
{
if ( votes[i]> votes[maxIndex])
{
maxIndex = i ;
}
}
return maxIndex ;
}
void printResults ( string lastNames[], int votes[], double percentages[])
{
cout << setw(15)<< left << "Candidate" << setw(10)<< right << "Votes" << setw(15)<< "Percentage" << endl;
cout << setfill('-')<< setw(40)<<""<< setfill('')<< endl;
cout << fixed << setprecision(2);
for (int i =0 ; i < NUM_CANDIDATES ; ++i)
{
cout << setw(15)<< left << lastNames[i]<< setw(10)<< right << votes[i]<< setw(15)<< percentages[i]<<"%"<< endl;
}
}

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 Finance Questions!