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 main runs successfully. Use the lines to construct the code, then complete the code in the correct order.

  1. class room {
        private:
            int length;
            int width;
            int height;
  2. int calculate_volume () {
        return length * width * height;
    }
  3. int get_height () const {
        return height;
    }
  4. int get_length () const {
        return length;
    }
  5. int get_width () const {
        return width;
    }
  6. int main() {
  7. public:
        int calculate_area () {
            return length * width;
        }
  8. r.set_height(10);
  9. r.set_length(12);
  10. r.set_width(14);
  11. room r;
  12. 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';
  13. void set_height (int h) {
        height = h;
    }
  14. void set_length (int l) {
        length = l;
    }
  15. void set_width (int w) {
        width = w;
    }
  16. }
  17. };

in main create a temp object to calculate what 100 degrees Celsius is in Fahrenheit. Use the lines to construct the code, then complete the code in the correct order.

  1. int main() {
  2. t.print_temp();
  3. t.set_celsius(100);
  4. t.set_fahrenheit(t.get_fahrenheit());
  5. temp t;
  6. }

What if we had an existing vector with data that we want to copy into our my_vector? Write a constructor that takes a vector and copies the data into the elements vector. Use the lines to construct the code, then complete the code in the correct order.

  1. elements = vec;
  2. my_vector (std::vector<int> vec) {
  3. }

Now we can write some of our own fun functions! No longer do we need to write for loops every time we want to print out a vector. With my_vector, we can just call the member function print! Write the my_vector member function print, which prints out the contents of my_vector. For example, if our my_vector contained the elements 2, 5, 1, and 8, print should print out [2, 5, 1, 8] followed by a newline. Use the lines to construct the code, then complete the code in the correct order.

  1. for (std::size_t i = 0; i < elements.size(); ++i) {
  2. if (i != 0) std::cout << ", ";
    std::cout << elements[i];
  3. std::cout << '[';
  4. std::cout << ']' << '\n';
  5. void print() {
  6. }
  7. }

What if we wanted to return the largest and smallest elements in our my_vector? Write the public member functions max and min which calls the private member functions find_max and find_min. find_max and find_min return the indices of the max and min values, and max and min call 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 the max and min functions in public when you complete the code.

  1. // find_max private member function
    std::size_t find_max (const std::vector<int>& vec) {
  2. // find_min private member function
    std::size_t find_min (const std::vector<int>& vec) {
  3. // max public member function
    int my_vector::max () {
  4. // min public member function
    int my_vector::min () {
  5. for (std::size_t i = 0; i < vec.size(); i++) {
  6. for (std::size_t i = 1; i < vec.size(); i++) {
  7. if (vec.empty()) throw std::out_of_range("empty vector");
    std::size_t in_max = 0;
  8. if (vec.empty()) throw std::out_of_range("empty vector");
    std::size_t in_min = 0;
  9. if (vec[i] < min) {
  10. if (vec[i] > max) {
  11. in_max = i;
  12. in_min = i;
  13. int max = vec[0];
  14. int min = vec[0];
  15. max = vec[i];
  16. min = vec[i];
  17. return elements[find_max(elements)];
  18. return elements[find_min(elements)];
  19. return in_max;
  20. return in_min;
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }
  28. }

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.

  1.       double calculate_area () { return 3.14 * radius * radius; }
    };
  2. circle (double r) { radius = r; }
  3. circle (int r) { radius = r; }  #distractor
  4. class circle {
  5. double radius;
  6. private {  #distractor
  7. private:
  8. public:
  9. struct circle {  #distractor
  10. }  #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.

  1. class circle {
       private:
          double radius;
       public:
          circle (double r) { radius = r; }
          double calculate_area () { return 3.14 * radius * radius; }
  2. double get_radius () {
  3. double set_radius (double r) {  #distractor
  4. else { radius = r; }
  5. if (r < 0) { std::cout << "Error! Cannot have a negative radius!" << '\n'; }
  6. return radius;
  7. void get_radius () {  #distractor
  8. void set_radius (double r) {
  9. }
  10. }
  11. };

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.

  1. c.radius = 2.4;  #distractor
  2. c.radius = 3.6;  #distractor
  3. circle c(2.4);
  4. circle c;  #distractor
  5. int main() {
  6. s.set_radius (3.6);
  7. std::cout << "New radius: " << c.get_radius () << '\n';
  8. std::cout << "New radius: " << c.radius << '\n';  #distractor
  9. std::cout << "Radius: " << c.get_radius () << '\n';
  10. std::cout << "Radius: " << c.radius << '\n';  #distractor
  11. }

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.

  1. class point {
  2. double get_x () { return x; }
  3. double get_y () { return y; }
  4. double x, y;
  5. point () { x = 0; y = 0; }
  6. point (double x_val, double y_val) { x = x_val; y = y_val; }
  7. private:
  8. public:
  9. void set_x (double x_val) { x = x_val; }
  10. void set_y (double y_val) { y = y_val; }
  11. };

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.

  1. class rectangle {
  2. double length, height;
  3. point get_lower_right () { return lower_right; }
  4. point get_upper_left () { return upper_left; }
  5. point upper_left, lower_right;
  6. private:
  7. public:
  8. rectangle (point up_left, point low_right) { upper_left = up_left; lower_right = low_right; }
  9. void set_lower_right (point p) { lower_right = p; }
  10. void set_upper_left (point p) { upper_left = p; }
  11. };

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.

  1. double rectangle::calculate_area () {
  2. double rectangle::calculate_sides () {
  3. height = get_upper_left().get_y() - get_lower_right().get_y();
  4. height = upper_left.y - lower_right.y;  #distractor
  5. length = get_lower_right().get_x() - get_upper_left().get_x();
  6. length = lower_right.x - upper_left.x;  #distractor
  7. return get_length() * get_height();  #distractor
  8. return height;  #distractor
  9. return length * height;
  10. return length;  #distractor
  11. void rectangle::calculate_sides () {
  12. }
  13. }

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.

  1. int main() {
  2. point p1(2.5, 7.5);
  3. point p2(8, 1.5);
  4. point p3(4.2, 10.7);
  5. r.calculate_sides();
  6. r.calculate_sides();
  7. r.set_upper_left(p3);
  8. r.upper_left = p3;  #distractor
  9. rectangle r(p1, p2);
  10. rectangle r(p2, p1);  #distractor
  11. std::cout << "Area: " << r.calculate_area() << '\n';
  12. std::cout << "length: " << r.get_length() << ", Height: " << r.get_height() << '\n';  #distractor
  13. std::cout << "length: " << r.length << ", Height: " << r.height << '\n';
  14. std::cout << "New area: " << r.calculate_area() << '\n';
  15. }

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.

  1. class date {
  2. date () { day = 0; month = 0; year = 0; }  #distractor
  3. date () { day = 1; month = 1; year = 2000; }
  4. date (int d, int m, int y) { day = d; month = m; year = y; }
  5. int day, month, year;
  6. int get_day () { return day; }
  7. int get_month () { return month; }
  8. int get_year () { return year; }
  9. private:
  10. public:
  11. std::vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  12. void set_day (int d) { if (d > 0 && d < 32) day = d; }
  13. void set_day (int d) { if (d > 1 && d < 31) day = d; }  #distractor
  14. void set_month (int m) { if (m > 0 && m < 13) month = m; }
  15. void set_month (int m) { if (m >= 0 && m <= 12) month = m; }  #distractor
  16. void set_year (int y) { if (y >= 2000 && y < 3000) year = y; }  #distractor
  17. void set_year (int y) { year = y; }
  18. };

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.

  1. else {
  2. if (get_year() < 0) {
  3. std::cout << get_month() << '/' << get_day() << '/' << -get_year() << " BCE" << '\n';
  4. std::cout << get_month() << '/' << get_day() << '/' << get_year() << " BCE" << '\n';  #distractor
  5. std::cout << get_month() << '/' << get_day() << '/' << get_year() << " CE" << '\n';
  6. std::cout << month << '/' << day << '/' << year << " BCE" << '\n';  #distractor
  7. void date::print_date () {
  8. }
  9. }
  10. }

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.

  1. bool date::is_leap_year () {
  2. else if (get_year() % 100 != 0) { return true; }
  3. else if (get_year() % 400 != 0) { return false; }
  4. else {
  5. else { return false; }  #distractor
  6. else { return true; }
  7. if (get_year() % 4 != 0) { return false; }
  8. if (get_year() % 4 == 0) { return false; }  #distractor
  9. if (is_leap_year() && get_month() == 2) {
  10. if (is_leap_year()) {  #distractor
  11. int date::last_day_in_month () {
  12. return days_in_month[get_month() - 1] + 1;
  13. return days_in_month[get_month() - 1];
  14. return days_in_month[get_month()];  #distractor
  15. }
  16. }
  17. }