Show description
CSE 240 | The Complete Guide
CSE 240 | The Complete Guide
CSE 240: The Guide
Mastering C, C++, and Core Programming Concepts
Module 1: Foundations
Module 2: Data & I/O
Module 3: Pointers & Structs
Module 4: Memory & Files
Module 5: Recursion
Module 6: OOP
Module 1: Foundations & Paradigms
Programming Paradigms
A paradigm is a model or pattern for how we express computation. The two main branches are Imperative (how to execute) and Declarative (what to execute).
Imperative/Procedural: The classic approach. You provide a step-by-step sequence of commands that change the program's state. (e.g., C, FORTRAN)
Object-Oriented (OOP): An extension of imperative programming. Data and the code that operates on that data are bundled into "objects". (e.g., C++, Java, C#)
Functional/Applicative: Treats computation as the evaluation of mathematical functions. It avoids changing state and mutable data (no side-effects). (e.g., Scheme, LISP)
Logic/Declarative: You define a set of facts and rules. The computer uses logic to figure out the solution. The focus is on the goal, not the algorithm. (e.g., Prolog)
Program Structure & Errors
Programs are analyzed in layers, from basic symbols to their ultimate meaning. Errors can occur at different stages.
Lexical: The basic building blocks (tokens) like keywords (int), identifiers (myVar), and operators (+).
Syntactic: The grammar rules. Does if (x > 0) follow the language's structure?
Contextual: The rules beyond grammar. Did you declare a variable before using it? Are the types compatible (e.g., you can't add a string to an integer without conversion)? Type checking happens here.
Semantic: The runtime meaning. A classic semantic error is division by zero. The code might be syntactically and contextually correct, but its behavior is invalid.
Macros vs. Functions
Both macros and functions are ways to reuse code, but they work very differently.
A macro is a preprocessor directive. It's a simple text substitution that happens *before* compilation. It's like a find-and-replace for your code.
// Macro definition
#define MAX(x, y) ((x) > (y) ? (x) : (y))
int a = 5, b = 10;
int m = MAX(a, b); // Preprocessor changes this to: int m = ((a) > (b) ? (a) : (b));
Pros: Faster execution as there is no function call overhead.…
CSE 240 | The Complete Guide
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSE 240 | The Complete Guide</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&family=VT323&display=swap" rel="stylesheet">
<style>
:root {
--background-color: #0a0a2a;
--primary-text: #e0e0e0;
--header-text: #00ff9b;
--accent-color: #ff00ff;
--secondary-accent: #ffff00;
--border-color: #00ff9b;
--code-bg: #1a1a3a;
--code-text: #80deea;
--shadow-color: rgba(0, 255, 155, 0.5);
--shadow-color-accent: rgba(255, 0, 255, 0.5);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
scroll-behavior: smooth;
}
body {
font-family: 'VT323', monospace;
background-color: var(--background-color);
color: var(--primary-text);
font-size: 20px;
line-height: 1.6;
overflow-x: hidden;
background-image:
linear-gradient(rgba(10, 10, 42, 0.95), rgba(10, 10, 42, 0.95)),
url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"%3E%3Cg fill-rule="evenodd"%3E%3Cg fill="%2300ff9b" fill-opacity="0.05"%3E%3Cpath opacity=".5" d="M96 95h4v1h-4v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9zm-1 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm9-10v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm9-10v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm9-10v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9z"/%3E%3Cpath d="M6 5V0h1v5h9V0h1v5h9V0h1v5h9V0h1v5h9V0h1v5h9V0h1v5h9V0h1v5h9V0h1v5h9V0h1v5h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1H6v-1h9v-9H6v-1h9v-9H6v-1h9v-9H6v-1h9v-9H6v-1h9v-9H6v-1h9v-9H6v-1h9v-9H6v-1h9v-9H6V5zm10 1h9v9h-9V6zm10 0h9v9h-9V6zm10 0h9v9h-9V6zm10 0h9v9h-9V6zm10 0h9v9h-9V6zm10 0h9v9h-9V6zm10 0h9v9h-9V6zm10 0h9v9h-9V6zm9 10h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-9 10h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm9 10h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-9 10h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm9 10h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-10 0h-9v9h9v-9zm-9 10h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9zm10 0h9v9h-9v-9z"/%3E%3C/g%3E%3C/g%3E%3C/svg%3E');
}
.container {
width: 90%;
max-width: 1200px;
margin: auto;
padding: 20px;
}
header {
text-align: center;
padding: 80px 20px 40px;
border-bottom: 4px solid var(--border-color);
box-shadow: 0 4px 15px -5px var(--shadow-color);
}
.glitch-title {
font-family: 'Press Start 2P', cursive;
font-size: 3rem;
color: var(--header-text);
text-shadow:
-2px -2px 0 var(--accent-color),
2px 2px 0 var(--secondary-accent);
position: relative;
animation: glitch 1s infinite;
}
@keyframes glitch {
0% { text-shadow: -2px -2px 0 var(--accent-color), 2px 2px 0 var(--secondary-accent); }
25% { text-shadow: 2px 2px 0 var(--accent-color), -2px -2px 0 var(--secondary-accent); }
50% { text-shadow: 2px -2px 0 var(--accent-color), -2px 2px 0 var(--secondary-accent); }
75% { text-shadow: -2px 2px 0 var(--accent-color), 2px -2px 0 var(--secondary-accent); }
100% { text-shadow: -2px -2px 0 var(--accent-color), 2px 2px 0 var(--secondary-accent); }
}
header p {
font-size: 1.5rem;
color: var(--secondary-accent);
margin-top: 10px;
}
nav {
background-color: var(--code-bg);
padding: 15px 0;
position: sticky;
top: 0;
z-index: 1000;
border-bottom: 2px solid var(--border-color);
box-shadow: 0 2px 10px var(--shadow-color);
}
nav ul {
list-style-type: none;
display: flex;
justify-content: center;
flex-wrap: wrap;
}
nav ul li {
margin: 5px 15px;
}
nav ul li a {
color: var(--header-text);
text-decoration: none;
font-size: 1.2rem;
padding: 5px 10px;
transition: all 0.3s ease;
text-shadow: 1px 1px 0 var(--accent-color);
}
nav ul li a:hover, nav ul li a.active {
color: var(--secondary-accent);
text-shadow: 1px 1px 0 var(--accent-color), 0 0 10px var(--secondary-accent);
}
section {
padding: 60px 20px;
border-bottom: 2px dashed var(--border-color);
}
section:last-of-type {
border-bottom: none;
}
h2 {
font-family: 'Press Start 2P', cursive;
font-size: 2.2rem;
color: var(--header-text);
margin-bottom: 30px;
text-align: center;
text-shadow: 2px 2px 0px var(--accent-color);
}
h3 {
font-family: 'Press Start 2P', cursive;
font-size: 1.5rem;
color: var(--secondary-accent);
margin-top: 40px;
margin-bottom: 20px;
border-bottom: 1px solid var(--accent-color);
padding-bottom: 5px;
}
p, ul, li {
margin-bottom: 15px;
}
ul {
padding-left: 40px;
list-style-type: '>> ';
}
ul li::marker {
color: var(--header-text);
}
.pixel-box {
background-color: var(--code-bg);
border: 2px solid var(--border-color);
padding: 20px;
margin: 20px 0;
box-shadow:
inset 0 0 10px rgba(0,0,0,0.5),
5px 5px 0 var(--border-color);
position: relative;
}
.pixel-box.accent-shadow {
box-shadow:
inset 0 0 10px rgba(0,0,0,0.5),
5px 5px 0 var(--accent-color);
}
code {
font-family: 'VT323', monospace;
background-color: rgba(255, 255, 0, 0.1);
color: var(--secondary-accent);
padding: 3px 6px;
border-radius: 3px;
}
pre {
background-color: var(--code-bg);
color: var(--code-text);
padding: 20px;
border: 2px solid var(--accent-color);
box-shadow: 3px 3px 0 var(--accent-color);
overflow-x: auto;
font-size: 18px;
white-space: pre-wrap;
word-wrap: break-word;
}
pre code {
background: none;
padding: 0;
color: inherit;
}
strong {
color: var(--header-text);
}
footer {
text-align: center;
padding: 40px 20px;
margin-top: 40px;
border-top: 4px solid var(--border-color);
font-size: 1.2rem;
color: var(--accent-color);
}
.typewriter {
overflow: hidden;
border-right: .15em solid var(--header-text);
white-space: nowrap;
margin: 0 auto;
letter-spacing: .1em;
animation:
typing 3s steps(30, end) forwards,
blink-caret .75s step-end infinite;
}
@keyframes typing {
from { width: 0 }
to { width: 100% }
}
@keyframes blink-caret {
from, to { border-color: transparent }
50% { border-color: var(--header-text); }
}
.scroll-to-top {
position: fixed;
bottom: 20px;
right: 20px;
background-color: var(--accent-color);
color: var(--background-color);
border: 2px solid var(--header-text);
width: 50px;
height: 50px;
text-align: center;
font-size: 30px;
line-height: 46px;
cursor: pointer;
display: none;
z-index: 1001;
box-shadow: 3px 3px 0 var(--header-text);
transition: all 0.3s ease;
}
.scroll-to-top:hover {
background-color: var(--header-text);
color: var(--background-color);
box-shadow: 3px 3px 0 var(--accent-color);
transform: translateY(-5px);
}
@media (max-width: 768px) {
.glitch-title {
font-size: 2rem;
}
nav ul {
flex-direction: column;
align-items: center;
}
}
</style>
</head>
<body>
<header>
<div class="container">
<h1 class="glitch-title typewriter">CSE 240: The Guide</h1>
<p>Mastering C, C++, and Core Programming Concepts</p>
</div>
</header>
<nav id="navbar">
<div class="container">
<ul>
<li><a href="#module1">Module 1: Foundations</a></li>
<li><a href="#module2">Module 2: Data & I/O</a></li>
<li><a href="#module3">Module 3: Pointers & Structs</a></li>
<li><a href="#module4">Module 4: Memory & Files</a></li>
<li><a href="#module5">Module 5: Recursion</a></li>
<li><a href="#module6">Module 6: OOP</a></li>
</ul>
</div>
</nav>
<main class="container">
<section id="module1">
<h2>Module 1: Foundations & Paradigms</h2>
<div class="pixel-box">
<h3>Programming Paradigms</h3>
<p>A paradigm is a model or pattern for how we express computation. The two main branches are Imperative (how to execute) and Declarative (what to execute).</p>
<ul>
<li><strong>Imperative/Procedural:</strong> The classic approach. You provide a step-by-step sequence of commands that change the program's state. (e.g., C, FORTRAN)</li>
<li><strong>Object-Oriented (OOP):</strong> An extension of imperative programming. Data and the code that operates on that data are bundled into "objects". (e.g., C++, Java, C#)</li>
<li><strong>Functional/Applicative:</strong> Treats computation as the evaluation of mathematical functions. It avoids changing state and mutable data (no side-effects). (e.g., Scheme, LISP)</li>
<li><strong>Logic/Declarative:</strong> You define a set of facts and rules. The computer uses logic to figure out the solution. The focus is on the goal, not the algorithm. (e.g., Prolog)</li>
</ul>
</div>
<div class="pixel-box accent-shadow">
<h3>Program Structure & Errors</h3>
<p>Programs are analyzed in layers, from basic symbols to their ultimate meaning. Errors can occur at different stages.</p>
<ul>
<li><strong>Lexical:</strong> The basic building blocks (tokens) like keywords (<code>int</code>), identifiers (<code>myVar</code>), and operators (<code>+</code>).</li>
<li><strong>Syntactic:</strong> The grammar rules. Does <code>if (x > 0)</code> follow the language's structure?</li>
<li><strong>Contextual:</strong> The rules beyond grammar. Did you declare a variable before using it? Are the types compatible (e.g., you can't add a string to an integer without conversion)? Type checking happens here.</li>
<li><strong>Semantic:</strong> The runtime meaning. A classic semantic error is division by zero. The code might be syntactically and contextually correct, but its behavior is invalid.</li>
</ul>
</div>
<div class="pixel-box">
<h3>Macros vs. Functions</h3>
<p>Both macros and functions are ways to reuse code, but they work very differently.</p>
<p>A <strong>macro</strong> is a preprocessor directive. It's a simple text substitution that happens *before* compilation. It's like a find-and-replace for your code.</p>
<pre><code>// Macro definition
#define MAX(x, y) ((x) > (y) ? (x) : (y))
int a = 5, b = 10;
int m = MAX(a, b); // Preprocessor changes this to: int m = ((a) > (b) ? (a) : (b));</code></pre>
<ul>
<li><strong>Pros:</strong> Faster execution as there is no function call overhead. Code is "inlined".</li>
<li><strong>Cons (Side Effects):</strong> Arguments are evaluated every time they appear. <code>MAX(a++, b++)</code> will increment the variables multiple times, leading to unexpected behavior. The programmer is responsible for correctness. Parentheses are crucial to avoid operator precedence issues.</li>
</ul>
<p>A <strong>function</strong> involves a proper function call. Execution jumps to the function's code, arguments are pushed to the stack, and a value is returned. It's safer but has a small performance overhead.</p>
</div>
</section>
<section id="module2">
<h2>Module 2: Data Types & Basic I/O</h2>
<div class="pixel-box">
<h3>Data Types & Type Checking</h3>
<p>A data type defines a set of allowed values and the operations that can be performed on them. C/C++ have several primitive types:</p>
<ul>
<li><code>char</code>: A single character (typically 1 byte).</li>
<li><code>int</code>: An integer. Can be modified with <code>short</code>, <code>long</code>, <code>signed</code>, <code>unsigned</code>.</li>
<li><code>float</code>, <code>double</code>: Floating-point numbers with different precision.</li>
<li><code>void</code>: Represents the absence of a type.</li>
<li>C++ adds <code>bool</code> (true/false) and <code>wchar_t</code> (wide character).</li>
</ul>
<p><strong>Type Checking</strong> ensures that operations are valid for the given types.
<ul>
<li><strong>Static Typing (C/C++):</strong> Types are checked at compile-time. Catches errors early.</li>
<li><strong>Dynamic Typing (Python, JS):</strong> Types are checked at run-time. More flexible, but errors might only appear during execution.</li>
<li><strong>Strong vs. Weak Typing:</strong> Refers to how strictly type rules are enforced. C++ is strongly typed (you can't add an int to a string without explicit conversion), while C is weaker (<code>char</code> and <code>int</code> can sometimes be used interchangeably).</li>
</ul>
</div>
<div class="pixel-box accent-shadow">
<h3>Basic I/O in C and C++</h3>
<p><strong>Formatted I/O</strong> reads/writes data in specific formats.</p>
<p>In C, we use <code><stdio.h></code>:</p>
<pre><code>#include <stdio.h>
int main() {
int i;
printf("Enter an integer: ");
// scanf needs the ADDRESS of the variable to store the value
scanf("%d", &i);
printf("You entered: %d\n", i);
return 0;
}</code></pre>
<p>In C++, we use <code><iostream></code>:</p>
<pre><code>#include <iostream>
int main() {
int i;
std::cout << "Enter an integer: ";
std::cin >> i; // cin handles the memory automatically
std::cout << "You entered: " << i << std::endl;
return 0;
}</code></pre>
<p><strong>Unformatted I/O</strong> reads raw characters or lines. A common issue is mixing formatted and unformatted input, which can leave a newline character (<code>\n</code>) in the input buffer. Use functions like <code>fflush(stdin)</code> (C) or <code>cin.ignore()</code> (C++) to clear it.</p>
</div>
</section>
<section id="module3">
<h2>Module 3: Pointers, Arrays & Structs</h2>
<div class="pixel-box">
<h3>Pointers: The Core Concept</h3>
<p>A pointer is a variable that stores the memory address of another variable. The value of a pointer is an integer representing a location in memory.</p>
<ul>
<li><strong>Referencing (`&`):</strong> The "address-of" operator. It gets the memory address of a variable. <code>int* p = &myVar;</code></li>
<li><strong>Dereferencing (`*`):</strong> The "value-at-address" operator. It gets the value stored at the memory address the pointer is holding. <code>int value = *p;</code></li>
</ul>
<p>Pointers allow for indirect modification of data, which is essential for dynamic memory, data structures, and efficient function parameter passing.</p>
<pre><code>int x = 10; // A regular integer variable
int* ptr; // A pointer to an integer
ptr = &x; // ptr now holds the memory address of x
*ptr = 20; // Dereference ptr to change the value AT that address.
// This changes x to 20!
printf("Value of x is: %d\n", x); // Prints 20</code></pre>
</div>
<div class="pixel-box accent-shadow">
<h3>Arrays and Strings</h3>
<p>An array is a collection of elements of the same type stored in contiguous memory locations. In C, the name of an array "decays" into a pointer to its first element.</p>
<pre><code>int arr[5] = {10, 20, 30, 40, 50};
int* p = arr; // Same as p = &arr[0];
// These two lines are equivalent:
printf("%d\n", arr[2]); // Prints 30 (Array notation)
printf("%d\n", *(p + 2)); // Prints 30 (Pointer arithmetic)</code></pre>
<p>A <strong>C-style string</strong> is simply an array of characters that is terminated by a special null character (<code>'\0'</code>). This terminator is crucial for functions like <code>strcpy</code> or <code>printf("%s", ...)</code> to know where the string ends.</p>
</div>
<div class="pixel-box">
<h3>Defining New Types: `struct`, `enum`, `typedef`</h3>
<p>You can create your own custom data types.</p>
<ul>
<li><strong><code>struct</code>:</strong> A composite type that groups together variables of different types under a single name. Similar to a class in Java but traditionally only contains data.</li>
<pre><code>struct Contact {
char name[50];
int phone;
};
struct Contact person1;
strcpy(person1.name, "Ada Lovelace");
person1.phone = 12345;</code></pre>
<li><strong><code>enum</code>:</strong> Creates a type where variables can only hold a specific set of named integer constants.</li>
<pre><code>enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }; // MON=0, TUE=1, ...
enum Day today = WED;</code></pre>
<li><strong><code>typedef</code>:</strong> Creates a synonym or alias for an existing type, making code more readable.</li>
<pre><code>typedef unsigned long ulong;
ulong bigNumber = 1000000UL;
// Often used with structs to avoid typing 'struct' repeatedly
typedef struct Contact Person;
Person person2;</code></pre>
</div>
</section>
<section id="module4">
<h2>Module 4: Memory & File I/O</h2>
<div class="pixel-box accent-shadow">
<h3>Memory Organization: Static, Stack, Heap</h3>
<p>A C/C++ program's memory is divided into three main areas:</p>
<ul>
<li><strong>Static/Global:</strong> Memory for global and `static` variables. Allocated once when the program starts and lasts for the entire execution.</li>
<li><strong>Stack:</strong> Memory for local variables and function parameters. It's managed automatically. When a function is called, a "stack frame" is created for its variables. When the function returns, the frame is destroyed. This is fast but limited in size.</li>
<li><strong>Heap:</strong> A large pool of memory for dynamic allocation. You manually request memory from the heap using `malloc()` (C) or `new` (C++), and you are responsible for freeing it with `free()` or `delete`. This is where memory leaks happen!</li>
</ul>
</div>
<div class="pixel-box">
<h3>Parameter Passing</h3>
<p>How data is passed to functions is crucial.</p>
<ul>
<li><strong>Pass-by-Value:</strong> The function receives a *copy* of the argument's value. Changes inside the function do not affect the original variable. This is the default in C/C++.</li>
<li><strong>Pass-by-Address (Pointer):</strong> The function receives the memory *address* of the argument. By dereferencing this pointer, the function can directly modify the original variable.</li>
<li><strong>Pass-by-Alias (Reference - C++ only):</strong> The function receives an *alias* for the original variable. Syntactically it looks like pass-by-value, but it behaves like pass-by-address, allowing direct modification of the original.</li>
</ul>
<pre><code>// Pass-by-Value (y is a copy)
// Pass-by-Address (*x points to original)
void func(int *x, int y) {
*x = *x + y; // Modifies the original variable x points to
y = 2; // Modifies only the local copy of y
}
int main() {
int a = 10, b = 10;
func(&a, b);
// After call: a is 20, b is still 10
printf("a: %d, b: %d\n", a, b);
}</code></pre>
</div>
<div class="pixel-box">
<h3>File I/O and Buffering</h3>
<p>Reading from/writing to a disk is very slow. To improve performance, file operations use a <strong>buffer</strong>—a temporary storage area in memory. When you open a file with <code>fopen()</code>, a buffer is created. Data is read from the disk into the buffer in large chunks, or written from your program to the buffer. The buffer is only flushed (written) to the actual disk file when it's full or when you explicitly close the file with <code>fclose()</code>.</p>
</div>
<div class="pixel-box accent-shadow">
<h3>Linked Lists</h3>
<p>A linked list is a fundamental dynamic data structure. It's a sequence of nodes where each node contains data and a pointer to the next node in the sequence. The list is accessed via a pointer to the first node, called the <code>head</code>.</p>
<pre><code>struct Node {
int data;
struct Node* next;
};</code></pre>
<p>Operations like adding or removing nodes involve manipulating these <code>next</code> pointers to correctly link the nodes together.</p>
</div>
</section>
<section id="module5">
<h2>Module 5: Recursion & Data Structures</h2>
<div class="pixel-box">
<h3>Recursion</h3>
<p>A function is recursive if it calls itself. A recursive solution must have two parts:</p>
<ol>
<li><strong>Base Case:</strong> A stopping condition that does not make a recursive call. This prevents infinite loops.</li>
<li><strong>Recursive Step:</strong> The part of the function that calls itself, but on a smaller or simpler version of the problem.</li>
</ol>
<pre><code>// Recursive factorial function
int factorial(int n) {
// Base Case
if (n == 0) {
return 1;
}
// Recursive Step
else {
return n * factorial(n - 1); // Solves a smaller problem
}
}</code></pre>
<p>While elegant, recursive calls use the stack for each call. Deep recursion can lead to a "stack overflow" error.</p>
</div>
<div class="pixel-box accent-shadow">
<h3>Trees and Binary Search Trees (BST)</h3>
<p>A <strong>tree</strong> is a hierarchical data structure. If each node can have at most two children (a left and a right child), it's a <strong>binary tree</strong>.</p>
<p>A <strong>Binary Search Tree (BST)</strong> is a special binary tree that maintains a specific ordering property:</p>
<ul>
<li>For any given node, all values in its <strong>left</strong> subtree are less than the node's value.</li>
<li>For any given node, all values in its <strong>right</strong> subtree are greater than the node's value.</li>
</ul>
<p>This property makes searching for an element very efficient, with an average complexity of O(log n).</p>
<h3>Tree Traversal</h3>
<p>Traversal is the process of visiting each node in the tree exactly once. The three main recursive traversal orders are:</p>
<ul>
<li><strong>Pre-order:</strong> Visit Root, then traverse Left subtree, then traverse Right subtree.</li>
<li><strong>In-order:</strong> Traverse Left subtree, then visit Root, then traverse Right subtree. (For a BST, this visits nodes in sorted order!)</li>
<li><strong>Post-order:</strong> Traverse Left subtree, then traverse Right subtree, then visit Root. (Useful for deleting a tree).</li>
</ul>
</div>
</section>
<section id="module6">
<h2>Module 6: OOP & Memory Management</h2>
<div class="pixel-box">
<h3>Object-Oriented Programming (OOP) in C++</h3>
<p>OOP is a paradigm centered around objects. A <strong>class</strong> is a blueprint for creating objects. An <strong>object</strong> is an instance of a class.</p>
<p>Key Principles:</p>
<ul>
<li><strong>Encapsulation:</strong> Bundling data (member variables) and the methods (member functions) that operate on the data into a single unit (the class).</li>
<li><strong>Information Hiding:</strong> Restricting access to certain members of an object. This is done with access specifiers: <code>public</code>, <code>protected</code>, and <code>private</code>. Data members are almost always kept <code>private</code>.</li>
<li><strong>Abstraction:</strong> Hiding the complex implementation details and showing only the essential features of the object.</li>
</ul>
</div>
<div class="pixel-box accent-shadow">
<h3>Constructors and Destructors</h3>
<p>These are special member functions that control an object's life cycle.</p>
<ul>
<li><strong>Constructor:</strong> A function with the same name as the class. It's called automatically when an object is created. Its job is to initialize the object's state.</li>
<li><strong>Destructor:</strong> A function with the same name as the class, preceded by a tilde (<code>~</code>). It's called automatically when an object is destroyed. Its primary job is to release any resources the object acquired, especially heap memory.</li>
</ul>
<pre><code>class Queue {
private:
int* buffer;
int size;
public:
// Constructor: allocates heap memory
Queue(int s) {
size = s;
buffer = new int[size]; // Request memory from the heap
}
// Destructor: must free the heap memory
~Queue() {
delete[] buffer; // Release the memory
}
};</code></pre>
</div>
<div class="pixel-box">
<h3>Manual Memory Management & Garbage Collection</h3>
<p>C++ does <strong>not</strong> have an automatic garbage collector. You, the programmer, are responsible for memory management.</p>
<ul>
<li><strong>Memory Leak:</strong> Occurs when you allocate memory on the heap (with <code>new</code> or <code>malloc</code>) but forget to deallocate it (with <code>delete</code> or <code>free</code>). The memory remains reserved and unusable for the rest of the program's life.</li>
<li><strong>Dangling Pointer:</strong> A pointer that points to a memory location that has already been freed. Accessing a dangling pointer leads to undefined behavior.</li>
</ul>
<p><strong>The Rule of Thumb:</strong> For every <code>new</code>, there must be a corresponding <code>delete</code>. For every <code>new[]</code> (array allocation), there must be a corresponding <code>delete[]</code>.</p>
<p>The best way to delete a dynamic data structure like a linked list or tree is to traverse it and delete each node one by one. For a tree, a <strong>post-order traversal</strong> is ideal for deletion, as it deletes children before their parent.</p>
</div>
</section>
</main>
<footer>
<div class="container">
<p>END OF TRANSMISSION. GOOD LUCK, PROGRAMMER.</p>
</div>
</footer>
<div class="scroll-to-top" id="scrollToTopBtn">^</div>
<script>
// JavaScript for interactivity
document.addEventListener('DOMContentLoaded', function() {
const navLinks = document.querySelectorAll('nav ul li a');
const sections = document.querySelectorAll('main section');
const scrollToTopBtn = document.getElementById('scrollToTopBtn');
// Active nav link on scroll
function changeLinkState() {
let index = sections.length;
while(--index && window.scrollY + 50 < sections[index].offsetTop) {}
navLinks.forEach((link) => link.classList.remove('active'));
if (navLinks[index]) {
navLinks[index].classList.add('active');
}
}
changeLinkState();
window.addEventListener('scroll', changeLinkState);
// Scroll to top button
window.addEventListener('scroll', () => {
if (window.pageYOffset > 300) {
scrollToTopBtn.style.display = "block";
} else {
scrollToTopBtn.style.display = "none";
}
});
scrollToTopBtn.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
// Typewriter effect re-trigger for demo
const typewriter = document.querySelector('.typewriter');
if(typewriter) {
const text = typewriter.innerHTML;
typewriter.innerHTML = '';
typewriter.style.width = '0';
setTimeout(() => {
typewriter.innerHTML = text;
typewriter.style.animation = 'none'; // reset animation
typewriter.offsetHeight; // trigger reflow
typewriter.style.animation = null;
}, 100);
}
});
</script>
</body>
</html>