Skip to content

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:

x4+3y312x2+z13\frac{x^4 + 3y^3 - 1}{2x^2 + z - 13}

If you compute 12\frac{1}{2} 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:

x+33÷y\sqrt[3]{\sqrt{x} + 3} \div y

y3\sqrt[3]{y} can be written as y1/3y^{1/3} using pow(y, 1.0 / 3). Similarly, x\sqrt{x} can be expressed as x1/2x^{1/2} using pow(x, 1.0 / 2).

Problem 3

Convert the following mathematical expression into a C++ expression:

ln(x2)\lfloor \ln(x^2) \rfloor

\lfloor \cdot \rfloor 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 ln\ln , use the log function. For instance, log(5) in C++ is equivalent to ln(5)\ln(5) .

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 the double 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:

a=5.3b=ac=b1371\begin{align*} a &= 5.3 \\ b &= \lceil a \rceil \\ c &= \frac{b}{1371} \end{align*}

Problem 2

Convert the following mathematical equations into a series of C++ variable initializations:

x=7y=x12+5z=xy\begin{align*} x &= 7 \\ y &= x^{12} + 5 \\ z &= x \cdot y \end{align*}

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:

a=5.3b=12c=abd=loga(b)+c\begin{align*} a &= 5.3 \\ b &= 12 \\ c &= \sqrt[b]{a} \\ d &= \log_a(b) + c \end{align*}

Use the change of base formula for logarithms:

loga(b)=ln(b)ln(a)\log_a(b) = \frac{\ln(b)}{\ln(a)}

You can use any base, but this uses ee (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:

ax2+bx+c=0ax^2 + bx + c = 0

where aa , bb , and cc are known constants, and xx 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 xx , you can use the quadratic formula, which is guaranteed to work (provided a real-valued solution exists):

x=b±d2ax = \frac{-b \pm \sqrt{d}}{2a}

where dd is the discriminant:

d=b24acd = b^2 - 4ac

The ±± symbol in the quadratic formula indicates there are two possible values of xx for any given quadratic equation (unless d=0d = 0 , in which case the two solutions are the same).

  • Using the ++ sign gives one solution for xx .
  • Using the - sign gives the other solution.

Creating a Quadratic Solver

Create a program that does the following:

  1. Prompt the user to input values for aa , bb , and cc using std::cout, and receive the values via std::cin.
  2. Compute the discriminant d=b24acd = b^2 - 4ac and print its value using std::cout.
  3. Use the quadratic formula to compute the two possible values of xx , 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 dtd_t between the garage and your car after tt seconds can be expressed as:

dt=d0+v0t+12at2d_t = d_0 + v_0t + \frac{1}{2}at^2

where:

  • d0d_0 is the initial distance from the garage (20 meters),
  • v0v_0 is the initial speed (2.2 meters per second),
  • aa is the acceleration (10 meters per second squared),
  • tt is the time in seconds.

By substituting the known values (d0=20d_0 = 20 , v0=2.2v_0 = 2.2 , a=10a = 10 ) into the equation, we get:

dt=20+2.2t+5t2d_t = 20 + 2.2t + 5t^2

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 aa , bb , and cc .

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 dt=1000d_t = 1000 and rearrange the equation, it looks like this: 5t2+2.2t980=05t^2 + 2.2t - 980 = 0 . Looking at this equation, what are the appropriate values of aa , bb , and cc with respect to the quadratic formula? Don’t get hung up on the fact that our variable is called tt instead of xx (names are just names).