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.
Write a simple function called greetUser
which prompts the user
for their full name. Then the function outputs “Hello fullName
!”.
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
.
Robots will naturally deplete their charge as they carry out tasks.
Write a function called chargeRobot
which takes a Robot
as
a parameter and charges the robot to 100 percent. Then output the statement
“Robot name
is fully charged!”.
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.
Write the Pokemon
structure, which has instance variables string pokeName
,
string type
, int level
, and int healthPercentage
in that order.
Next, write the function printPokeInfo
, which takes a Pokemon
as a parameter and outputs the
Pokemon’s info in the following format: pokeName
(Lv. level
, healthPercentage
% HP).
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.
When Pokemon are injured, they can be healed up at the Pokemon Center.
Write the function healPokemon
, which takes a Trainer
as a parameter
and heals the Trainer’s Pokemon to 100 percent health.
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.
Ever wanted to know how much you’d weigh on each planet? Write the convertWeight
function, which takes a double earthWeight
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 convertWeight
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 earthWeight
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.