Show description
CSE 240 Master Cheat Sheet
CSE 240 Master Cheat Sheet
CSE 240: Master Cheat Sheet
Core Concepts & Programming Paradigms
Programming Paradigms
Imperative/Procedural: Focus on *how* to execute (C). Step-by-step control flow that modifies program state.
Object-Oriented: Encapsulates state and behavior into objects (C++). Focuses on data abstraction and inheritance.
Functional: Focus on *what* to execute (Scheme). Treats computation as the evaluation of mathematical functions, avoiding side effects.
Logic: Rule-based approach using facts and queries (Prolog).
Program Structure Analysis
Lexical: The basic building blocks (tokens) like keywords, identifiers, operators.
Syntactic: The grammar rules for combining tokens into valid statements (e.g., BNF).
Contextual: Rules beyond grammar, like type checking and variable scope.
Semantic: The meaning and runtime behavior of the code (e.g., division by zero).
C Programming (Imperative)
Pointers: The Core of C
Pointers store memory addresses. Their value is an integer.
int x = 10; // A variable
int *p; // A pointer to an integer
p = &x; // & (address-of): p now holds the address of x
*p = 20; // * (dereference): the value at the address p points to is now 20. x is now 20!
p++; // Pointer arithmetic: moves pointer to the next memory slot of its type.
Memory Management (Manual)
Memory must be manually allocated and freed from the heap.
int *arr = (int*)malloc(10 * sizeof(int));
// ... use the array ...
free(arr); // Always free what you malloc!
Structs & Typedef
Group related data.…
CSE 240 Master Cheat Sheet
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSE 240 Master Cheat Sheet</title>
<script src="https://cdn.tailwindcss.com"></script>
<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=Inter:wght@400;500;700&family=Roboto+Mono:wght@400;500&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Inter', sans-serif;
background-color: #1a1a1a;
color: #E0E0E0;
}
.container {
max-width: 1200px;
margin: auto;
padding: 2rem;
}
h1, h2, h3 {
font-family: 'Inter', sans-serif;
font-weight: 700;
color: #FFA726; /* Orange accent */
}
h1 {
border-bottom: 2px solid #FFA726;
padding-bottom: 0.5rem;
margin-bottom: 2rem;
}
h2 {
border-bottom: 1px solid #424242;
padding-bottom: 0.5rem;
margin-top: 2.5rem;
margin-bottom: 1.5rem;
}
code, pre {
font-family: 'Roboto Mono', monospace;
background-color: #2C2C2C;
border-radius: 0.5rem;
border: 1px solid #424242;
}
code {
padding: 0.2em 0.4em;
margin: 0;
font-size: 85%;
}
pre {
padding: 1rem;
white-space: pre-wrap;
word-break: break-all;
}
.keyword {
color: #FFB74D; /* Lighter orange for keywords */
}
.comment {
color: #9E9E9E; /* Grey for comments */
}
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
gap: 2rem;
}
.card {
background-color: #212121;
border: 1px solid #424242;
border-radius: 0.75rem;
padding: 1.5rem;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.card h3 {
margin-top: 0;
margin-bottom: 1rem;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 1rem;
}
th, td {
text-align: left;
padding: 0.75rem;
border-bottom: 1px solid #424242;
}
th {
color: #FFA726;
}
</style>
</head>
<body>
<div class="container">
<h1>CSE 240: Master Cheat Sheet</h1>
<!-- Section: Core Concepts & Paradigms -->
<h2>Core Concepts & Programming Paradigms</h2>
<div class="grid-container">
<div class="card">
<h3>Programming Paradigms</h3>
<ul>
<li><strong>Imperative/Procedural:</strong> Focus on *how* to execute (C). Step-by-step control flow that modifies program state.</li>
<li><strong>Object-Oriented:</strong> Encapsulates state and behavior into objects (C++). Focuses on data abstraction and inheritance.</li>
<li><strong>Functional:</strong> Focus on *what* to execute (Scheme). Treats computation as the evaluation of mathematical functions, avoiding side effects.</li>
<li><strong>Logic:</strong> Rule-based approach using facts and queries (Prolog).</li>
</ul>
</div>
<div class="card">
<h3>Program Structure Analysis</h3>
<ul>
<li><strong>Lexical:</strong> The basic building blocks (tokens) like keywords, identifiers, operators.</li>
<li><strong>Syntactic:</strong> The grammar rules for combining tokens into valid statements (e.g., BNF).</li>
<li><strong>Contextual:</strong> Rules beyond grammar, like type checking and variable scope.</li>
<li><strong>Semantic:</strong> The meaning and runtime behavior of the code (e.g., division by zero).</li>
</ul>
</div>
</div>
<!-- Section: C Programming -->
<h2>C Programming (Imperative)</h2>
<div class="grid-container">
<div class="card">
<h3>Pointers: The Core of C</h3>
<p>Pointers store memory addresses. Their value is an integer.</p>
<pre><span class="keyword">int</span> x = <span class="keyword">10</span>; <span class="comment">// A variable</span>
<span class="keyword">int</span> *p; <span class="comment">// A pointer to an integer</span>
p = &x; <span class="comment">// & (address-of): p now holds the address of x</span>
*p = <span class="keyword">20</span>; <span class="comment">// * (dereference): the value at the address p points to is now 20. x is now 20!</span>
p++; <span class="comment">// Pointer arithmetic: moves pointer to the next memory slot of its type.</span></pre>
</div>
<div class="card">
<h3>Memory Management (Manual)</h3>
<p>Memory must be manually allocated and freed from the heap.</p>
<pre><span class="keyword">int</span> *arr = (<span class="keyword">int</span>*)<span class="keyword">malloc</span>(<span class="keyword">10</span> * <span class="keyword">sizeof</span>(<span class="keyword">int</span>));
<span class="comment">// ... use the array ...</span>
<span class="keyword">free</span>(arr); <span class="comment">// Always free what you malloc!</span></pre>
</div>
<div class="card">
<h3>Structs & Typedef</h3>
<p>Group related data. `typedef` creates a convenient alias.</p>
<pre><span class="keyword">typedef struct</span> {
<span class="keyword">char</span> name[<span class="keyword">50</span>];
<span class="keyword">int</span> id;
} Student;
Student s1;
strcpy(s1.name, <span class="string">"Alex"</span>);
s1.id = <span class="keyword">123</span>;</pre>
</div>
<div class="card">
<h3>C-Style Strings & Arrays</h3>
<p>A string is just a `char` array ending with a null terminator `\0`.</p>
<pre><span class="keyword">char</span> greeting[] = <span class="string">"Hello"</span>; <span class="comment">// Size 6 ('H','e','l','l','o','\0')</span>
<span class="keyword">int</span> numbers[<span class="keyword">5</span>] = {<span class="keyword">1</span>, <span class="keyword">2</span>, <span class="keyword">3</span>, <span class="keyword">4</span>, <span class="keyword">5</span>};</pre>
</div>
</div>
<!-- Section: C++ and OOP -->
<h2>C++ & Object-Oriented Programming</h2>
<div class="grid-container">
<div class="card">
<h3>Classes & Objects</h3>
<p>Encapsulates data (private) and operations (public).</p>
<pre><span class="keyword">class</span> Rectangle {
<span class="keyword">private:</span>
<span class="keyword">int</span> width, height;
<span class="keyword">public:</span>
<span class="comment">// Constructor</span>
Rectangle(<span class="keyword">int</span> w, <span class="keyword">int</span> h) : width(w), height(h) {}
<span class="keyword">int</span> area() { <span class="keyword">return</span> width * height; }
<span class="comment">// Destructor</span>
~Rectangle() { <span class="comment">// Cleanup code here</span> }
};</pre>
</div>
<div class="card">
<h3>Memory Management (C++)</h3>
<p>Use `new` to allocate and `delete` to deallocate.</p>
<pre>Rectangle *r = <span class="keyword">new</span> Rectangle(<span class="keyword">5</span>, <span class="keyword">10</span>);
<span class="comment">// ... use the object via r->area() ...</span>
<span class="keyword">delete</span> r; <span class="comment">// Deletes a single object</span>
<span class="keyword">int</span> *arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="keyword">50</span>];
<span class="keyword">delete</span>[] arr; <span class="comment">// Deletes an array of objects</span></pre>
</div>
<div class="card">
<h3>Inheritance & Polymorphism</h3>
<p>Build new classes from existing ones. `virtual` enables dynamic behavior.</p>
<pre><span class="keyword">class</span> Shape {
<span class="keyword">public:</span>
<span class="keyword">virtual void</span> draw() { <span class="comment">/*...*/</span> }
};
<span class="keyword">class</span> Circle : <span class="keyword">public</span> Shape {
<span class="keyword">public:</span>
<span class="keyword">void</span> draw() <span class="keyword">override</span> { <span class="comment">/* Draw a circle */</span> }
};</pre>
</div>
</div>
<!-- Section: Scheme Programming -->
<h2>Scheme (Functional Programming)</h2>
<div class="grid-container">
<div class="card">
<h3>Core Syntax & Philosophy</h3>
<p>Everything is an S-expression in prefix notation. Avoids side effects.</p>
<pre><span class="comment">; (operator operand1 operand2 ...)</span>
(+ <span class="keyword">10</span> (* <span class="keyword">2</span> <span class="keyword">5</span>)) <span class="comment">; Evaluates to 20</span></pre>
<p><strong>`'()`</strong> is the empty list, and it is the only value that is not `#t` in a boolean context.</p>
</div>
<div class="card">
<h3>Key Forms</h3>
<table>
<tr>
<th>Form</th>
<th>Purpose</th>
</tr>
<tr>
<td><code>(define name value)</code></td>
<td>Global binding</td>
</tr>
<tr>
<td><code>(define (f x) body)</code></td>
<td>Function definition</td>
</tr>
<tr>
<td><code>(lambda (params) body)</code></td>
<td>Create an anonymous function</td>
</tr>
<tr>
<td><code>(let ((n v)) body)</code></td>
<td>Local binding</td>
</tr>
<tr>
<td><code>(if test then else)</code></td>
<td>Conditional</td>
</tr>
</table>
</div>
<div class="card">
<h3>Pairs & Lists</h3>
<p>The fundamental data structure is the pair, built with `cons`.</p>
<pre>(<span class="keyword">cons</span> <span class="keyword">1</span> <span class="keyword">2</span>) <span class="comment">; -> '(1 . 2) (a pair)</span>
(<span class="keyword">cons</span> <span class="keyword">1</span> '()) <span class="comment">; -> '(1) (a list)</span>
(<span class="keyword">cons</span> <span class="keyword">1</span> '(<span class="keyword">2</span> <span class="keyword">3</span>)) <span class="comment">; -> '(1 2 3)</span>
(<span class="keyword">car</span> '(<span class="keyword">1</span> <span class="keyword">2</span> <span class="keyword">3</span>)) <span class="comment">; -> 1 (first element)</span>
(<span class="keyword">cdr</span> '(<span class="keyword">1</span> <span class="keyword">2</span> <span class="keyword">3</span>)) <span class="comment">; -> '(2 3) (rest of the list)</span></pre>
</div>
<div class="card">
<h3>Higher-Order Functions</h3>
<p>Functions that take other functions as arguments.</p>
<pre><span class="comment">; Apply (* 2) to each element</span>
(<span class="keyword">map</span> (<span class="keyword">lambda</span> (x) (* x <span class="keyword">2</span>)) '(<span class="keyword">1</span> <span class="keyword">2</span> <span class="keyword">3</span>))
<span class="comment">; -> '(2 4 6)</span>
<span class="comment">; Keep only even numbers</span>
(<span class="keyword">filter</span> even? '(<span class="keyword">1</span> <span class="keyword">2</span> <span class="keyword">3</span> <span class="keyword">4</span>))
<span class="comment">; -> '(2 4)</span></pre>
</div>
</div>
</div>
</body>
</html>