14.15. Coding Practice

Activecode

There are errors in the code below. Modify the code so that main runs successfully. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_14_ac_2q
 1#include <iostream>
 2
 3class room {
 4    private:
 5        int length;
 6        int width;
 7        int height;
 8
 9    public:
10        int calculate_area () {
11            return length * width;
12        }
13
14        int calculate_volume () {
15            return length * width * height;
16        }
17
18        // Add any necessary functions here.
19};
20
21int main() {
22    room r;
23    r.length = 12;
24    r.width = 14;
25    r.height = 10;
26    std::cout << "The room with dimensions " << r.length ", " << r.width
27        << ", and " << r.height << " has an area of " << r.calculate_area()
28        << " and a volume of " << r.calculate_volume << std::endl;
29}

Parsonsprob

There are errors in the code below. Modify the code so that main runs successfully. Use the lines to construct the code, then go back to complete the Activecode tab.

  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() << std::endl;
  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. };

Activecode

in main create a temp object to calculate what 100 degrees Celsius is in Fahrenheit. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_14_ac_4q
 1#include <iostream>
 2
 3class temp {
 4    private:
 5        double fahrenheit;
 6        double celsius;
 7        bool is_fahrenheit;
 8        bool is_celsius;
 9
10        double c_to_f() {
11            return celsius * 9/5 + 32;
12        }
13
14        double f_to_c() {
15            return (fahrenheit - 32) * 5/9;
16        }
17
18    public:
19        double get_fahrenheit () {
20            if (is_celsius) { return c_to_f(); }
21            else { return fahrenheit; }
22        }
23        double get_celsius () {
24            if (is_fahrenheit) { return f_to_c(); }
25            else { return celsius; }
26        }
27        void set_fahrenheit (double f) { fahrenheit = f; is_fahrenheit = true; is_celsius = false; }
28        void set_celsius (double c) { celsius = c; is_celsius = true; is_fahrenheit = false; }
29        void print_temp () {
30            if (is_fahrenheit) {
31                std::cout << "It is " << get_fahrenheit() << " degrees Fahrenheit" << std::endl;
32            }
33            else {
34                std::cout << "It is " << get_celsius() << " degrees Celsius" << std::endl;
35            }
36        }
37};
38
39int main() {
40    // Write your code here.
41}

Parsonsprob

in main create a temp object to calculate what 100 degrees Celsius is in Fahrenheit. Use the lines to construct the code, then go back to complete the Activecode tab.

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

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_14_ac_6q
 1#include <iostream>
 2#include <vector>
 3
 4class my_vector {
 5    private:
 6        std::vector<int> elements;
 7
 8    public:
 9        my_vector() {};
10        // Write your constructor here.
11};

Parsonsprob

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 go back to complete the Activecode tab.

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

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_14_ac_8q
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5
 6class my_vector {
 7    private:
 8        std::vector<int> elements;
 9
10    public:
11        my_vector() {};
12        my_vector(std::vector<int> vec);
13
14        std::size_t size();
15        void push_back(int value);
16        void pop_back();
17        int at(std::size_t index);
18
19        // Write your print function here.
20};
21
22int main() {
23    my_vector my_vec;
24    my_vec.push_back(13);
25    my_vec.push_back(2);
26    my_vec.push_back(4);
27    my_vec.push_back(7);
28    my_vec.push_back(9);
29    my_vec.push_back(24);
30    my_vec.print();
31}

Parsonsprob

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 go back to complete the Activecode tab.

  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 << "]" << std::endl;
  5. void print() {
  6. }
  7. }

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_14_ac_10q
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7class my_vector {
 8    private:
 9        std::vector<int> elements;
10
11        // Write your find_max function here.
12
13        // Write your find_min function here.
14
15    public:
16        my_vector() {};
17        my_vector(std::vector<int> vec);
18
19        std::size_t size();
20        void push_back(int value);
21        void pop_back();
22        int at(std::size_t index);
23        void print();
24        void push_front(int value);
25        void pop_front();
26};
27
28// Write your max function here.
29
30// Write your min function here.
31
32int main() {
33    std::vector<int> vec = { 8, 1, 5, 87, 23, 64 };
34    my_vector my_vec(vec);
35    cout << "The largest element is " << my_vec.max() << std::endl;
36    cout << "The smallest element is " << my_vec.min() << std::endl;
37}

Parsonsprob

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 go back to complete the Activecode tab. Include <stdexcept> for the empty-vector check. Be sure to declare the max and min functions in public when you complete the Activecode.

  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. }