8.15. Coding Practice

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

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

Loading a dynamic question ...
Selecting from: cp_8_AC_2q, cp_8_AC_2q_pp

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 serialNumber, int batteryLevelPercentage, and string task in that order. Then write the printRobotData function, which takes a Robot as a parameter and prints out the robot’s data in the following format: name (model serialNumber) has batteryLevelPercentage percent battery and is currently executing the task “task”.

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.

Loading a dynamic question ...
Selecting from: cp_8_AC_4q, cp_8_AC_4q_pp

In case a robot malfunctions, let’s write the function resetRobot. resetRobot takes a Robot as a parameter and resets its name to “EnterAName”, recharges the battery to 100 percent, and resets the task to “Idle”.

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 serialNumber are the same.

Loading a dynamic question ...
Selecting from: cp_8_AC_6q, cp_8_AC_6q_pp

Now write the Trainer structure, which has instance variables string trainerName, char gender, int numBadges, and six Pokemon objects named first, second, etc., in that order. Then, write the function printTrainerInfo, 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)

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

Loading a dynamic question ...
Selecting from: cp_8_AC_8q, cp_8_AC_8q_pp

Now write the function pokeCenter 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 healPokemon and output the correct dialogue. If user inputs ‘n’, don’t call healPokemon 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.

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

Loading a dynamic question ...
Selecting from: cp_8_AC_10q, cp_8_AC_10q_pp

You have attempted of activities on this page