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

modify this code (C++) with using dynamic arrays. it must ask the user for the n

ID: 3708446 • Letter: M

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 hold it in

#include <iostream>
#include <iomanip>
#include <string>

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<double>(votes[i]) / static_cast<double>(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;
}

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()
{
   int size, i,totalVotes;;
   string* candidates = NULL;
   int* votes = NULL;
   cout<<"Enter number of candidates you want to annroll: ";
   cin>>size;
   candidates = new string[size];
   votes = new int[size];

   cout << "Enter candidate's name and the votes received by "
   << "the candidate." << endl;

   for (i = 0; i < size; i++)
       cin >> candidates[i] >> votes[i];

   totalVotes = sumVotes(votes, size);

   cout << "Candidate    Votes Received   % of Total Votes" << endl;
   for (i = 0; i < size; i++)
       cout << left << setw(10) << candidates[i]
   << right << " " << setw(10) << votes[i] << "   " << setw(15)
   << (static_cast<double>(votes[i]) / static_cast<double>(totalVotes)) * 100
   << endl;

   cout << "Total            " << totalVotes << endl;

   cout << "The Winner of the Election is "
   << candidates[winnerIndex(votes, size)]
   << "." << 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;
   }