Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Recode apple sorting example such that it uses suggested STL algorithms instead of hand-written loops. apple sorting example #include #include #include #include #include #include using

Recode apple sorting example such that it uses suggested STL algorithms instead of hand-written loops.

apple sorting example

#include  #include  #include  #include  #include  #include  using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::deque; struct Apples{ double weight; // oz string color; // red or green void print() const { cout << color << ", " << weight << endl; } }; int main(){ srand(time(nullptr)); const double minWeight = 8.; const double maxWeight = 3.; cout << "Input crate size: "; int size; cin >> size; vector  crate(size); // assign random weight and color to apples in the crate // replace with generate() for(auto it = crate.begin(); it != crate.end(); ++it){ it->weight = minWeight + static_cast(rand())/RAND_MAX*(maxWeight - minWeight); it->color = rand() % 2 == 1 ? "green" : "red"; } cout << "Enter weight to find: "; double toFind; cin >> toFind; // count_if() int cnt = 0; for(auto it = crate.cbegin(); it != crate.cend(); ++it) if(it->weight > toFind) ++cnt; cout << "There are " << cnt << " apples heavier than " << toFind << " oz" << endl; // find_if() cout << "at positions "; for(int i=0; i < size; ++i) if(crate[i].weight > toFind) cout << i << ", "; cout << endl; // max_element() double heaviest = crate[0].weight; for(int i=1; i < size; ++i) if(crate[i].weight > heaviest) heaviest = crate[i].weight; cout << "Heaviest apple weighs: " << heaviest << " oz" << endl; // for_each() or accumulate() double sum = 0; for(int i=0; i < size; ++i) sum += crate[i].weight; cout << "Total apple weight is: " << sum << " oz" << endl; // transform(); cout << "How much should they grow: "; double toGrow; cin >> toGrow; for(int i=0; i < crate.size(); ++i) crate[i].weight += toGrow; // remove_if() cout << "Input minimum acceptable weight: "; double minAccept; cin >> minAccept; for(auto it=crate.begin(); it != crate.end(); ) if(it->weight < minAccept) it = crate.erase(it); else ++it; cout << "removed " << size - crate.size() << " elements" << endl; // bubble sort, replace with sort() bool swapped; do{ swapped = false; for(int i=0; i < crate.size()-1; ++i) if(crate[i].weight > crate[i+1].weight){ std::swap(crate[i], crate[i+1]); swapped = true; } }while(swapped); // moving all red apples from crate to peck // remove_copy_if() with back_inserter()/front_inserter() or equivalents deque peck; for(auto it=crate.begin(); it != crate.end();) if(it->color == "red"){ peck.push_front(std::move(*it)); it=crate.erase(it); }else ++it; // for_each() possibly cout << "apples in the create"<< endl; for(const auto &e: crate) { e.print(); } cout << endl; // for_each() possibly cout << "apples in the peck"<< endl; for(const auto &e: peck) { e.print(); } // prints every "space" apple in the peck // const int space=3; cout << " every " << space << "\'d apple in the peck"<< endl; // replace with advance()/next()/distance() // no pointer arithmetic auto it=peck.cbegin(); int i = 0; while(it != peck.cend()){ if(i == space){ it->print(); i=0; } ++i; ++it; } // putting all small green apples in a jam // use a binder to create a functor with configurable max weight // count_if() then remove_if() const double weightToJam = 10.0; double jamWeight = 0; for(auto it=crate.begin(); it != crate.end();) if(it->weight < weightToJam){ jamWeight += it->weight; it=crate.erase(it); }else ++it; cout << "Weight of jam is: " << jamWeight << endl; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

Evaluate the importance of diversity in the workforce.

Answered: 1 week ago

Question

Identify the legal standards of the recruitment process.

Answered: 1 week ago