Recap - Simple Math
Part of the Fundamentals section of Coddy's R journey — lesson 15 of 78.
Challenge
EasyCalculate various statistics about a road trip using all the arithmetic operators you've learned.
You are provided with the following variables:
total_distance <- 847
fuel_used <- 42
hours_driven <- 13
rest_stops <- 4Perform the following calculations:
- Create a variable called
fuel_efficiencythat stores the distance traveled per unit of fuel (total distance divided by fuel used) - Create a variable called
avg_speedthat stores the average speed (total distance divided by hours driven) - Create a variable called
hours_per_stopthat stores how many complete hours passed between each rest stop (use integer division of hours driven by rest stops) - Create a variable called
extra_minutesthat stores the remainder when dividing hours driven by rest stops, then multiply it by 60 to convert to minutes - Create a variable called
distance_squaredthat stores the total distance raised to the power of 2
Use the print() function to display fuel_efficiency, avg_speed, hours_per_stop, extra_minutes, and distance_squared in that exact order.
Try it yourself
# Given variables for the road trip
total_distance <- 847
fuel_used <- 42
hours_driven <- 13
rest_stops <- 4
# TODO: Write your code below
# 1. Calculate fuel_efficiency (total_distance / fuel_used)
# 2. Calculate avg_speed (total_distance / hours_driven)
# 3. Calculate hours_per_stop (use integer division %/%)
# 4. Calculate extra_minutes (use modulo %% then multiply by 60)
# 5. Calculate distance_squared (use exponentiation ^)
# Print the results in order
print(fuel_efficiency)
print(avg_speed)
print(hours_per_stop)
print(extra_minutes)
print(distance_squared)All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values