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
- 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.
- 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 counterclockwise around the origin by 90 degrees. The program should print the and coordinates of the newly rotated point. If your implementation is correct, the output should be .
Mathematical Equations
To rotate a point counterclockwise around the origin by radians, use the following formulas to compute the new point :
To convert degrees to radians , use the equation:
Function Parameters
Each function should take the values , , and (degrees) as inputs:
- The first function computes and returns
- The second function computes and returns
Hints
- The
<cmath>
header provides trigonometric functions likesin
andcos
, 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:
- Prompts the user to input a whole number between 1 and 10.
- Stores the user input in an
int
variable. - Multiplies the stored number by 1.5, preserving decimal places (ie., no truncation).
- 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:
- Prompted the user to input values for , , and for a quadratic equation in the form .
- Used the quadratic formula to calculate the discriminant and the roots of the equation.
- 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:
- Break down your program into several smaller, well-designed functions.
- Follow the course style guidelines, ensuring your code remains clean and consistent.
- 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