Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Exercises 9.15 Coding Practice

1.

Write the Cake structure, which has instance variables name, color, diameter, and has_icing.
Solution.
Below is one way to implement the program. We declare the Cake struct and list the instance variables in order.
#include <iostream>
using namespace std;

struct Cake {
    string name;
    string color;
    double diameter;
    bool has_icing;
};

int main() {
    Cake c = { "Mary", "blue", 3.5, false };
}

2.

Write the function printCakeInfo, 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”, printCakeInfo prints out “Happy birthday name! Your cake is color, has a diameter inch diameter, and comes with/without icing.” Check the hint below for help with the construction of the code.
Hint.

Activity 9.15.1.

Write the function printCakeInfo, 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”, printCakeInfo prints out “Happy birthday name! Your cake is color, has a diameter inch diameter, and comes with/without icing.” Use the lines to construct the code, then go back to complete the Activecode.

3.

Write the makeCake function, which prompts the user for a name, color, diameter, and whether or not they want icing. The function then returns the cake.
Solution.
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.
#include <iostream>
using namespace std;

struct Cake {
    string name;
    string color;
    double diameter;
    bool has_icing;
};

void printCakeInfo(Cake c);

Cake makeCake() {
    Cake input;
    string name, color;
    double diameter;
    char icing;
    cout << "Name: ";
    cin >> name;
    input.name = name;
    cout << "Color: ";
    cin >> color;
    input.color = color;
    cout << "Diameter: ";
    cin >> diameter;
    input.diameter = diameter;
    cout << "Icing? (y/n) ";
    cin >> icing;
    if (icing == 'y') {
        input.has_icing = true;
    }
    else {
        input.has_icing = false;
    }
    return input;
}

int main() {
    Cake input = makeCake();
    printCakeInfo(input);
}

4.

Write the function changeCakeDiameter, which takes a Cake and a double as a parameter. changeCakeDiameter then multiplies the original diameter by the double and modifies the cake to have this new diameter. Check the hint below for help with the construction of the code.
Hint.

Activity 9.15.2.

Write the function changeCakeDiameter, which takes a Cake and a double as a parameter. changeCakeDiameter then multiplies the original diameter by the double and modifies the cake to have this new diameter. Use the lines to construct the code, then go back to complete the Activecode.

5.

Write the editCake 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 makeCake function in your implementation to avoid duplicate code!
Solution.
Below is one way to implement the program. We call makeCake in editCake and then set the original cake equal to the new one.
#include <iostream>
using namespace std;

struct Cake {
    string name;
    string color;
    double diameter;
    bool has_icing;
};

void printCakeInfo(Cake c);
Cake makeCake();

void editCake(Cake& c) {
    Cake newCake = makeCake();
    c = newCake;
}

int main() {
    Cake original = { "Oops", "orange", 185, true };
    editCake(original);
    printCakeInfo(original);
}

6.

Write the Pants structure, which has instance variables size and material.
Solution.
Below is one way to implement the program. We declare the Pants struct and list the instance variables in order.
#include <iostream>
using namespace std;

struct Pants {
    char size;
    string material;
};

int main() {
    Pants p = { 'S', "denim" };
}

7.

Write the struct Shirt, which has the instance variables color and size (in that order). Check the hint below for help with the construction of the code.
Hint.

Activity 9.15.3.

Write the struct Shirt, which has the instance variables color and size (in that order). Use the lines to construct the code, then go back to complete the Activecode.

8.

Write the struct Outfit, which is a nested structure that has a Shirt, Pants, and has_hat (in that order). Check the hint below for help with the construction of the code.
Hint.

Activity 9.15.4.

Write the struct Outfit, which is a nested structure that has a Shirt, Pants, and has_hat (in that order). Use the lines to construct the code, then go back to complete the Activecode.

9.

Write the printOutfit function, which prints out details of the outfit. The output below should be “Shirt: blue and L; Pants: S and denim; has hat”.
Solution.
Below is one way to implement the program. We declare the Cake struct and list the instance variables in order.
#include <iostream>
using namespace std;

struct Shirt {
    string color;
    char size;
};

struct Pants {
    char size;
    string material;
};

struct Outfit {
    Shirt s;
    Pants p;
    bool has_hat;
}

void printOutfit(Outfit o) {
"Shirt: blue and L; Pants: S and denim; has hat"
    cout << "Shirt: " << o.s.color << " and " << o.s.size << "; Pants:" << o.p.size << " and " << o.p.material << "; ";
    if (o.has_hat) {
        cout << "has hat" << endl;
    }
    else {
        cout << "does not have hat" << endl;
    }
}

int main() {
    Shirt t = { "blue", 'L' };
    Pants p = { 'S', "denim" };
    Outfit o = { t, p, true };
    printOutfit(o);
}

10.

Write the changeShirts and changePants functions, which both take an Outfit as a parameter. changeShirts also takes a Shirt as a parameter and changePants also takes a Pants as a parameter. Each function modifies the Outfit and changes the shirt or pants to the new input. Check the hint below for help with the construction of the code.
Hint.

Activity 9.15.5.

Write the changeShirts and changePants functions, which both take an Outfit as a parameter. changeShirts also takes a Shirt as a parameter and changePants also takes a Pants as a parameter. Each function modifies the Outfit and changes the shirt or pants to the new input. Use the lines to construct the code, then go back to complete the Activecode.
You have attempted 1 of 16 activities on this page.