14.15. Coding Practice¶
Activecode
There are errors in the code below. Modify the code so that
mainruns successfully. Select the Parsonsprob tab for hints for the construction of the code.
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
mainruns successfully. Use the lines to construct the code, then go back to complete the Activecode tab.
-
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() << std::endl; -
void set_height (int h) { height = h; } -
void set_length (int l) { length = l; } -
void set_width (int w) { width = w; } -
} -
};
Activecode
in
maincreate atempobject to calculate what 100 degrees Celsius is in Fahrenheit. Select the Parsonsprob tab for hints for the construction of the code.
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
maincreate atempobject to calculate what 100 degrees Celsius is in Fahrenheit. Use the lines to construct the code, then go back to complete the Activecode tab.
-
int main() { -
t.print_temp(); -
t.set_celsius(100); -
t.set_fahrenheit(t.get_fahrenheit()); -
temp t; -
}
Activecode
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. Select the Parsonsprob tab for hints for the construction of the code.
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
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 go back to complete the Activecode tab.
-
elements = vec; -
my_vector (std::vector<int> vec) { -
}
Activecode
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,
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
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 << "]" << std::endl; -
void print() { -
} -
}
Activecode
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. Select the Parsonsprob tab for hints for the construction of the code.
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 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 go back to complete the Activecode tab. Include<stdexcept>for the empty-vector check. Be sure to declare themaxandminfunctions inpublicwhen you complete the Activecode.
-
// 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; -
} -
} -
} -
} -
} -
} -
} -
}