Question

In: Computer Science

IN C ++ PLEASE CODE FOR BUBBLE SORT---Add code to sort the bowlers. You have to...

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

Solutions

Expert Solution

#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 ********************************************************


Related Solutions

Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a data set (txt file) then do the sorting algorithm to measure how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND BUBBLE SORT FUNCTION TO THIS PROGRAM #include <iostream> #include<vector> #include <algorithm >   #include <chrono>    #include <ctime> using namespace std; void bubblesSort() { // Please create Bubble Sort function// Make another for Selection Sort and  Insertion Sort } int main() { // empty vector vector<int> data; // data [0], data [1]... data[N-1] <-- end(data) // set of values to test N for (auto N :...
Please use the code I provided!! Use either bubble sort or selection sort!! Thank you in...
Please use the code I provided!! Use either bubble sort or selection sort!! Thank you in advance This lab involves adding a sorting function to an existing C++ template. In this module, a file is provided for you -- SortableBag.h -- that implements a simple "bag" (unsorted collection of numbers) structure with array storage. Part 1 Add a sort() method to this template, which should not return any values or take any arguments ('void' for both the return type and...
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into ascending order. Use the Bubble Sort algorithm from the lecture. You can use either Base Addressing or Indexed Addressing for the arrays. For this assignment, make sure you prompt the user for the numbers. Do not hard-code them in the data section. NOTE: Declare the array last in the Data section.
Bubble and Selection Sort For this assignment, you are to consider bubble and selection sort. Both...
Bubble and Selection Sort For this assignment, you are to consider bubble and selection sort. Both are O(n^2) however it may be possible to classify one algorithm as being more efficient than the other. You are to discuss which algorithm you feel is the most efficient and in what cases it will be more efficient. Provide any relevant test cases and code to support your belief. Submit a pdf containing your findings and test results along with any relevant code...
The following flow chart is a bubble sort. Write the code for this sort. Make sure...
The following flow chart is a bubble sort. Write the code for this sort. Make sure you display each pass and comparison. Include comments.
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
How would I add a quickSort function to the below C++ code to sort the randomly...
How would I add a quickSort function to the below C++ code to sort the randomly generated numbers? #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int i; int array[10]; int odd; int Max; int counter = 0; int main() { cout << "The 10 random elements are: "; cout << endl; srand ( time(0) ); for (int j = 0; j < 99; j++) { i = rand() % 100; if (i != i - 1) array[j] =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT