9.14. Coding PracticeΒΆ

Q1

Question

Write the cake structure, which has instance variables name, color, diameter, and has_icing.

Example cp_9_AC_1q
1#include <iostream>
2using namespace std;
3
4// Write your code for the struct cake here.
5
6int main() {
7    cake c = { "Mary", "blue", 3.5, false };
8}

Answer

Below is one way to implement the program. We declare the cake struct and list the instance variables in order.

Example cp_9_AC_1a
 1#include <iostream>
 2using namespace std;
 3
 4struct cake {
 5    string name;
 6    string color;
 7    double diameter;
 8    bool has_icing;
 9};
10
11int main() {
12    cake c = { "Mary", "blue", 3.5, false };
13}

Q2

Write the function print_cake_info, which prints the cake's information in the format "This is a color, diameter inch diameter cake with/without icing." If name does not have the value "n/a", print_cake_info prints out "Happy birthdday name! Your cake is color, has a diameter inch diameter, and comes with/without icing."

Example cp_9_AC_2q
 1#include <iostream>
 2using namespace std;
 3
 4struct cake {
 5    string name;
 6    string color;
 7    double diameter;
 8    bool has_icing;
 9};
10
11// Write your code for the print_cake_info function here.
12
13int main() {
14    cake c1 = { "n/a", "red", 12.5, true };
15    print_cake_info (c1);
16    cake c2 = { "Tom", "white", 10, false };
17    print_cake_info (c2);
18}

Q3

Question

Write the make_cake function, which prompts the user for a name, color, diameter, and whether or not they want icing. The function then returns the cake.

Example cp_9_AC_3q
 1#include <iostream>
 2using namespace std;
 3
 4struct cake {
 5    string name;
 6    string color;
 7    double diameter;
 8    bool has_icing;
 9};
10
11void print_cake_info (cake c);
12
13// Write your code for the make_cake function here.
14
15int main() {
16    cake input = make_cake ();
17    print_cake_info (input);
18}

Answer

Below is one way to implement the program. We create a cake for the user, read in the user's input using cin, and return the cake.

Example cp_9_AC_3a
 1#include <iostream>
 2using namespace std;
 3
 4struct cake {
 5    string name;
 6    string color;
 7    double diameter;
 8    bool has_icing;
 9};
10
11void print_cake_info (cake c);
12
13cake make_cake () {
14    cake input;
15    string name, color;
16    double diameter;
17    char icing;
18    cout << "Name: ";
19    cin >> name;
20    input.name = name;
21    cout << "Color: ";
22    cin >> color;
23    input.color = color;
24    cout << "Diameter: ";
25    cin >> diameter;
26    input.diameter = diameter;
27    cout << "Icing? (y/n) ";
28    cin >> icing;
29    if (icing == 'y') {
30        input.has_icing = true;
31    }
32    else {
33        input.has_icing = false;
34    }
35    return input;
36}
37
38int main() {
39    cake input = make_cake ();
40    print_cake_info (input);
41}

Q4

Write the function change_cake_diameter, which takes a cake and a double as a parameter. change_cake_diameter then multiplies the original diameter by the double and modifies the cake to have this new diameter.

Example cp_9_AC_4q
 1#include <iostream>
 2using namespace std;
 3
 4struct cake {
 5    string name;
 6    string color;
 7    double diameter;
 8    bool has_icing;
 9};
10
11void print_cake_info (cake c);
12cake make_cake ();
13
14// Write your code for the change_cake_diameter function here.
15
16int main() {
17    cake original = { "John", "green", 8.5, true };
18    change_cake_diameter (original, 2);
19    print_cake_info (original);
20}

Q5

Question

Write the edit_cake function, which prompts the user for a new name, color, diameter, and whether or not they want icing. The function modifies the original cake that is passed in as a parameter. Use the make_cake function in your implementation to avoid duplicate code!

Example cp_9_AC_5q
 1#include <iostream>
 2using namespace std;
 3
 4struct cake {
 5    string name;
 6    string color;
 7    double diameter;
 8    bool has_icing;
 9};
10
11void print_cake_info (cake c);
12cake make_cake ();
13
14// Write your code for the edit_cake function here.
15
16int main() {
17    cake original = { "Oops", "orange", 185, true };
18    edit_cake (original);
19    print_cake_info (original);
20}

Answer

Below is one way to implement the program. We call make_cake in edit_cake and then set the original cake equal to the new one.

Example cp_9_AC_5a
 1#include <iostream>
 2using namespace std;
 3
 4struct cake {
 5    string name;
 6    string color;
 7    double diameter;
 8    bool has_icing;
 9};
10
11void print_cake_info (cake c);
12cake make_cake ();
13
14void edit_cake (cake& c) {
15    cake new_cake = make_cake ();
16    c = new_cake;
17}
18
19int main() {
20    cake original = { "Oops", "orange", 185, true };
21    edit_cake (original);
22    print_cake_info (original);
23}

Q6

Write the struct shirt, which has the instance variables color and size.

Example cp_9_AC_6q
1#include <iostream>
2using namespace std;
3
4// Write your code for the struct shirt here.
5
6int main () {
7    shirt t = { "blue", 'L' };
8}

Q7

Question

Write the pants structure, which has instance variables size and material.

Example cp_9_AC_7q
1#include <iostream>
2using namespace std;
3
4// Write your code for the struct pants here.
5
6int main() {
7    pants p = { 'S', "denim" };
8}

Answer

Below is one way to implement the program. We declare the pants struct and list the instance variables in order.

Example cp_9_AC_7a
 1#include <iostream>
 2using namespace std;
 3
 4struct pants {
 5    char size;
 6    string material;
 7};
 8
 9int main() {
10    pants p = { 'S', "denim" };
11}

Q8

Write the struct outfit, which is a nested structure that has a shirt, pants, and has_hat.

Example cp_9_AC_8q
 1#include <iostream>
 2using namespace std;
 3
 4// Write your code for the struct outfit here.
 5
 6int main () {
 7    shirt t = { "blue", 'L' };
 8    pants p = { 'S', "denim" };
 9    outfit o = { t, p, true };
10}

Q9

Question

Write the print_outfit function, which prints out details of the outfit. The output below should be "shirt: blue and L; pants: S and denim; has hat".

Example cp_9_AC_9q
 1#include <iostream>
 2using namespace std;
 3
 4struct shirt {
 5    string color;
 6    char size;
 7};
 8
 9struct pants {
10    char size;
11    string material;
12};
13
14struct outfit {
15    shirt s;
16    pants p;
17    bool has_hat;
18};
19
20// Write your code for the print_outfit function here.
21
22int main() {
23    shirt t = { "blue", 'L' };
24    pants p = { 'S', "denim" };
25    outfit o = { t, p, true };
26    print_outfit (o);
27}

Answer

Below is one way to implement the program.

Example cp_9_AC_9a
 1#include <iostream>
 2using namespace std;
 3
 4struct shirt {
 5    string color;
 6    char size;
 7};
 8
 9struct pants {
10    char size;
11    string material;
12};
13
14struct outfit {
15    shirt s;
16    pants p;
17    bool has_hat;
18};
19
20void print_outfit (outfit o) {
21// "shirt: blue and L; pants: S and denim; has hat"
22    cout << "shirt: " << o.s.color << " and " << o.s.size << "; pants:" << o.p.size << " and " << o.p.material << "; ";
23    if (o.has_hat) {
24        cout << "has hat" << '\n';
25    }
26    else {
27        cout << "does not have hat" << '\n';
28    }
29}
30
31int main() {
32    shirt t = { "blue", 'L' };
33    pants p = { 'S', "denim" };
34    outfit o = { t, p, true };
35    print_outfit (o);
36}

Q10

Write the change_shirts and change_pants functions, which both take an outfit as a parameter. change_shirts also takes a shirt as a parameter and change_pants also takes a pants as a parameter. Each function modifies the outfit and changes the shirt or pants to the new input.

If you did Q9, then feel free to copy the print_outfit function from there into this program.

Example cp_9_AC_10q
 1#include <iostream>
 2using namespace std;
 3
 4struct shirt {
 5    string color;
 6    char size;
 7};
 8
 9struct pants {
10    char size;
11    string material;
12};
13
14struct outfit {
15    shirt s;
16    pants p;
17    bool has_hat;
18};
19
20// Write your code for the change_shirts function here.
21
22// Write your code for the change_pants function here.
23
24int main() {
25    shirt t = { "blue", 'L' };
26    pants p = { 'S', "denim" };
27    outfit o = { t, p, true };
28    print_outfit (o);
29    shirt new_shirt = { "red", 'M' };
30    pants new_pants = { 'M', "khakis" };
31    change_shirts (o, new_shirt);
32    change_pants (o, new_pants);
33    print_outfit (o);
34}