Week 3 Review Variables and Assignments Continued
Week 3 Review Variables and Assignments Continued
● The modulo operator (%) is a binary arithmetic operation that returns the remainder of a
division between two numbers. The modulo operator offers a means to perform modular
division, efficiently determining the remainder of an integer division. It's a versatile tool
with applications ranging from basic arithmetic operations to advanced programming
patterns and logic.
● Modulo is frequently used to determine the remainder when one integer is divided by
another.
● The modulo operator can be used to check if a number is even or odd. IE. if
someNumber % 2 == 0 then the number is even, otherwise odd.
● Modulo is useful in random number generation where you need to confine numbers to a
specific range.
● Using a divisor of 0 will lead to undefined behavior. Always ensure the divisor is non-
zero before performing the modulo operation.
● The behavior of the modulo operator with negative operands is consistent in C++11 and
later. Specifically, the sign of the result is the same as the dividend. For example, -7 % 3
would result in -1.
● The cmath library in C++ provides a suite of built-in mathematical functions that ensure
precision, efficiency, and platform-independent behavior.
● The sqrt function from the cmath library computes the square root of a number.
● The pow function from the cmath library computes the power of a number raised to an
exponent.
● The fabs function returns the absolute value of a floating-point number, which is the
non-negative value of its argument.
● The abs function returns the absolute value of an integer number, which is the non-
negative value of its argument.
Wednesday
● Implicit Type Conversion (Coercion):
○ This happens automatically when the compiler encounters a situation where a
specific data type is expected but a different one is provided.
○ 10 / 3.0 // results in 3.333… because the result is implicitly cast to double.
● Binary representation is the system computers use to store and process data, and it's
based on two states: 0 and 1. These states can be thought of as "off" and "on",
respectively.
● When numbers, letters, or other symbols are saved in a computer, they are converted
into sequences of 0s and 1s. These binary sequences are used because electronic
devices (like computer processors) can efficiently differentiate between the two states
(on/off, high/low, etc.) to read, process, and store data.
Friday
● The char data type is a fundamental data type in C++ used to store a single character. It
typically requires 1 byte of memory, which means it can represent 256 distinct values.
● The escape character is a backslash ( \ ). It's used to represent certain characters within
string literals and characters that otherwise have a specific meaning, such as newline,
tab, double quote, and single quote.
● A string is a sequence of characters used to represent text. It can be managed using the
standard library class std::string, which provides various functions for string manipulation
and handling. Strings are enclosed in double quotes, like "Hello, world!".
● When you use the cin object (from the iostream library) to read input into a std::string
variable, by default, it reads characters only up to the first whitespace encountered.
Whitespace includes spaces, tabs, and newline characters. The function getline(cin,
stringVar) gets all remaining text on the current input line, up to the next newline
character
● Quiz