Expressions, Operators, and Variables
Expressions and Operators
Below are example problems demonstrating how to convert mathematical expressions into C++ expressions. Note that there may be multiple valid solutions for each problem. Make sure you understand the examples before attempting the remaining three problems.
You may write your solutions on paper or in any text document.
Review the provided examples carefully and ensure you account for operator precedence and truncation issues when writing your solutions. Use parentheses as needed for clarity and correctness.
Problem 1
Convert the following mathematical expression into a C++ expression:
If you compute directly, ensure you avoid truncation by using 1.0 / 2
instead of 1 / 2
.
Problem 2
Convert the following mathematical expression into a C++ expression:
can be written as using pow(y, 1.0 / 3)
. Similarly, can be expressed as using pow(x, 1.0 / 2)
.
Problem 3
Convert the following mathematical expression into a C++ expression:
is the floor operator, which rounds a value down to the nearest integer. If the input is already an integer, the output will remain the same. The cmath
library provides the floor function for this purpose. For example, floor(x / 2)
divides x
by 2
and rounds the result down. Similarly, the ceil
function in cmath
performs the ceiling operation (rounding up to the nearest integer).
To compute the natural logarithm , use the log
function. For instance, log(5)
in C++ is equivalent to .
Variables
A variable represents a fixed location in memory where data of a fixed, specified type is stored. Below are example problems involving the conversion of mathematical equations into corresponding C++ variable initializations. There may be more than one valid solution for each problem. Ensure you understand the examples before attempting the provided problems.
Assumptions:
- The
cmath
library is already included. - Use the
int
type for variables that are guaranteed to hold integer values. Use thedouble
type for all other variables.
You may write your solutions on paper or in any text document.
When writing your solutions, pay attention to operator precedence and type conversions to avoid unintended truncation or type mismatches. Use parentheses to clarify operations when necessary.
Problem 1
Convert the following mathematical equations into a series of C++ variable initializations:
Problem 2
Convert the following mathematical equations into a series of C++ variable initializations:
Raising one integer to the power of another integer results in an integer. Similarly, the product of two integers is still an integer.
Problem 3
Convert the following mathematical equations into a series of C++ variable initializations:
Use the change of base formula for logarithms:
You can use any base, but this uses (natural logarithm) for simplicity. This makes the code straightforward to implement.
Quadratics
A quadratic equation is an equation that can be written in the following form:
where , , and are known constants, and is the variable.
Quadratic equations frequently appear in real-world applications. For example:
- The position of an object undergoing constant acceleration is a quadratic function of time.
- The surface areas of various 2D and 3D shapes can be expressed as quadratic functions of side lengths.
To solve a quadratic equation for , you can use the quadratic formula, which is guaranteed to work (provided a real-valued solution exists):
where is the discriminant:
The symbol in the quadratic formula indicates there are two possible values of for any given quadratic equation (unless , in which case the two solutions are the same).
- Using the sign gives one solution for .
- Using the sign gives the other solution.
Creating a Quadratic Solver
Create a program that does the following:
- Prompt the user to input values for , , and using
std::cout
, and receive the values viastd::cin
. - Compute the discriminant and print its value using
std::cout
. - Use the quadratic formula to compute the two possible values of , and print both results using
std::cout
.
Using Your Program to Solve a Problem
Now, let’s test your program with a real-world example.
Scenario
Your car accelerates at a constant rate of 10 meters per second squared when you press the gas pedal. Assume there’s no maximum speed (the car will keep accelerating as long as the gas pedal is pressed).
When you start the stopwatch:
- Your car is already moving at 2.2 meters per second.
- Your car is 20 meters away from the garage.
The distance between the garage and your car after seconds can be expressed as:
where:
- is the initial distance from the garage (20 meters),
- is the initial speed (2.2 meters per second),
- is the acceleration (10 meters per second squared),
- is the time in seconds.
By substituting the known values ( , , ) into the equation, we get:
Question
How many seconds will it take for the car to reach a distance of 1 kilometer (1,000 meters) from the garage? Use your program to solve this problem by plugging in the appropriate values for , , and .
Keep in mind that your program will calculate two possible solutions for t, but one of them will be negative. Since negative time does not make sense in this context, only the positive solution is relevant.
If you substitute and rearrange the equation, it looks like this: . Looking at this equation, what are the appropriate values of , , and with respect to the quadratic formula? Don’t get hung up on the fact that our variable is called instead of (names are just names).