Podcast
Questions and Answers
Which function would you use to calculate the cosine of an angle given in radians?
Which function would you use to calculate the cosine of an angle given in radians?
What does the function log10(x) return?
What does the function log10(x) return?
Which of the following functions is used to obtain the angle in radians for the inverse of cosine?
Which of the following functions is used to obtain the angle in radians for the inverse of cosine?
If you need to round down a floating-point number x, which function should you use?
If you need to round down a floating-point number x, which function should you use?
Signup and view all the answers
What is the result of calling sin(PI / 6)?
What is the result of calling sin(PI / 6)?
Signup and view all the answers
Which function should be used for calculating e raised to the power of x?
Which function should be used for calculating e raised to the power of x?
Signup and view all the answers
For which of the following numeric inputs is the function sqrt(x) defined?
For which of the following numeric inputs is the function sqrt(x) defined?
Signup and view all the answers
What does the function atan(a) compute?
What does the function atan(a) compute?
Signup and view all the answers
Study Notes
Mathematical Functions - Trigonometric
- Trigonometric functions (sin, cos, tan, asin, acos, atan) operate on angles in radians.
- These functions are not required to be memorized, but students should know they exist and how to use them (online search).
Mathematical Functions - Exponent
- All functions from page 3 to 5 are from the cmath header library.
- exp(x): Returns e raised to the power of x (ex).
- log(x): Returns the natural logarithm of x (ln(x)).
- log10(x): Returns the base 10 logarithm of x (log10(x)).
- sqrt(x): Returns the square root of x (√x), where x ≥ 0.
- ceil(x): Rounds x up to the nearest integer (returned as a double).
- floor(x): Rounds x down to the nearest integer (returned as a double).
- pow(a, b): Returns a raised to the power of b (ab).
Mathematical Functions - min, max, abs
- Functions min(x, y), max(x, y), and abs(x) are from the cstdlib header library.
- min(x, y): Returns the minimum of x and y (x and y should be the same type).
- max(x, y): Returns the maximum of x and y (x and y should be the same type).
- abs(x): Returns the absolute value of x.
Character Data Type and Operations
- char stores a single character.
- Character literals are enclosed in single quotes (e.g., '9', 'Z').
- ASCII (American Standard Code for Information Interchange) represents characters in 1 byte.
Escape Sequence
- Some characters cannot be directly typed or have special meanings in C++.
- Escape sequences (e.g., \b, \t, \n, \f, \r, \, ', ") represent these characters.
- A backslash () followed by a character or combination of digits is interpreted as a single character.
Numeric Operators on Characters
- char is treated as an integer (byte size).
- Numeric operators can be applied to char operands.
- char operands are automatically converted to numbers if the other operand is a number.
Casting Numeric Types into Chars
- Positive integers in hex range 0x00 to 0xFF can be implicitly cast to char.
- For larger integers, only the last byte is used.
- Floating-point types are explicitly cast into char.
- C++ first converts them to integers.
String Type
- A string is a sequence of characters.
- Functions length() and size() return the number of characters in a string.
- at(index) returns the character at the specified index in a string.
- The subscript operator [] can be used to access and modify characters in a string.
- Functions like append, substr, and others, can be used to manipulate strings more effectively.
String Operators
- Strings can be concatenated using the + operator.
- Relational operators (==, !=, >, >=, <, <=) compare strings character-by-character.
Reading Strings
- Strings can be read from the console using cin.
- getline(cin, var) reads an entire line of input into a string variable.
Example - Generate a Random Number
- The rand() function generates pseudorandom positive integers.
- The rand() % (max - min + 1) + min; formula generates random numbers between specified limits.
-
srand(time(0));
initializes the random number generator with the current time.
Example - Generate a Random Character
- Every character has a unique ASCII code value between 0 and 127.
- Generate a random integer between 0 and 127, cast to char.
Checkpoint - Character Functions
-
isdigit(ch)
returns non-zero if the character is a digit (0–9). -
isalpha(ch)
: returns non-zero if it's a letter. -
isalnum(ch)
: returns non-zero if it's a letter or digit -
islower(ch)
: returns non-zero if it's a lowercase letter. -
isupper(ch)
: returns non-zero if it's an uppercase letter. -
isspace(ch)
: returns non-zero if it's a whitespace character. -
tolower(ch)
: Converts a character to lowercase. -
toupper(ch)
: Converts a character to uppercase.
Reading and Writing to a File
- Objects (for File I/O) are defined using the
#include <fstream>
header file. -
ofstream
objects write to files,ifstream
objects read from files -
open()
function initializes these objects with a file. - The
>>
(extraction) and<<
(insertion) operators are used with files - Close the file (using
.close()
) when finished to avoid issues if not closed, or in larger programs. - Files are stored on a disk and can be accessed later.
Tips when reading from files.
- Check if the file exists before trying to read from it.
- If there is no data when reading from the file, the variables' values will not be updated.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge of fundamental mathematical functions including trigonometric functions, exponent functions, and min/max/abs calculations. This quiz will help you understand the application and usage of these functions in various contexts.