[SOLVED] algorithm CS 32 Summer 2019

$25

File Name: algorithm_CS_32__Summer_2019.zip
File Size: 263.76 KB

5/5 - (1 vote)

CS 32 Summer 2019
E. Ambrosio
Project 4 Warmup
We anticipate that many people working on Project 4 will spend a lot of time debugging something that arises from a common novice misunderstanding. To save you that time later, well give you a chance to make that mistake in a simpler context, so you can work out that issue and how it manifests itself. (It may turn out that you dont have that misunderstanding, so you wont have any problems during this warmup. Still, keep this warmup in mind, because you may still make the mistake in Project 4.)
1. ImplementtheremoveOddsfunction.
#include #include
#include
#include
#include
using namespace std;
// Remove the odd integers from li.
// It is acceptable if the order of the remaining even
// integers is not the same as in the original list.
void removeOdds(list & li)
{
}
void test() {
int a[8] = { 2, 8, 5, 6, 7, 3, 4, 1 };
list x(a, a+8); // construct x from the array
assert(x.size() == 8 && x.front() == 2 && x.back() ==
1);
removeOdds(x);
assert(x.size() == 4);
vector v(x.begin(), x.end()); // construct v from x
sort(v.begin(), v.end());
int expect[4] = { 2, 4, 6, 8 };
for (int k = 0; k < 4; k++)assert(v[k] == expect[k]);}int main() {test(); cout << “Passed” << endl;} CS 32 Summer 2019E. Ambrosio2. ImplementtheremoveOddsfunction.#include
#include
#include
#include
using namespace std;
// Remove the odd integers from v.
// It is acceptable if the order of the remaining even integers
// is not the same as in the original vector.
void removeOdds(vector & v)
{
}
void test() {
int a[8] = { 2, 8, 5, 6, 7, 3, 4, 1 };
vector x(a, a+8); // construct x from the array
assert(x.size() == 8 && x.front() == 2 && x.back() ==
1);
removeOdds(x);
assert(x.size() == 4);
sort(x.begin(), x.end());
int expect[4] = { 2, 4, 6, 8 };
for (int k = 0; k < 4; k++)assert(x[k] == expect[k]);}int main() {test(); cout << “Passed” << endl;}3. ImplementtheremoveBadfunction.#include #include
#include
#include
#include
using namespace std;
vector destroyedOnes;

CS 32 Summer 2019
E. Ambrosio
class Movie {
public:
Movie(int r) : m_rating(r) {}
~Movie() { destroyedOnes.push_back(m_rating); }
int rating() const { return m_rating; }
private:
int m_rating;
};
// Remove the movies in li with a rating below 50 and
// destroy them. It is acceptable if the order of the
// remaining movies is not the same as in the original
// list.
void removeBad(list & li)
{
}
void test() {
int a[8] = { 85, 80, 30, 70, 20, 15, 90, 10 };
list x;
for (int k = 0; k < 8; k++)x.push_back(new Movie(a[k])); assert(x.size() == 8 && x.front()->rating() == 85 &&
x.back()->rating() == 10);
removeBad(x);
assert(x.size() == 4 && destroyedOnes.size() == 4);
vector v;
for (list ::iterator p = x.begin(); p != x.end(); p++)
{
Movie* mp = *p;
v.push_back(mp->rating());
}
// Aside: In C++11, the above loop could be
// for (auto p = x.begin(); p != x.end(); p++)
// {
// Movie* mp = *p;
// v.push_back(mp->rating());
// }
// or
// for (auto p = x.begin(); p != x.end(); p++)
// {
// auto mp = *p;
// v.push_back(mp->rating());
// }
// or

CS 32 Summer 2019
E. Ambrosio
// for (Movie* mp : x)
// v.push_back(mp->rating());
// or
// for (auto mp : x)
// v.push_back(mp->rating());
sort(v.begin(), v.end());
int expect[4] = { 70, 80, 85, 90 };
for (int k = 0; k < 4; k++)assert(v[k] == expect[k]); sort(destroyedOnes.begin(), destroyedOnes.end()); int expectGone[4] = { 10, 15, 20, 30 }; for (int k = 0; k < 4; k++)assert(destroyedOnes[k] == expectGone[k]);}int main() {test(); cout << “Passed” << endl;}4. ImplementtheremoveBadfunction.#include
#include
#include
#include
using namespace std;
vector destroyedOnes;
class Movie {
public:
Movie(int r) : m_rating(r) {}
~Movie() { destroyedOnes.push_back(m_rating); }
int rating() const { return m_rating; }
private:
int m_rating;
};

CS 32 Summer 2019 E. Ambrosio
// Remove the movies in v with a rating below 50 and destroy
// them. It is acceptable if the order of the remaining movies
// is not the same as in the original vector.
void removeBad(vector & v)
{
}
void test() {
int a[8] = { 85, 80, 30, 70, 20, 15, 90, 10 };
vector x;
for (int k = 0; k < 8; k++)x.push_back(new Movie(a[k])); assert(x.size() == 8 && x.front()->rating() == 85 &&
x.back()->rating() == 10);
removeBad(x);
assert(x.size() == 4 && destroyedOnes.size() == 4);
vector v;
for (int k = 0; k < 4; k++)v.push_back(x[k]->rating());
sort(v.begin(), v.end());
int expect[4] = { 70, 80, 85, 90 };
for (int k = 0; k < 4; k++)assert(v[k] == expect[k]); sort(destroyedOnes.begin(), destroyedOnes.end()); int expectGone[4] = { 10, 15, 20, 30 }; for (int k = 0; k < 4; k++)assert(destroyedOnes[k] == expectGone[k]);}int main() {test(); cout << “Passed” << endl;}

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] algorithm CS 32 Summer 2019
$25