Make modification to the following C++ code to so that insertion sorting TO DISP
ID: 3941445 • Letter: M
Question
Make modification to the following C++ code to so that insertion sorting TO DISPLAY THE RESULTS IN DESCENDING ORDER AS SHOWN BELOW:
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
#include
#include
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 "<
cin>>name[i];
cout<<"Please input candidate "<
cin>>vote[i];
total+=vote[i];
}
cout<<"Candidate "<<"Votes Received "<<"%"<
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]
max=i;
}
}
cout<<"The winner of the election is "<
delete []name;
delete []vote;
delete []percent;
return 0;
}
Explanation / Answer
// 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;
// sort the votes
void insertionSort(string *name, double *vote, int size)
{
int i,j,t;
string tempn;
for(i=1;i<size;i++)
{
tempn = *(name+i);
t = *(vote+i);
j=i-1;
while(t > *(vote+j) && j>=0)
{
*(name+(j+1)) = *(name+j);
*(vote+(j+1)) = *(vote+j);
j--;
}
*(name+(j+1)) = tempn;
*(vote+(j+1)) = 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];
}
insertionSort(name,vote,size);
cout<<" Candidate Votes Received Vote percentage ";
int i;
for(i=0; i<size; i++)
{
percent[i]=vote[i]/total;
cout << name[i] << " " << fixed << setprecision(0) << vote[i] << " " << 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%
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.