Midterm
Midterm Overview
Course Information
Course: ECGR 2103 Computer Utilization in C++Instructor: Dr. Ke (Cory) WangInstitution: The William States Lee College of Engineering
Outline
Input and Output
Overview of how C++ programs handle input and output operations, including various methods of interacting with users or other systems.
Data Types
Understanding basic and advanced data types in C++, including how different data types are stored in memory and their respective sizes.
Branching Statements
Techniques for implementing decision-making within programs, including if statements, switch cases, and the importance of logical conditions.
Header and Namespace
Preprocessor Directives
#include<iostream>: Included for input and output operations. Essential for enabling functionalities likecoutandcin.Preprocessor Directive: Executes lines that begin with
#before compilation, allowing for code modularity and reuse.#define M_PI: Defines a constant for mathematical operations, enhancing code readability and maintainability.
Header Files
<iostream>: Essential for:Displaying output on the screen, enabling interactive user interfaces.
Reading input from the keyboard for dynamic program behavior.
Other standard C++ headers include
<cstring>,<array>,<vector>,<cmath>, and<memory>which provide additional functionalities.
Using Namespace
Namespaces in C++: To prevent naming conflicts in larger programs, the C++ standard library primarily uses the
stdnamespace. For best practices, it is recommended to use specific declarations likeusing std::cout;instead ofusing namespace std;, as it clarifies code and reduces ambiguity.
Basic Input and Output (iostream)
Insertion and Extraction Operators
<<: Insertion operator used to output data to the monitor. Example:cout << "Hello, World!";cin >>: Extraction operator that retrieves input from the keyboard. Example:cin >> wage;ensures ease of capturing user inputs and processing.
Text and Newlines
To output text, use:
cout << "desired text";(String literals must be enclosed in double quotes).To start a new line, use
cout << endl;, which can also flush the output buffer for immediate user feedback.
Buffering Mechanism
Input Buffering with
cin:Starting input: Buffers characters until the first non-whitespace character is detected, allowing for efficient user data entry.
Ending input: Transforms the buffered input to the corresponding variable type if a match occurs, enhancing program accuracy.
Output Buffering with
cout:Buffers output data until a semicolon (
;) or newline character is detected, minimizing the number of write operations to the console and improving performance.
Commenting
Comment Types
Single-line comments: Start with
//, are used for brief explanations.Multi-line comments: Start with
/*and end with*/, suitable for longer explanations, documentation, or code annotations.
Variables and Assignments
Variable Declaration
Variables must be clearly declared before usage, specifying their type, name, memory location, and an initial value, ensuring type safety and clarity.
Assignment Operator
Syntax:
variable = expression;wherevariablereceives the computedexpressionvalue.Example:
z = x + y - (2 * z);demonstrates assignment and arithmetic operations in action.
Value Swapping Method
A third temporary variable is often used to swap two variables, crucial in algorithms where order modification is required.
Integer Types
Data Type Overview
Common types include:
short int: typically occupies 2 bytes of memory.int: occupies 4 bytes with a wide range from -2,147,483,648 to +2,147,483,647, covering most integer applications.long long int: occupies 8 bytes, providing a large numerical range for calculations involving very large numbers.
Integer Literals
Defined in various number systems such as decimal, binary, hexadecimal, and octal, which provide flexibility in inputting values. Suffixes (e.g.,
u,l,ll) can be used to control the type and prevent overflow errors.
Arithmetic Operations
Operators
Basic arithmetic operations include
+,-,*,/, and%, specifically for integers. Understanding the difference in behavior between integer and floating-point division is crucial for accurate calculations.
Precedence Rules
Expressions respect standard mathematical precedence rules; knowing these helps avoid logical errors in complex expressions.
Increment and Decrement
The use of
++(increment) and--(decrement) operators allows for concise manipulation of variable values, pivotal in iterative processes.
Floating-point Numbers
Definition
A
doublevariable can store real and fractional numbers, such as98.6,0.0001,-55.667, allowing for precise real-number calculations.
Scientific Notation
Especially useful for representing very large or small values, often printed in a format like
2.99792e+08, enhancing readability and comprehension of data ranges.
Floating-point Division
The behavior of division by zero varies; C++ does not throw an error but returns special values like
inf(infinity) orNaN(not-a-number), which is crucial for error handling.
Constants
Definition
A constant variable is defined using the
constkeyword and holds its value post-initialization, offering protection against unintended modifications.
Type Conversions
Type Casting
Type casting allows for converting between data types using
static_castor C-style casting, essential for ensuring compatibility of variables in operations.
Math Functions
Common Mathematical Functions
Standard C++ math functions include
ceil(x),sqrt(x),pow(x, y),fabs(x), andexp(x), each serving distinct computational roles in mathematical operations.
Binary and Decimal Conversion
Binary to Decimal
Conversion from binary to decimal involves summing powers of two from the binary representation, foundational for understanding number systems.
Decimal to Binary
Determining binary representation from decimal input requires finding powers of two, further enhancing numerical literacy and programming skills.
ASCII and Bitwise Operators
ASCII Table
Defines character encoding for both control and printable characters, crucial for text processing in programming.
Bitwise Operators
Operators such as AND (
&), OR (|), and NOT (~) operate directly at the bit level, enabling low-level data manipulation techniques in applications.
Strings
String Basics
Character types store single characters; strings represent sequences of characters encapsulated in double quotes, providing robust methods for text handling.
String Input
The
getline()function captures input strings with whitespace, ensuring complete and accurate user data acquisition. Example:getline(cin, firstString);
C++ String Class Methods
Various functions such as
strlen(),strcpy(), and operators like+=are available for manipulating strings effectively.
Logical and Relational Operators
Logical Operators
Utilize logical operators (AND, OR, NOT) to evaluate truthiness, essential in decision-making processes in programming.
Relational Operators
Used primarily for comparisons, e.g.,
>,<,==, which guide conditional statements based on data relationships.
Conditional Statements
If Statement
Allows for conditional execution of code blocks based on true/false evaluations, providing dynamic control flow in applications.
Switch Statement
Useful for implementing multi-branch behavior based on matching constant values, simplifying complex conditional logic.
Comparison Practices
Floating-point Equality: Requires comparison for "close enough" due to precision issues that may arise in calculations, typically defined using an epsilon value to establish acceptable limits for equality comparisons.