8.15. Coding Practice

Q1

Question

Write the function rectangle_info which prompts the user for the width and height of a rectangle. Then rectangle_info prints out the area and perimeter of the rectangle.

Example cp_8_AC_1q
 1#include <iostream>
 2using namespace std;
 3
 4void rectangle_info () {
 5    // Write your implementation here.
 6}
 7
 8int main() {
 9    rectangle_info ();
10}

Answer

Below is one way to implement the program. We prompt the user for input using cin before printing the area and perimeter.

Example cp_8_AC_1a
 1#include <iostream>
 2
 3void rectangle_info () {
 4    int height, width;
 5    std::cout << "Please enter the height and width of a rectangle separated by spaces: ";
 6    std::cin >> height >> width;
 7    std::cout << "The area of the rectangle is " << height * width << '\n';
 8    std::cout << "The perimeter of the rectangle is " << 2 * (height + width) << '\n';
 9}
10
11int main() {
12    rectangle_info ();
13}

Q2

Write a simple function called greet_user which prompts the user for their full name. Then the function outputs "Hello full_name!".

Example cp_8_AC_2q
1#include <iostream>
2
3void greet_user () {
4    // Write your implementation here.
5}
6
7int main() {
8    greet_user ();
9}

Q3

Question

In the not so distant future, robots have replaced humans to do any kind of imaginable work or chore. Define the robot structure, which has instance variables string name, string model, int serial_number, int battery_level_percentage, and string task in that order. Then write the print_robot_data function, which takes a robot as a parameter and prints out the robot's data in the following format: name (model serial_number) has battery_level_percentage percent battery and is currently executing the task "task".

Example cp_8_AC_3q
 1#include <iostream>
 2
 3// Write your code for the struct robot here.
 4
 5// Write your code for the function print_robot_data here.
 6
 7int main() {
 8    robot rob = { "Rob", "XLV", 9800, 45, "washing dishes" };
 9    std::cout << "Your output:\n";
10    print_robot_data (rob);
11    std::cout << "Correct output:\n";
12    std::cout << "Rob (XLV 9800) has 45 percent battery and is currently executing the task \"washing dishes\"";
13}

Answer

Below is one way to implement the program. First we declare the instance variables in the struct definition. Next, we use dot notation to access the instance variables and output them using cout.

Example cp_8_AC_3a
 1#include <iostream>
 2using namespace std;
 3
 4struct robot {
 5    string name;
 6    string model;
 7    int serial_number;
 8    int battery_level_percentage;
 9    string task;
10};
11
12void print_robot_data (robot r) {
13     cout << r.name << " (" << r.model << ' ' << r.serial_number
14          << ") has " << r.battery_level_percentage
15          << " percent battery and is currently executing the task \""
16          << r.task << '"' << '\n';
17}
18
19int main() {
20    robot rob = { "Rob", "XLV", 9800, 45, "washing dishes" };
21    cout << "Your output:" << '\n';
22    print_robot_data (rob);
23    cout << "Correct output:" << '\n';
24    cout << "Rob (XLV 9800) has 45 percent battery and is currently executing the task \"washing dishes\"";
25}

Q4

Robots will naturally deplete their charge as they carry out tasks. Write a function called charge_robot which takes a robot as a parameter and charges the robot to 100 percent. Then output the statement "robot name is fully charged!".

Example cp_8_AC_4q
 1#include <iostream>
 2using namespace std;
 3
 4struct robot {
 5    string name;
 6    string model;
 7    int serial_number;
 8    int battery_level_percentage;
 9    string task;
10};
11
12void print_robot_data (robot r);
13
14// Write your code for the function charge_robot here.
15
16int main() {
17    robot bob = { "Bob", "MKZ", 143, 65, "sweeping floors" };
18    charge_robot (bob);
19    cout << "Your output:" << '\n';
20    print_robot_data (bob);
21    cout << "Correct output:" << '\n';
22    cout << "Bob (MKZ 143) has 100 percent battery and is currently executing the task \"sweeping floors\"";
23}

Q5

Question

In case a robot malfunctions, let's write the function reset_robot. reset_robot takes a robot as a parameter and resets its name to "EnterAName", recharges the battery to 100 percent, and resets the task to "Idle".

Example cp_8_AC_5q
 1#include <iostream>
 2using namespace std;
 3
 4struct robot {
 5    string name;
 6    string model;
 7    int serial_number;
 8    int battery_level_percentage;
 9    string task;
10};
11
12void print_robot_data (robot r);
13
14// Write your code for the function reset_robot here.
15
16int main() {
17    robot a = { "Bot", "RSO", 1985, 32, "gardening" };
18    reset_robot (a);
19    cout << "Your output:" << '\n';
20    print_robot_data (a);
21    cout << "Correct output:" << '\n';
22    cout << "EnterAName (RSO 1985) has 100 percent battery and is currently executing the task \"Idle\"";
23}

Answer

Below is one way to implement the program. We can create another robot with the settings after being reset. Then we set r equal to the new robot we created. Notice we use dot notation to ensure that the model and serial_number are the same.

Example cp_8_AC_5a
 1#include <iostream>
 2using namespace std;
 3
 4struct robot {
 5    string name;
 6    string model;
 7    int serial_number;
 8    int battery_level_percentage;
 9    string task;
10};
11
12void print_robot_data (robot r);
13
14void reset_robot(robot& r) {
15    robot reset = { "EnterAName", r.model, r.serial_number, 100, "Idle" };
16    r = reset;
17}
18
19int main() {
20    robot a = { "Bot", "RSO", 1985, 32, "gardening" };
21    reset_robot (a);
22    cout << "Your output:" << '\n';
23    print_robot_data (a);
24    cout << "Correct output:" << '\n';
25    cout << "EnterAName (RSO 1985) has 100 percent battery and is currently executing the task \"Idle\"";
26}

Q6

Write the pokemon structure, which has instance variables string poke_name, string type, int level, and int health_percentage in that order. Next, write the function print_poke_info, which takes a pokemon as a parameter and outputs the pokemon's info in the following format: poke_name (Lv. level, health_percentage% HP).

Example cp_8_AC_6q
 1#include <iostream>
 2using namespace std;
 3
 4// Write your code for the struct pokemon here.
 5
 6// Write your code for the function print_poke_info here.
 7
 8int main() {
 9    pokemon magikarp = { "Magikarp", "Water", 12, 100 };
10    cout << "Your output:" << '\n';
11    print_poke_info (magikarp);
12    cout << "Correct output:" << '\n';
13    cout << "Magikarp (Lv. 12, 100% HP)";
14}

Q7

Question

Now write the trainer structure, which has instance variables string trainer_name, char gender, int num_badges, and six pokemon objects named first, second, etc., in that order. Then, write the function print_trainer_info, which takes a trainer as a parameter and outputs the trainer's info. For example, the code below should print:

trainer Red has 8 badges and Red's team consists of
Pikachu (Lv. 81, 100% HP)
Espeon (Lv. 72, 100% HP)
Snorlax (Lv. 75, 100% HP)
Venusaur (Lv. 77, 100% HP)
Charizard (Lv. 77, 100% HP)
Blastoise (Lv. 77, 100% HP)
Example cp_8_AC_7q
 1#include <iostream>
 2using namespace std;
 3
 4struct pokemon {
 5    string poke_name;
 6    string type;
 7    int level;
 8    int health_percentage;
 9};
10
11// Write your code for the struct trainer here.
12
13void print_poke_info(pokemon p);
14
15// Write your code for the function print_trainer_info here.
16
17int main() {
18    pokemon pikachu = { "Pikachu", "Electric", 81, 100 };
19    pokemon espeon = { "Espeon", "Psychic", 72, 100 };
20    pokemon snorlax = { "Snorlax", "Normal", 75, 100 };
21    pokemon venusaur = { "Venusaur", "Grass & Poison", 77, 100 };
22    pokemon charizard = { "Charizard", "Fire & Flying", 77, 100 };
23    pokemon blastoise = { "Blastoise", "Water", 77, 100 };
24    trainer red = { "Red", 'M', 8, pikachu, espeon, snorlax, venusaur, charizard, blastoise };
25    print_trainer_info (red);
26}

Answer

Below is one way to implement the program. First we declare the instance variables in the struct definition. Next, we call print_poke_info on each pokemon in trainer and output the trainer's info in the correct format.

Example cp_8_AC_7a
 1#include <iostream>
 2using namespace std;
 3
 4struct pokemon {
 5    string poke_name;
 6    string type;
 7    int level;
 8    int health_percentage;
 9};
10
11struct trainer {
12    string trainer_name;
13    char gender;
14    int num_badges;
15    pokemon first, second, third, fourth, fifth, sixth;
16};
17
18void print_poke_info(pokemon p);
19
20void print_trainer_info(trainer t) {
21    cout << "trainer " << t.trainer_name << " has " << t.num_badges
22         << " badges and " << t.trainer_name << "'s team consists of " << '\n';
23    print_poke_info(t.first);
24    print_poke_info(t.second);
25    print_poke_info(t.third);
26    print_poke_info(t.fourth);
27    print_poke_info(t.fifth);
28    print_poke_info(t.sixth);
29}
30
31int main() {
32    pokemon pikachu = { "Pikachu", "Electric", 81, 100 };
33    pokemon espeon = { "Espeon", "Psychic", 72, 100 };
34    pokemon snorlax = { "Snorlax", "Normal", 75, 100 };
35    pokemon venusaur = { "Venusaur", "Grass & Poison", 77, 100 };
36    pokemon charizard = { "Charizard", "Fire & Flying", 77, 100 };
37    pokemon blastoise = { "Blastoise", "Water", 77, 100 };
38    trainer red = { "Red", 'M', 8, pikachu, espeon, snorlax, venusaur, charizard, blastoise };
39    print_trainer_info (red);
40}

Q8

When pokemon are injured, they can be healed up at the pokemon Center. Write the function heal_pokemon, which takes a trainer as a parameter and heals the trainer's pokemon to 100 percent health.

Example cp_8_AC_8q
 1#include <iostream>
 2using namespace std;
 3
 4struct pokemon {
 5    string poke_name;
 6    string type;
 7    int level;
 8    int health_percentage;
 9};
10
11struct trainer {
12    string trainer_name;
13    char gender;
14    int num_badges;
15    pokemon first, second, third, fourth, fifth, sixth;
16};
17
18void print_poke_info(pokemon p);
19void print_trainer_info(trainer t);
20
21// Write your code for the function heal_pokemon here.
22
23int main() {
24    pokemon exeggutor = {"Exeggutor", "Grass & Psychic", 58, 78};
25    pokemon alakazam = {"Alakazam", "Psychic", 54, 0};
26    pokemon arcanine = {"Arcanine", "Fire", 58, 24};
27    pokemon rhydon = {"Rhydon", "Ground & Rock", 56, 55};
28    pokemon gyarados = {"Gyarados", "Water & Flying", 58, 100};
29    pokemon pidgeot = {"Pidgeot", "Normal & Flying", 56, 35};
30    trainer blue = {"blue", 'M', 8, exeggutor, alakazam, arcanine, rhydon, gyarados, pidgeot};
31    print_trainer_info(blue);
32    heal_pokemon(blue);
33    print_trainer_info(blue);  // pokemon should now all be healed to 100% health
34}

Q9

Question

Now write the function poke_center which takes a trainer as a parameter and prompts the user if they'd like to heal their pokemon. Below are the possible outputs (y, n, or an invalid input). If user inputs 'y', call heal_pokemon and output the correct dialogue. If user inputs 'n', don't call heal_pokemon and output the correct dialogue. If user inputs an invalid character, output the error message.

Welcome to the Pokémon Center. Would you like me to take your Pokémon? (y/n) y
Okay, I'll take your Pokémon for a few seconds.
Your Pokémon are now healed. We hope to see you again.

or

Welcome to the Pokémon Center. Would you like me to take your Pokémon? (y/n) n
We hope to see you again.

or

Welcome to the Pokémon Center. Would you like me to take your Pokémon? (y/n) h
Sorry, not a valid input.
Example cp_8_AC_9q
 1#include <iostream>
 2using namespace std;
 3
 4struct pokemon {
 5    string poke_name;
 6    string type;
 7    int level;
 8    int health_percentage;
 9};
10
11struct trainer {
12    string trainer_name;
13    char gender;
14    int num_badges;
15    pokemon first, second, third, fourth, fifth, sixth;
16};
17
18void print_poke_info(pokemon p);
19void print_trainer_info(trainer t);
20void heal_pokemon(trainer& t);
21
22// Write your code for the function poke_center here.
23
24int main() {
25    pokemon exeggutor = {"Exeggutor", "Grass & Psychic", 58, 78};
26    pokemon alakazam = {"Alakazam", "Psychic", 54, 0};
27    pokemon arcanine = {"Arcanine", "Fire", 58, 24};
28    pokemon rhydon = {"Rhydon", "Ground & Rock", 56, 55};
29    pokemon gyarados = {"Gyarados", "Water & Flying", 58, 100};
30    pokemon pidgeot = {"Pidgeot", "Normal & Flying", 56, 35};
31    trainer blue = {"blue", 'M', 8, exeggutor, alakazam, arcanine, rhydon, gyarados, pidgeot};
32    print_trainer_info(blue);
33    poke_center(blue);
34    print_trainer_info(blue);  // pokemon should now all be healed to 100% health
35}

Answer

Below is one way to implement the program. We use conditionals to perform the correct output and operation depending on the user's input.

Example cp_8_AC_9a
 1#include <iostream>
 2using namespace std;
 3
 4struct pokemon {
 5    string poke_name;
 6    string type;
 7    int level;
 8    int health_percentage;
 9};
10
11struct trainer {
12    string trainer_name;
13    char gender;
14    int num_badges;
15    pokemon first, second, third, fourth, fifth, sixth;
16};
17
18void print_poke_info(pokemon p);
19void print_trainer_info(trainer t);
20void heal_pokemon(trainer& t);
21
22void poke_center(trainer& t) {
23    char response;
24    cout << "Welcome to the Pokémon Center. Would you like me to take your Pokémon? (y/n) ";
25    cin >> response;
26    if (response == 'y') {
27        cout << "Okay, I'll take your Pokémon for a few seconds." << '\n';
28        heal_pokemon(t);
29        cout << "Your Pokémon are now healed. We hope to see you again." << '\n';
30    }
31    else if (response == 'n') {
32        cout << "We hope to see you again." << '\n';
33    }
34    else {
35        cout << "Sorry, not a valid input." << '\n';
36    }
37}
38
39int main() {
40    pokemon exeggutor = {"Exeggutor", "Grass & Psychic", 58, 78};
41    pokemon alakazam = {"Alakazam", "Psychic", 54, 0};
42    pokemon arcanine = {"Arcanine", "Fire", 58, 24};
43    pokemon rhydon = {"Rhydon", "Ground & Rock", 56, 55};
44    pokemon gyarados = {"Gyarados", "Water & Flying", 58, 100};
45    pokemon pidgeot = {"Pidgeot", "Normal & Flying", 56, 35};
46    trainer blue = {"blue", 'M', 8, exeggutor, alakazam, arcanine, rhydon, gyarados, pidgeot};
47    print_trainer_info(blue);
48    poke_center(blue);
49    print_trainer_info(blue);  // pokemon should now all be healed to 100% health
50}

Q10

Ever wanted to know how much you'd weigh on each planet? Write the convert_weight function, which takes a double earth_weight and int planet as parameters. First, in main, prompt the user to enter their weight in pounds and a number corresponding to a planet (Mercury is 1, Venus is 2, etc.). Next, call the convert_weight function using the user's input. Finally, print out their weight on that planet. If the user inputs an invalid planet, print out an error message. The weight conversion are as follows (multiply the number by earth_weight to get the weight on that planet): Mercury - 0.38, Venus - 0.91, Earth - 1.00, Mars - 0.38, Jupiter - 2.34, Saturn - 1.06, Uranus - 0.92, and Neptune - 1.19. Below are some examples.

Please enter your weight in pounds: 145.6
Please select a planet: 3
Your weight on Earth is 145.6 pounds.

or

Please enter your weight in pounds: 170
Please select a planet: 1
Your weight on Mercury is 64.6 pounds.

or

Please enter your weight in pounds: 170
Please select a planet: 23
Error, not a valid planet.
Example cp_8_AC_10q
1#include <iostream>
2using namespace std;
3
4// Write your code for the function convert_weight here.
5
6int main() {
7    // Write your implementation here.
8}