Make modification to the following C code to so that SELECTI
Make modification to the following C++ code to so that SELECTION sorting TO DISPLAY IN DESCENDING ORDER AS SHOWEN IN THE OUTPUT BELLOW:
Please input candidate 1 name:
a
Please input candidate 1 votes:
1
Please input candidate 2 name:
b
Please input candidate 2 votes:
2
Please input candidate 3 name:
c
Please input candidate 3 votes:
3
Please input candidate 4 name:
d
Please input candidate 4 votes:
4
Please input candidate 5 name:
e
Please input candidate 5 votes:
5
Candidate Votes Received
e 5
d 4
c 3
b 2
a 1
The winner of this election is e
Program eded with exit code: 0
************** COPY AND PASTE THIS CODE AND MAKE MODIFICATION ***********
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
string *name;
double *vote;
double *percent;
double total=0;
int max=0;
name=new string[5];
vote=new double[5];
percent=new double[5];
for(int i=0; i<5;i++){
cout<<\"Please input candidate \"<<i+1<<\" name: \"<<endl;
cin>>name[i];
cout<<\"Please input candidate \"<<i+1<<\" votes: \"<<endl;
cin>>vote[i];
total+=vote[i];
}
cout<<\"Candidate \"<<\"Votes Received \"<<\"%\"<<endl;
for(int i=0; i<5; i++){
percent[i]=vote[i]/total;
cout << name[i] << \" \" << fixed << setprecision(0) << vote[i] << \" \" << fixed << setprecision(2) << percent[i] * 100 << endl;
if(percent[max]<percent[i]){
max=i;
}
}
cout<<\"The winner of the election is \"<<name[max]<<endl;
delete []name;
delete []vote;
delete []percent;
return 0;
}
Solution
// C++ code
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <iomanip> // std::setprecision
#include <math.h>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
void selectionSort(string *name, double *vote, int size)
{
int i, j, minimum_index;
double t;
string tempn;
// One by one move boundary of unsorted subarray
for (i = 0; i < size-1; i++)
{
// Find the minimum element in unsorted array
minimum_index = i;
for (j = i+1; j < size; j++)
if (*(vote+j) < *(vote+minimum_index))
minimum_index = j;
// Swap the found minimum element with the first element
tempn = *(name+j);
t = *(vote+j);
*(name+j) = *(name+minimum_index);
*(vote+j) = *(vote+minimum_index);
*(name+minimum_index) = tempn;
*(vote+minimum_index) = t;
}
}
int main()
{
int size = 5;
string *name;
double *vote;
double *percent;
double total=0;
int max = 0;
name=new string[size];
vote=new double[size];
percent=new double[size];
for(int i=0; i<size;i++)
{
cout<<\"Please input candidate \" << (i+1) << \" name: \";
cin>>name[i];
cout<<\"Please input candidate \" << (i+1) << \" votes: \";
cin>>vote[i];
total += vote[i];
}
selectionSort(name,vote,size);
cout<<\"\ \ Candidate\\tVotes Received\\tVote percentage\ \";
int i;
for(i=0; i<size; i++)
{
percent[i]=vote[i]/total;
cout << name[i] << \"\\t\\t\" << fixed << setprecision(0) << vote[i] << \"\\t\\t\" << fixed << setprecision(2) << percent[i] * 100 << endl;
if(vote[i] > vote[max])
max=i;
}
cout<<\"\ The winner of the election is \" << name[max] << \" with \" << vote[max] << \" votes and a vote percentage of \" << percent[max]*100 << \"%\" << endl << endl;
delete []name;
delete []vote;
delete []percent;
return 0;
}
/*
output:
Please input candidate 1 name: ayush
Please input candidate 1 votes: 23
Please input candidate 2 name: eoin
Please input candidate 2 votes: 56
Please input candidate 3 name: morgan
Please input candidate 3 votes: 23
Please input candidate 4 name: mike
Please input candidate 4 votes: 46
Please input candidate 5 name: jason
Please input candidate 5 votes: 23
Candidate Votes Received Vote percentage
eoin 56 32.75
mike 46 26.90
ayush 23 13.45
morgan 23 13.45
jason 23 13.45
The winner of the election is eoin with 56.00 votes and a vote percentage of 32.75%
*/



