14.13. Mixed-Up Code Exercises¶
Arrange the following Mixed-Up Code exercises to assess what you have learned in this chapter.
14.13.1. Parsons exercises¶
There are errors in the code below. Modify the code so that
mainruns successfully. Use the lines to construct the code, then complete the code in the correct order.
-
class room { private: int length; int width; int height; -
int calculate_volume () { return length * width * height; } -
int get_height () const { return height; } -
int get_length () const { return length; } -
int get_width () const { return width; } -
int main() { -
public: int calculate_area () { return length * width; } -
r.set_height(10); -
r.set_length(12); -
r.set_width(14); -
room r; -
std::cout << "The room with dimensions " << r.get_length() << ", " << r.get_width() << ", and " << r.get_height() << " has an area of " << r.calculate_area() << " and a volume of " << r.calculate_volume() << '\n'; -
void set_height (int h) { height = h; } -
void set_length (int l) { length = l; } -
void set_width (int w) { width = w; } -
} -
};
in
maincreate atempobject to calculate what 100 degrees Celsius is in Fahrenheit. Use the lines to construct the code, then complete the code in the correct order.
-
int main() { -
t.print_temp(); -
t.set_celsius(100); -
t.set_fahrenheit(t.get_fahrenheit()); -
temp t; -
}
What if we had an existing
vectorwith data that we want to copy into ourmy_vector? Write a constructor that takes avectorand copies the data into theelementsvector. Use the lines to construct the code, then complete the code in the correct order.
-
elements = vec; -
my_vector (std::vector<int> vec) { -
}
Now we can write some of our own fun functions! No longer do we need to write
forloops every time we want to print out avector. Withmy_vector, we can just call the member functionmy_vectormember functionmy_vector. For example, if ourmy_vectorcontained the elements 2, 5, 1, and 8,
-
for (std::size_t i = 0; i < elements.size(); ++i) { -
if (i != 0) std::cout << ", "; std::cout << elements[i]; -
std::cout << '['; -
std::cout << ']' << '\n'; -
void print() { -
} -
}
What if we wanted to return the largest and smallest elements in our
my_vector? Write the public member functionsmaxandminwhich calls the private member functionsfind_maxandfind_min.find_maxandfind_minreturn the indices of the max and min values, andmaxandmincall these private member functions and return the max and min values. Use the lines to construct the code, then complete the code in the correct order. Include<stdexcept>for the empty-vector check. Be sure to declare themaxandminfunctions inpublicwhen you complete the code.
-
// find_max private member function std::size_t find_max (const std::vector<int>& vec) { -
// find_min private member function std::size_t find_min (const std::vector<int>& vec) { -
// max public member function int my_vector::max () { -
// min public member function int my_vector::min () { -
for (std::size_t i = 0; i < vec.size(); i++) { -
for (std::size_t i = 1; i < vec.size(); i++) { -
if (vec.empty()) throw std::out_of_range("empty vector"); std::size_t in_max = 0; -
if (vec.empty()) throw std::out_of_range("empty vector"); std::size_t in_min = 0; -
if (vec[i] < min) { -
if (vec[i] > max) { -
in_max = i; -
in_min = i; -
int max = vec[0]; -
int min = vec[0]; -
max = vec[i]; -
min = vec[i]; -
return elements[find_max(elements)]; -
return elements[find_min(elements)]; -
return in_max; -
return in_min; -
} -
} -
} -
} -
} -
} -
} -
}
Let's write the class definition for circle. circle should have its
radius stored in a private member variable. Also write the constructor
for circle, which takes a radius as a parameter, in addition to the
public member function calculate_area, which returns the area of
the circle. Make sure to include the private and public keywords!
Use 3.14 for the value of pi. Put the necessary
blocks of code in the correct order.
-
double calculate_area () { return 3.14 * radius * radius; } }; -
circle (double r) { radius = r; } -
circle (int r) { radius = r; } #distractor -
class circle { -
double radius; -
private { #distractor -
private: -
public: -
struct circle { #distractor -
} #distractor
Now that we have our circle class, let's write some accessor
functions! Write the circle member functions get_radius
and set_radius. It doesn't make sense for a circle's
radius to be negative, so in your set_radius function,
output an error message if the given radius is negative.
Put the necessary blocks of code in the correct order.
-
class circle { private: double radius; public: circle (double r) { radius = r; } double calculate_area () { return 3.14 * radius * radius; } -
double get_radius () { -
double set_radius (double r) { #distractor -
else { radius = r; } -
if (r < 0) { std::cout << "Error! Cannot have a negative radius!" << '\n'; } -
return radius; -
void get_radius () { #distractor -
void set_radius (double r) { -
} -
} -
};
Write a main. in main, create a circle with radius 2.4
and output the radius. Then change the radius to 3.6 and output
the new radius. Put the necessary blocks of code in the correct order.
-
c.radius = 2.4; #distractor -
c.radius = 3.6; #distractor -
circle c(2.4); -
circle c; #distractor -
int main() { -
s.set_radius (3.6); -
std::cout << "New radius: " << c.get_radius () << '\n'; -
std::cout << "New radius: " << c.radius << '\n'; #distractor -
std::cout << "Radius: " << c.get_radius () << '\n'; -
std::cout << "Radius: " << c.radius << '\n'; #distractor -
}
A rectangle can be constructed given only two points. First,
write the class definition for point, which stores an x and
a y value in private member variables. Also write the default constructor, which
sets x and y to 0, and a constructor that takes in an x_val and y_val.
in addition, write its accessor functions,
get_x, get_y, set_x, and set_y.
Put the necessary blocks of code in the correct order.
-
class point { -
double get_x () { return x; } -
double get_y () { return y; } -
double x, y; -
point () { x = 0; y = 0; } -
point (double x_val, double y_val) { x = x_val; y = y_val; } -
private: -
public: -
void set_x (double x_val) { x = x_val; } -
void set_y (double y_val) { y = y_val; } -
};
Now that we've defined the point class, we can go back to
writing the rectangle class. rectangle should store
it's upper-left and lower-right points as private member variables.
Write accessor functions for these variables after the constructor.
It should also have length and height stored as public member variables.
Also write a constructor that
takes an upper-left point and a lower-right point as parameters.
-
class rectangle { -
double length, height; -
point get_lower_right () { return lower_right; } -
point get_upper_left () { return upper_left; } -
point upper_left, lower_right; -
private: -
public: -
rectangle (point up_left, point low_right) { upper_left = up_left; lower_right = low_right; } -
void set_lower_right (point p) { lower_right = p; } -
void set_upper_left (point p) { upper_left = p; } -
};
Write the rectangle member function calculate_sides, which finds
the length and height of the rectangle using the stored point``s.
Afterwards, write the ``rectangle member function calculate_area,
which returns the area of the rectangle.
-
double rectangle::calculate_area () { -
double rectangle::calculate_sides () { -
height = get_upper_left().get_y() - get_lower_right().get_y(); -
height = upper_left.y - lower_right.y; #distractor -
length = get_lower_right().get_x() - get_upper_left().get_x(); -
length = lower_right.x - upper_left.x; #distractor -
return get_length() * get_height(); #distractor -
return height; #distractor -
return length * height; -
return length; #distractor -
void rectangle::calculate_sides () { -
} -
}
Write a main in main, create a rectangle with corners
at (2.5, 7.5) and (8, 1.5). Print out the length and height, calculate the area,
and print out the area. Then change the upper_left corner to be at (4.2, 10.7) and
print out the new area.
-
int main() { -
point p1(2.5, 7.5); -
point p2(8, 1.5); -
point p3(4.2, 10.7); -
r.calculate_sides(); -
r.calculate_sides(); -
r.set_upper_left(p3); -
r.upper_left = p3; #distractor -
rectangle r(p1, p2); -
rectangle r(p2, p1); #distractor -
std::cout << "Area: " << r.calculate_area() << '\n'; -
std::cout << "length: " << r.get_length() << ", Height: " << r.get_height() << '\n'; #distractor -
std::cout << "length: " << r.length << ", Height: " << r.height << '\n'; -
std::cout << "New area: " << r.calculate_area() << '\n'; -
}
Let's write the date class. date stores information
about the day, month, and year in private variables, in addition to a vector
of the number of days in each month. Write accessor functions
for each variable, keeping in mind the valid values each variable can take.
in addition, write the default constructor, which initializes
the date to January 1, 2000. Write another constructor which takes in a day,
month, and year in that order.
-
class date { -
date () { day = 0; month = 0; year = 0; } #distractor -
date () { day = 1; month = 1; year = 2000; } -
date (int d, int m, int y) { day = d; month = m; year = y; } -
int day, month, year; -
int get_day () { return day; } -
int get_month () { return month; } -
int get_year () { return year; } -
private: -
public: -
std::vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; -
void set_day (int d) { if (d > 0 && d < 32) day = d; } -
void set_day (int d) { if (d > 1 && d < 31) day = d; } #distractor -
void set_month (int m) { if (m > 0 && m < 13) month = m; } -
void set_month (int m) { if (m >= 0 && m <= 12) month = m; } #distractor -
void set_year (int y) { if (y >= 2000 && y < 3000) year = y; } #distractor -
void set_year (int y) { year = y; } -
};
Let's write the date member function, print_date,
which prints the date out in the following format: month/day/year CE/BCE
depending on whether the year is negative or not.
-
else { -
if (get_year() < 0) { -
std::cout << get_month() << '/' << get_day() << '/' << -get_year() << " BCE" << '\n'; -
std::cout << get_month() << '/' << get_day() << '/' << get_year() << " BCE" << '\n'; #distractor -
std::cout << get_month() << '/' << get_day() << '/' << get_year() << " CE" << '\n'; -
std::cout << month << '/' << day << '/' << year << " BCE" << '\n'; #distractor -
void date::print_date () { -
} -
} -
}
Write the date member function is_leap_year, which returns true if
the year is a leap year. Then write the date member function last_day_in_month,
which returns the last day in the date's month.
-
bool date::is_leap_year () { -
else if (get_year() % 100 != 0) { return true; } -
else if (get_year() % 400 != 0) { return false; } -
else { -
else { return false; } #distractor -
else { return true; } -
if (get_year() % 4 != 0) { return false; } -
if (get_year() % 4 == 0) { return false; } #distractor -
if (is_leap_year() && get_month() == 2) { -
if (is_leap_year()) { #distractor -
int date::last_day_in_month () { -
return days_in_month[get_month() - 1] + 1; -
return days_in_month[get_month() - 1]; -
return days_in_month[get_month()]; #distractor -
} -
} -
}