In: Computer Science
IN C ++ PLEASE CODE FOR BUBBLE SORT---Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info .
You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one.
Sort by Average across, lowest to highest. The highest average should then be on the last row..
When you sort the average, you also have to move the names and scores, this is the reason you can't use a built-in sort.
Names score1 score2 score3 average score
---------------------------------------------------------------------------
Linus too good 100 23 210 111.00
Charlie brown 1 2 12 5.00
Snoopy 300 300 100 233.33
Peperment Patty 223 300 221 248.00
Pig Pen 234 123 212 189.67
Red Headed Girl 123 222 111 152.00
Marcey 1 2 3 2.00
keith hallmark 222 300 180 234.00
anna hallmark 222 111 211 181.33
roxie hallmark 100 100 2 67.33
Average for first score: 152.60
Average for second score: 148.30
Average for third score: 126.20
248.0 was bowled by Peperment Patty
2.0 was bowled by Marcey
....FINAL TABLE SHOULD LOOK LIKE THIS ....
-After sort-
Names score1 score2 score3 average score
---------------------------------------------------------------------------
Marcey 1 2 3 2.00
Charlie brown 1 2 12 5.00
roxie hallmark 100 100 2 67.33
Linus too good 100 23 210 111.00
Red Headed Girl 123 222 111 152.00
anna hallmark 222 111 211 181.33
Pig Pen 234 123 212 189.67
Snoopy 300 300 100 233.33
keith hallmark 222 300 180 234.00
Peperment Patty 223 300 221 248.00
#include <iostream>
#include <iomanip>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
class Bowler
{
private:
string name;
double score1, score2, score3, avgScore;
public:
Bowler()
{
this->name = "";
this->score1 = 0.0;
this->score2 = 0.0;
this->score3 = 0.0;
this->avgScore = 0.0;
}
Bowler(string name, double s1, double s2, double s3)
{
this->name = name;
this->score1 = s1;
this->score2 = s2;
this->score3 = s3;
calcAvgScore();
}
string getName(){ return this->name; }
double getScore1(){ return this->score1; }
double getScore2(){ return this->score2; }
double getScore3(){ return this->score3; }
double getAvgScore(){ return this->avgScore; }
void calcAvgScore()
{
this->avgScore = (this->score1 + this->score2 + this->score3) / 3;
}
};
// function prototype
bool compare2Bowlers(Bowler a, Bowler b);
int main()
{
vector<Bowler> bowlers;
bowlers.push_back(Bowler("Linus too good", 100, 23, 210));
bowlers.push_back(Bowler("Charlie brown", 1, 2, 12));
bowlers.push_back(Bowler("Snoopy", 300, 300, 100));
bowlers.push_back(Bowler("Peperment Patty", 223, 300, 221));
bowlers.push_back(Bowler("Pig Pen", 234, 123, 212));
bowlers.push_back(Bowler("Red Headed Girl", 123, 222, 111));
bowlers.push_back(Bowler("Marcey", 1, 2, 3));
bowlers.push_back(Bowler("keith hallmark", 222, 300, 180));
bowlers.push_back(Bowler("anna hallmark", 222, 111, 211));
bowlers.push_back(Bowler("roxie hallmark", 100, 100, 2));
// print average of 1st, 2nd and 3rd scores
int count = 0;
double sum1 = 0, sum2 = 0, sum3 = 0;
for(int i = 0; i < bowlers.size(); i++)
{
sum1 += bowlers[i].getScore1();
sum2 += bowlers[i].getScore2();
sum3 += bowlers[i].getScore3();
count++;
}
cout << setprecision(2) << fixed;
cout << "\nAverage for first score: " << (sum1 / (double)count) << endl;
cout << "Average for second score: " << (sum2 / (double)count) << endl;
cout << "Average for third score: " << (sum3 / (double)count) << endl;
// display the highest and lowest scores
cout << setprecision(1) << fixed;
double max = bowlers[0].getAvgScore();
int maxIndex = -1;
for(int i = 0; i < bowlers.size(); i++)
{
if(bowlers[i].getAvgScore() > max)
{
max = bowlers[i].getAvgScore();
maxIndex = i;
}
}
cout << max << " was bowled by " << bowlers[maxIndex].getName() << endl;
double min = bowlers[0].getAvgScore();
int minIndex = -1;
for(int i = 0; i < bowlers.size(); i++)
{
if(bowlers[i].getAvgScore() < min)
{
min = bowlers[i].getAvgScore();
minIndex = i;
}
}
cout << min << " was bowled by " << bowlers[minIndex].getName() << endl;
// sort the array
sort(bowlers.begin(), bowlers.end(), compare2Bowlers);
// display the table
cout << endl << "- After sort -" << endl;
cout << setw(10) << "Name" << setw(30) << "Score 1" << setw(20) << "Score 2" << setw(20) << "Score 3" << setw(20) << "Average Score" << endl;
for(int i = 0; i < bowlers.size(); i++)
{
cout << setw(10) << bowlers[i].getName() << setw(20) << bowlers[i].getScore1() << setw(20) << bowlers[i].getScore2() << setw(20) << bowlers[i].getScore3() << setw(20) << bowlers[i].getAvgScore() << endl;
}
cout << endl;
return 0;
}
bool compare2Bowlers(Bowler a, Bowler b)
{
return a.getAvgScore() < b.getAvgScore();
}
******************************************************************* SCREENSHOT ********************************************************