Skip to content

Functions

Functions are reusable blocks of code that interact with other functions by accepting one or more inputs (parameters) and producing zero or one outputs (return values). Below are examples of basic function problems in C++. Review them carefully before attempting the remaining problems.

Problem 1

  1. Implement a function that rotates a Cartesian point counterclockwise around the origin by a given number of degrees and returns the rotated point’s x-coordinate.
  2. Implement a second function that performs the same operation but returns the rotated point’s y-coordinate.

Write a program that uses these functions to rotate the point (x=1,y=2)(x = 1, y = 2) counterclockwise around the origin by 90 degrees. The program should print the xx and yy coordinates of the newly rotated point. If your implementation is correct, the output should be (x=2,y=1)(x = -2, y = 1) .

Mathematical Equations

To rotate a point (x1,y1)(x_1, y_1) counterclockwise around the origin by θ\theta radians, use the following formulas to compute the new point (x2,y2)(x_2, y_2) :

x2=cos(θ)x1sin(θ)y1y2=sin(θ)x1+cos(θ)y1\begin{align*}x_2 &= \cos(\theta) \cdot x_1 - \sin(\theta) \cdot y_1 \\ y_2 &= \sin(\theta) \cdot x_1 + \cos(\theta) \cdot y_1\end{align*}

To convert degrees dd to radians θ\theta , use the equation:

θ=dπ180\theta = \frac{d \cdot \pi}{180}

Function Parameters

Each function should take the values x1x_1 , y1y_1 , and dd (degrees) as inputs:

  • The first function computes and returns x2x_2
  • The second function computes and returns y2y_2

Hints

  • The <cmath> header provides trigonometric functions like sin and cos, which take a single argument in radians and return the corresponding trigonometric value.
  • Follow the course’s style guidelines, ensuring adherence to the Single Responsibility Principle (SRP) and Don’t Repeat Yourself (DRY) principles.
  • Since both functions involve some shared computations (e.g., converting degrees to radians), extract common logic into a helper function. You should end up with more than just the two required functions.
  • Include detailed block comments for all functions.

Problem 2

Write a program that:

  1. Prompts the user to input a whole number between 1 and 10.
  2. Stores the user input in an int variable.
  3. Multiplies the stored number by 1.5, preserving decimal places (ie., no truncation).
  4. Prints the result, formatted as:
Your number multiplied by 1.5 is: <RESULT_HERE>

Hints

  • Follow the course’s style guidelines.
  • Avoid writing a single function that both interacts with the terminal and performs mathematical calculations. Instead, separate tasks into distinct functions to improve clarity and reusability.
  • For printing the final result, consider using a function with no return value. Such functions can be created by specifying the return type as void.
  • Include detailed block comments for all functions.

Quadratics Revisited

In the previous studio, you wrote a program that:

  1. Prompted the user to input values for aa , bb , and cc for a quadratic equation in the form ax2+bx+c=0ax^2 + bx + c = 0 .
  2. Used the quadratic formula to calculate the discriminant and the roots of the equation.
  3. Applied the program to solve for the time variable in a uniform motion problem.

In this studio, you’ll build on that work by refactoring your program into smaller, well-structured functions.

Start by copying your code from the last studio into a new file. Use the cp command for this purpose.

Refactoring involves reorganizing your code to improve readability, modularity, and maintainability without altering its behavior. Specifically, you’ll:

  1. Break down your program into several smaller, well-designed functions.
  2. Follow the course style guidelines, ensuring your code remains clean and consistent.
  3. Provide detailed function block comments for each function to describe its purpose, parameters, and return values.

Your program should still perform exactly as it did previously. Verify this by solving the same uniform motion problem as in the previous studio and confirming that the output matches.

Remember that if , the quadratic equation has two real roots. Hence, you’ll need two functions, one for each root. While it’s possible to return multiple values in C++ by packing them into a single object, this concept has not yet been covered in the course. Stick to separate functions for now.