Question

(Small Question) Recode apple.cpp(The following code)such that it uses suggested STL algorithms(in the comments) instead of...

(Small Question) Recode apple.cpp(The following code)such that it uses suggested STL algorithms(in the comments) instead of hand-written loops

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <deque>
#include <string>

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 <Apples> 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<double>(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<Apples> 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 << "\nevery " << space << "\'d apple in the peck"<< endl;
  
// replace with advance()/next()/distance()
// no iterator 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
// accumulate() or 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;
  
}

Homework Answers

Answer #1

Source Code

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <deque>
#include <string>

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 <Apples> 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<double>(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<Apples> 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 << "\nevery " << space << "\'d apple in the peck"<< endl;
  
// replace with advance()/next()/distance()
// no iterator 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
// accumulate() or 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;
  
}

The Output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
Please fill in the blank bolded below that would be the best choice for this program....
Please fill in the blank bolded below that would be the best choice for this program. #include <iostream> using namespace std; const int size = 100000; class TheBig { public: double operator[](int index) const {return (theData[index]);} private: double theData[size]; }; void firstToBeThe( ______________________________________________________) { for (int i = 0; i <size; i++) cout << value[i] <<endl; } int main() { TheBig one; firstToBeThe(one); }
How do I make this code not include negative numbers in the total or average if...
How do I make this code not include negative numbers in the total or average if they are entered? (C++) For example, if you enter 4, 2, and -2, the average should not factor in the -2 and would be equal to 3. I want the code to factor in positive numbers only. ----- #include using namespace std; int main() {    int x, total = 0, count = 0;       cout << "Type in first value ";   ...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include <cmath> using namespace std; int getWeight(); float deliveryCharge(int weight); void displayCharge(int weight); int main () {    displayCharge(getWeight());    return 0; }   int getWeight() {    int weight;    cout << "Enter package weight (oz): ";    cin >> weight;    return weight; } float deliveryCharge(int weight) {    if (weight > 16)    {        return (((weight - 16) / 4) *...
c++ Language Fix this code to have it concatenate two strings st1 and st2 and put...
c++ Language Fix this code to have it concatenate two strings st1 and st2 and put the result in st3 with a space separator. it has to be done using array representation of strings #include <iostream> using namespace std; #include <string> int main() { string st1,st2, st3; int c = 0, i =0;    cout << "Enter a string: "; cin >> st1; cout << "Enter another string: "; cin >> st2;    while (st1[c] != '\0'){ st3[c] = st1[c];...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
Modify the sum_thread.cpp program to compute harmonic sum = 1 + 1/2 + 1/3 + 1/4...
Modify the sum_thread.cpp program to compute harmonic sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){...
There are 7 syntax errors and 3 semantic errors in the following code segment. Please mark...
There are 7 syntax errors and 3 semantic errors in the following code segment. Please mark them in the code and explain the reason briefly in place. #include<iostream> using namespace std; int main() { cout<<”Please input the radius (in integer): ”>>endl; int 1var = 10, var=20; double area = 40; cin<< var; //get an input of the radius and store in var float continue; float const pi = 2.64; pi += 0.5; do { cout<<”The radius is ”<<”var”<<endl; //print the...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT