Show description
Multi-Dimensional Arrays: The Ultimate Guide
Multi-Dimensional Arrays: The Ultimate Guide
MULTI-DIMENSIONAL ARRAYS
The Ultimate Programming Guide
BASICS
MEMORY
ALLOCATION
LANGUAGES
PERFORMANCE
EXAMPLES
LEVEL 1: FUNDAMENTALS
Multi-dimensional arrays are data structures that organize elements in multiple dimensions, creating matrices, cubes, and higher-dimensional structures. Think of them as grids within grids.
What Are Multi-Dimensional Arrays?
A multi-dimensional array is essentially an array of arrays. While a 1D array is like a list, a 2D array is like a table with rows and columns, and a 3D array is like a cube of data.
// 1D Array - Single dimension
int array1D[5] = {1, 2, 3, 4, 5};
// 2D Array - Rows and columns
int array2D[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// 3D Array - Depth, rows, and columns
int array3D[2][3][4];
Interactive Array Visualizer
1D Array
2D Array
3D Array
Click a button to visualize different array types!
LEVEL 2: MEMORY LAYOUT
Understanding how multi-dimensional arrays are stored in memory is crucial for writing efficient code and avoiding common pitfalls.
Row-Major vs Column-Major Order
Different programming languages store 2D arrays in memory differently.…
Multi-Dimensional Arrays: The Ultimate Guide
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multi-Dimensional Arrays: The Ultimate Guide</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
:root {
--bg-dark: #0f0f23;
--bg-secondary: #1a1a2e;
--accent-green: #00ff41;
--accent-orange: #ff8c00;
--accent-purple: #9d4edd;
--accent-cyan: #00ffff;
--text-primary: #e0e0e0;
--text-secondary: #a0a0a0;
--border-color: #333;
--code-bg: #16213e;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Press Start 2P', monospace;
background: var(--bg-dark);
color: var(--text-primary);
line-height: 1.6;
overflow-x: hidden;
}
/* Scanline effect */
body::before {
content: '';
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: repeating-linear-gradient(
0deg,
transparent,
transparent 2px,
rgba(0, 255, 65, 0.03) 2px,
rgba(0, 255, 65, 0.03) 4px
);
pointer-events: none;
z-index: 1000;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
/* Header */
.header {
text-align: center;
padding: 40px 0;
background: linear-gradient(45deg, var(--bg-secondary), var(--bg-dark));
border: 3px solid var(--accent-green);
margin-bottom: 30px;
position: relative;
animation: glow 2s ease-in-out infinite alternate;
}
@keyframes glow {
from { box-shadow: 0 0 5px var(--accent-green); }
to { box-shadow: 0 0 20px var(--accent-green), 0 0 30px var(--accent-green); }
}
.header h1 {
font-size: clamp(16px, 4vw, 28px);
color: var(--accent-green);
text-shadow: 2px 2px 0px var(--accent-orange);
margin-bottom: 10px;
}
.header p {
font-size: clamp(8px, 2vw, 12px);
color: var(--text-secondary);
}
/* Navigation */
.nav {
display: flex;
justify-content: center;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 40px;
}
.nav-btn {
background: var(--bg-secondary);
color: var(--accent-cyan);
border: 2px solid var(--accent-cyan);
padding: 8px 16px;
font-family: inherit;
font-size: 10px;
cursor: pointer;
transition: all 0.3s;
text-decoration: none;
display: inline-block;
}
.nav-btn:hover {
background: var(--accent-cyan);
color: var(--bg-dark);
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 255, 255, 0.3);
}
/* Sections */
.section {
background: var(--bg-secondary);
border: 2px solid var(--border-color);
margin-bottom: 30px;
padding: 25px;
position: relative;
animation: slideInLeft 0.5s ease-out;
}
@keyframes slideInLeft {
from { opacity: 0; transform: translateX(-20px); }
to { opacity: 1; transform: translateX(0); }
}
.section h2 {
color: var(--accent-orange);
font-size: clamp(12px, 3vw, 18px);
margin-bottom: 20px;
text-shadow: 1px 1px 0px var(--accent-purple);
}
.section h3 {
color: var(--accent-purple);
font-size: clamp(10px, 2.5vw, 14px);
margin: 20px 0 15px 0;
}
.section p {
font-size: clamp(8px, 2vw, 10px);
margin-bottom: 15px;
color: var(--text-secondary);
}
/* Code blocks */
.code-block {
background: var(--code-bg);
border: 2px solid var(--accent-green);
padding: 15px;
margin: 20px 0;
font-family: 'Courier New', monospace;
font-size: clamp(8px, 1.5vw, 10px);
overflow-x: auto;
position: relative;
}
.code-block::before {
content: 'CODE';
position: absolute;
top: -12px;
left: 10px;
background: var(--accent-green);
color: var(--bg-dark);
padding: 2px 8px;
font-size: 8px;
font-family: 'Press Start 2P', monospace;
}
.code-line {
display: block;
margin: 5px 0;
}
.keyword { color: var(--accent-purple); }
.string { color: var(--accent-orange); }
.comment { color: var(--text-secondary); }
.number { color: var(--accent-cyan); }
/* Memory visualization */
.memory-viz {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(60px, 1fr));
gap: 5px;
margin: 20px 0;
padding: 20px;
background: var(--bg-dark);
border: 2px solid var(--accent-orange);
}
.memory-cell {
background: var(--accent-green);
color: var(--bg-dark);
padding: 10px;
text-align: center;
font-size: 8px;
font-weight: bold;
border: 1px solid var(--bg-dark);
animation: fadeIn 0.5s ease-out;
cursor: pointer;
transition: all 0.3s;
}
.memory-cell:hover {
transform: scale(1.1);
box-shadow: 0 0 10px var(--accent-green);
}
.memory-cell.row-1 {
background: var(--accent-orange);
}
@keyframes fadeIn {
from { opacity: 0; transform: scale(0.8); }
to { opacity: 1; transform: scale(1); }
}
/* Interactive elements */
.interactive-demo {
background: var(--bg-dark);
border: 2px solid var(--accent-purple);
padding: 20px;
margin: 20px 0;
}
.demo-controls {
display: flex;
gap: 10px;
margin-bottom: 15px;
flex-wrap: wrap;
}
.demo-btn {
background: var(--accent-purple);
color: white;
border: none;
padding: 8px 16px;
font-family: inherit;
font-size: 8px;
cursor: pointer;
transition: all 0.3s;
}
.demo-btn:hover {
background: var(--accent-orange);
transform: translateY(-2px);
}
.demo-output {
background: var(--code-bg);
border: 1px solid var(--accent-green);
padding: 10px;
min-height: 100px;
font-family: 'Courier New', monospace;
font-size: 10px;
color: var(--accent-green);
}
/* Comparison table */
.comparison-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
font-size: clamp(8px, 1.5vw, 10px);
}
.comparison-table th,
.comparison-table td {
border: 1px solid var(--border-color);
padding: 10px;
text-align: left;
}
.comparison-table th {
background: var(--accent-purple);
color: white;
}
.comparison-table td {
background: var(--bg-secondary);
}
.comparison-table tr:nth-child(even) td {
background: var(--bg-dark);
}
/* Performance meter */
.performance-meter {
background: var(--bg-dark);
border: 2px solid var(--accent-cyan);
padding: 15px;
margin: 20px 0;
}
.meter-bar {
width: 100%;
height: 20px;
background: var(--bg-secondary);
border: 1px solid var(--accent-cyan);
position: relative;
margin: 10px 0;
}
.meter-fill {
height: 100%;
background: linear-gradient(90deg, var(--accent-green), var(--accent-orange));
transition: width 1s ease-out;
}
.meter-label {
font-size: 8px;
color: var(--text-secondary);
margin-bottom: 5px;
}
/* Footer */
.footer {
text-align: center;
padding: 40px;
background: var(--bg-secondary);
border-top: 3px solid var(--accent-green);
margin-top: 40px;
}
.footer p {
font-size: 8px;
color: var(--text-secondary);
}
/* Responsive design */
@media (max-width: 768px) {
.container {
padding: 10px;
}
.section {
padding: 15px;
}
.memory-viz {
grid-template-columns: repeat(4, 1fr);
}
.demo-controls {
flex-direction: column;
}
}
/* Scrollbar styling */
::-webkit-scrollbar {
width: 8px;
}
::-webkit-scrollbar-track {
background: var(--bg-dark);
}
::-webkit-scrollbar-thumb {
background: var(--accent-green);
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: var(--accent-orange);
}
</style>
</head>
<body>
<div class="container">
<!-- Header -->
<header class="header">
<h1>MULTI-DIMENSIONAL ARRAYS</h1>
<p>The Ultimate Programming Guide</p>
</header>
<!-- Navigation -->
<nav class="nav">
<a href="#basics" class="nav-btn">BASICS</a>
<a href="#memory" class="nav-btn">MEMORY</a>
<a href="#allocation" class="nav-btn">ALLOCATION</a>
<a href="#languages" class="nav-btn">LANGUAGES</a>
<a href="#performance" class="nav-btn">PERFORMANCE</a>
<a href="#examples" class="nav-btn">EXAMPLES</a>
</nav>
<!-- Section 1: Basics -->
<section class="section" id="basics">
<h2>LEVEL 1: FUNDAMENTALS</h2>
<p>Multi-dimensional arrays are data structures that organize elements in multiple dimensions, creating matrices, cubes, and higher-dimensional structures. Think of them as grids within grids.</p>
<h3>What Are Multi-Dimensional Arrays?</h3>
<p>A multi-dimensional array is essentially an array of arrays. While a 1D array is like a list, a 2D array is like a table with rows and columns, and a 3D array is like a cube of data.</p>
<div class="code-block">
<span class="code-line"><span class="comment">// 1D Array - Single dimension</span></span>
<span class="code-line"><span class="keyword">int</span> array1D[<span class="number">5</span>] = {<span class="number">1, 2, 3, 4, 5</span>};</span>
<span class="code-line"></span>
<span class="code-line"><span class="comment">// 2D Array - Rows and columns</span></span>
<span class="code-line"><span class="keyword">int</span> array2D[<span class="number">3</span>][<span class="number">4</span>] = {</span>
<span class="code-line"> {<span class="number">1, 2, 3, 4</span>},</span>
<span class="code-line"> {<span class="number">5, 6, 7, 8</span>},</span>
<span class="code-line"> {<span class="number">9, 10, 11, 12</span>}</span>
<span class="code-line">};</span>
<span class="code-line"></span>
<span class="code-line"><span class="comment">// 3D Array - Depth, rows, and columns</span></span>
<span class="code-line"><span class="keyword">int</span> array3D[<span class="number">2</span>][<span class="number">3</span>][<span class="number">4</span>];</span>
</div>
<div class="interactive-demo">
<h3>Interactive Array Visualizer</h3>
<div class="demo-controls">
<button class="demo-btn" onclick="visualizeArray('1D')">1D Array</button>
<button class="demo-btn" onclick="visualizeArray('2D')">2D Array</button>
<button class="demo-btn" onclick="visualizeArray('3D')">3D Array</button>
</div>
<div class="demo-output" id="array-output">
Click a button to visualize different array types!
</div>
</div>
</section>
<!-- Section 2: Memory Layout -->
<section class="section" id="memory">
<h2>LEVEL 2: MEMORY LAYOUT</h2>
<p>Understanding how multi-dimensional arrays are stored in memory is crucial for writing efficient code and avoiding common pitfalls.</p>
<h3>Row-Major vs Column-Major Order</h3>
<p>Different programming languages store 2D arrays in memory differently. C, C++, and most languages use row-major order, while Fortran uses column-major order.</p>
<div class="code-block">
<span class="code-line"><span class="comment">// C uses row-major order</span></span>
<span class="code-line"><span class="keyword">char</span> ma[<span class="number">2</span>][<span class="number">4</span>];</span>
<span class="code-line">ma[<span class="number">0</span>][<span class="number">0</span>] = <span class="string">'a'</span>;</span>
<span class="code-line">ma[<span class="number">0</span>][<span class="number">1</span>] = <span class="string">'b'</span>;</span>
<span class="code-line">ma[<span class="number">0</span>][<span class="number">2</span>] = <span class="string">'c'</span>;</span>
<span class="code-line">ma[<span class="number">0</span>][<span class="number">3</span>] = <span class="string">'d'</span>;</span>
<span class="code-line">ma[<span class="number">1</span>][<span class="number">0</span>] = <span class="string">'e'</span>;</span>
<span class="code-line">ma[<span class="number">1</span>][<span class="number">1</span>] = <span class="string">'f'</span>;</span>
<span class="code-line">ma[<span class="number">1</span>][<span class="number">2</span>] = <span class="string">'g'</span>;</span>
<span class="code-line">ma[<span class="number">1</span>][<span class="number">3</span>] = <span class="string">'\0'</span>;</span>
</div>
<h3>Memory Visualization</h3>
<p>This is how the array above is laid out in memory (row-major order):</p>
<div class="memory-viz" id="memory-display">
<div class="memory-cell">a<br>ma[0][0]</div>
<div class="memory-cell">b<br>ma[0][1]</div>
<div class="memory-cell">c<br>ma[0][2]</div>
<div class="memory-cell">d<br>ma[0][3]</div>
<div class="memory-cell row-1">e<br>ma[1][0]</div>
<div class="memory-cell row-1">f<br>ma[1][1]</div>
<div class="memory-cell row-1">g<br>ma[1][2]</div>
<div class="memory-cell row-1">\0<br>ma[1][3]</div>
</div>
<div class="code-block">
<span class="code-line"><span class="comment">// Pointer arithmetic with 2D arrays</span></span>
<span class="code-line"><span class="keyword">char</span>* p = ma;</span>
<span class="code-line"><span class="keyword">while</span> (*p) {</span>
<span class="code-line"> printf(<span class="string">"%c"</span>, *p);</span>
<span class="code-line"> p++; <span class="comment">// Move to next memory location</span></span>
<span class="code-line">}</span>
<span class="code-line"><span class="comment">// Output: abcdefg</span></span>
</div>
</section>
<!-- Section 3: Allocation Types -->
<section class="section" id="allocation">
<h2>LEVEL 3: ALLOCATION STRATEGIES</h2>
<p>Multi-dimensional arrays can be allocated in two fundamentally different ways, each with distinct performance characteristics.</p>
<h3>Static vs Dynamic Allocation</h3>
<table class="comparison-table">
<thead>
<tr>
<th>Allocation Type</th>
<th>Memory Layout</th>
<th>Performance</th>
<th>Flexibility</th>
</tr>
</thead>
<tbody>
<tr>
<td>Static (Contiguous)</td>
<td>Single memory block</td>
<td>Fastest access</td>
<td>Fixed size</td>
</tr>
<tr>
<td>Dynamic (Fragmented)</td>
<td>Multiple memory blocks</td>
<td>Slower due to indirection</td>
<td>Variable size</td>
</tr>
</tbody>
</table>
<h3>Contiguous Allocation</h3>
<div class="code-block">
<span class="code-line"><span class="comment">// Static allocation - contiguous memory</span></span>
<span class="code-line"><span class="keyword">int</span> matrix[<span class="number">1000</span>][<span class="number">1000</span>]; <span class="comment">// All elements in one block</span></span>
<span class="code-line"></span>
<span class="code-line"><span class="comment">// Dynamic contiguous allocation</span></span>
<span class="code-line"><span class="keyword">int</span>* matrix = (<span class="keyword">int</span>*)malloc(<span class="number">1000</span> * <span class="number">1000</span> * <span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span>
<span class="code-line"><span class="comment">// Access: matrix[i * cols + j]</span></span>
</div>
<h3>Fragmented Allocation</h3>
<div class="code-block">
<span class="code-line"><span class="comment">// Dynamic fragmented allocation</span></span>
<span class="code-line"><span class="keyword">int</span>** matrix = (<span class="keyword">int</span>**)malloc(<span class="number">1000</span> * <span class="keyword">sizeof</span>(<span class="keyword">int</span>*));</span>
<span class="code-line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">1000</span>; i++) {</span>
<span class="code-line"> matrix[i] = (<span class="keyword">int</span>*)malloc(<span class="number">1000</span> * <span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span>
<span class="code-line">}</span>
<span class="code-line"><span class="comment">// Access: matrix[i][j]</span></span>
</div>
<div class="performance-meter">
<h3>Performance Comparison</h3>
<div class="meter-label">Cache Efficiency</div>
<div class="meter-bar">
<div class="meter-fill" style="width: 90%" id="cache-meter"></div>
</div>
<div class="meter-label">Memory Overhead</div>
<div class="meter-bar">
<div class="meter-fill" style="width: 20%" id="overhead-meter"></div>
</div>
<div class="meter-label">Allocation Speed</div>
<div class="meter-bar">
<div class="meter-fill" style="width: 85%" id="speed-meter"></div>
</div>
<p style="font-size: 8px; margin-top: 10px;">Values shown for contiguous allocation. Click to compare with fragmented allocation.</p>
</div>
</section>
<!-- Section 4: Language Specifics -->
<section class="section" id="languages">
<h2>LEVEL 4: LANGUAGE IMPLEMENTATIONS</h2>
<p>Different programming languages handle multi-dimensional arrays in unique ways, each with their own syntax and optimizations.</p>
<h3>C/C++</h3>
<div class="code-block">
<span class="code-line"><span class="comment">// Static 2D array in C</span></span>
<span class="code-line"><span class="keyword">int</span> matrix[<span class="number">3</span>][<span class="number">4</span>] = {</span>
<span class="code-line"> {<span class="number">1, 2, 3, 4</span>},</span>
<span class="code-line"> {<span class="number">5, 6, 7, 8</span>},</span>
<span class="code-line"> {<span class="number">9, 10, 11, 12</span>}</span>
<span class="code-line">};</span>
<span class="code-line"></span>
<span class="code-line"><span class="comment">// Dynamic allocation with malloc</span></span>
<span class="code-line"><span class="keyword">int</span>** create2DArray(<span class="keyword">int</span> rows, <span class="keyword">int</span> cols) {</span>
<span class="code-line"> <span class="keyword">int</span>** arr = malloc(rows * <span class="keyword">sizeof</span>(<span class="keyword">int</span>*));</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < rows; i++)</span>
<span class="code-line"> arr[i] = malloc(cols * <span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span>
<span class="code-line"> <span class="keyword">return</span> arr;</span>
<span class="code-line">}</span>
</div>
<h3>Python</h3>
<div class="code-block">
<span class="code-line"><span class="comment"># List comprehension for 2D array</span></span>
<span class="code-line">matrix = [[<span class="number">0</span> <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">4</span>)] <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">3</span>)]</span>
<span class="code-line"></span>
<span class="code-line"><span class="comment"># NumPy arrays (much more efficient)</span></span>
<span class="code-line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span>
<span class="code-line">matrix = np.zeros((<span class="number">3, 4</span>), dtype=np.int32)</span>
<span class="code-line">matrix = np.array([[<span class="number">1, 2, 3, 4</span>],</span>
<span class="code-line"> [<span class="number">5, 6, 7, 8</span>],</span>
<span class="code-line"> [<span class="number">9, 10, 11, 12</span>]])</span>
</div>
<h3>Java</h3>
<div class="code-block">
<span class="code-line"><span class="comment">// Java 2D array declaration and initialization</span></span>
<span class="code-line"><span class="keyword">int</span>[][] matrix = <span class="keyword">new int</span>[<span class="number">3</span>][<span class="number">4</span>];</span>
<span class="code-line"></span>
<span class="code-line"><span class="comment">// Jagged arrays (different row sizes)</span></span>
<span class="code-line"><span class="keyword">int</span>[][] jaggedArray = <span class="keyword">new int</span>[<span class="number">3</span>][];</span>
<span class="code-line">jaggedArray[<span class="number">0</span>] = <span class="keyword">new int</span>[<span class="number">2</span>];</span>
<span class="code-line">jaggedArray[<span class="number">1</span>] = <span class="keyword">new int</span>[<span class="number">4</span>];</span>
<span class="code-line">jaggedArray[<span class="number">2</span>] = <span class="keyword">new int</span>[<span class="number">3</span>];</span>
</div>
<h3>JavaScript</h3>
<div class="code-block">
<span class="code-line"><span class="comment">// JavaScript 2D array using Array constructor</span></span>
<span class="code-line"><span class="keyword">const</span> matrix = <span class="keyword">new</span> Array(<span class="number">3</span>).fill(<span class="keyword">null</span>).map(() => <span class="keyword">new</span> Array(<span class="number">4</span>).fill(<span class="number">0</span>));</span>
<span class="code-line"></span>
<span class="code-line"><span class="comment">// Literal notation</span></span>
<span class="code-line"><span class="keyword">const</span> matrix2 = [</span>
<span class="code-line"> [<span class="number">1, 2, 3, 4</span>],</span>
<span class="code-line"> [<span class="number">5, 6, 7, 8</span>],</span>
<span class="code-line"> [<span class="number">9, 10, 11, 12</span>]</span>
<span class="code-line">];</span>
</div>
</section>
<!-- Section 5: Performance -->
<section class="section" id="performance">
<h2>LEVEL 5: PERFORMANCE OPTIMIZATION</h2>
<p>Understanding performance characteristics of multi-dimensional arrays is crucial for writing efficient algorithms.</p>
<h3>Cache Locality</h3>
<p>The way you access array elements significantly impacts performance due to CPU cache behavior.</p>
<div class="code-block">
<span class="code-line"><span class="comment">// Good: Row-major access pattern</span></span>
<span class="code-line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < rows; i++) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < cols; j++) {</span>
<span class="code-line"> matrix[i][j] = i * j; <span class="comment">// Sequential memory access</span></span>
<span class="code-line"> }</span>
<span class="code-line">}</span>
<span class="code-line"></span>
<span class="code-line"><span class="comment">// Bad: Column-major access pattern (for row-major storage)</span></span>
<span class="code-line"><span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < cols; j++) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < rows; i++) {</span>
<span class="code-line"> matrix[i][j] = i * j; <span class="comment">// Strided memory access</span></span>
<span class="code-line"> }</span>
<span class="code-line">}</span>
</div>
<h3>Memory Bandwidth Utilization</h3>
<div class="interactive-demo">
<h3>Performance Test</h3>
<div class="demo-controls">
<button class="demo-btn" onclick="performanceTest('row-major')">Test Row-Major Access</button>
<button class="demo-btn" onclick="performanceTest('column-major')">Test Column-Major Access</button>
<button class="demo-btn" onclick="performanceTest('random')">Test Random Access</button>
</div>
<div class="demo-output" id="performance-output">
Run performance tests to see the difference in access patterns!
</div>
</div>
<h3>Optimization Techniques</h3>
<div class="code-block">
<span class="code-line"><span class="comment">// Loop blocking/tiling for better cache usage</span></span>
<span class="code-line"><span class="keyword">void</span> matrixMultiplyBlocked(<span class="keyword">int</span>** A, <span class="keyword">int</span>** B, <span class="keyword">int</span>** C, <span class="keyword">int</span> n) {</span>
<span class="code-line"> <span class="keyword">int</span> blockSize = <span class="number">64</span>; <span class="comment">// Optimize for cache line size</span></span>
<span class="code-line"> </span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> ii = <span class="number">0</span>; ii < n; ii += blockSize) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> jj = <span class="number">0</span>; jj < n; jj += blockSize) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> kk = <span class="number">0</span>; kk < n; kk += blockSize) {</span>
<span class="code-line"> <span class="comment">// Process block</span></span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = ii; i < min(ii + blockSize, n); i++) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = jj; j < min(jj + blockSize, n); j++) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = kk; k < min(kk + blockSize, n); k++) {</span>
<span class="code-line"> C[i][j] += A[i][k] * B[k][j];</span>
<span class="code-line"> }</span>
<span class="code-line"> }</span>
<span class="code-line"> }</span>
<span class="code-line"> }</span>
<span class="code-line"> }</span>
<span class="code-line"> }</span>
<span class="code-line">}</span>
</div>
</section>
<!-- Section 6: Examples -->
<section class="section" id="examples">
<h2>LEVEL 6: PRACTICAL EXAMPLES</h2>
<p>Real-world applications and common algorithms using multi-dimensional arrays.</p>
<h3>Matrix Operations</h3>
<div class="code-block">
<span class="code-line"><span class="comment">// Matrix multiplication</span></span>
<span class="code-line"><span class="keyword">void</span> matrixMultiply(<span class="keyword">int</span>** A, <span class="keyword">int</span>** B, <span class="keyword">int</span>** C, <span class="keyword">int</span> n) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < n; j++) {</span>
<span class="code-line"> C[i][j] = <span class="number">0</span>;</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">0</span>; k < n; k++) {</span>
<span class="code-line"> C[i][j] += A[i][k] * B[k][j];</span>
<span class="code-line"> }</span>
<span class="code-line"> }</span>
<span class="code-line"> }</span>
<span class="code-line">}</span>
</div>
<h3>Image Processing</h3>
<div class="code-block">
<span class="code-line"><span class="comment">// 3D array for RGB image (height x width x channels)</span></span>
<span class="code-line"><span class="keyword">typedef struct</span> {</span>
<span class="code-line"> <span class="keyword">unsigned char</span> r, g, b;</span>
<span class="code-line">} Pixel;</span>
<span class="code-line"></span>
<span class="code-line">Pixel image[<span class="number">1080</span>][<span class="number">1920</span>]; <span class="comment">// HD image</span></span>
<span class="code-line"></span>
<span class="code-line"><span class="comment">// Apply blur filter</span></span>
<span class="code-line"><span class="keyword">void</span> blurImage(Pixel src[][<span class="number">1920</span>], Pixel dst[][<span class="number">1920</span>], <span class="keyword">int</span> height, <span class="keyword">int</span> width) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i < height - <span class="number">1</span>; i++) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j < width - <span class="number">1</span>; j++) {</span>
<span class="code-line"> <span class="comment">// 3x3 blur kernel</span></span>
<span class="code-line"> <span class="keyword">int</span> r = <span class="number">0</span>, g = <span class="number">0</span>, b = <span class="number">0</span>;</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> di = -<span class="number">1</span>; di <= <span class="number">1</span>; di++) {</span>
<span class="code-line"> <span class="keyword">for</span>(<span class="keyword">int</span> dj = -<span class="number">1</span>; dj <= <span class="number">1</span>; dj++) {</span>
<span class="code-line"> r += src[i + di][j + dj].r;</span>
<span class="code-line"> g += src[i + di][j + dj].g;</span>
<span class="code-line"> b += src[i + di][j + dj].b;</span>
<span class="code-line"> }</span>
<span class="code-line"> }</span>
<span class="code-line"> dst[i][j].r = r / <span class="number">9</span>;</span>
<span class="code-line"> dst[i][j].g = g / <span class="number">9</span>;</span>
<span class="code-line"> dst[i][j].b = b / <span class="number">9</span>;</span>
<span class="code-line"> }</span>
<span class="code-line"> }</span>
<span class="code-line">}</span>
</div>
<h3>Game Development: 2D Grid</h3>
<div class="code-block">
<span class="code-line"><span class="comment">// Game world represented as 2D grid</span></span>
<span class="code-line"><span class="keyword">typedef enum</span> { EMPTY, WALL, PLAYER, ENEMY, TREASURE } CellType;</span>
<span class="code-line"></span>
<span class="code-line">CellType gameWorld[<span class="number">100</span>][<span class="number">100</span>];</span>
<span class="code-line"></span>
<span class="code-line"><span class="comment">// Pathfinding with A* algorithm</span></span>
<span class="code-line"><span class="keyword">int</span> findPath(<span class="keyword">int</span> startX, <span class="keyword">int</span> startY, <span class="keyword">int</span> endX, <span class="keyword">int</span> endY) {</span>
<span class="code-line"> <span class="comment">// Implementation would use 2D arrays for:</span></span>
<span class="code-line"> <span class="comment">// - Distance map</span></span>
<span class="code-line"> <span class="comment">// - Visited cells</span></span>
<span class="code-line"> <span class="comment">// - Parent pointers for path reconstruction</span></span>
<span class="code-line"> <span class="keyword">return</span> <span class="number">1</span>; <span class="comment">// Path found</span></span>
<span class="code-line">}</span>
</div>
<div class="interactive-demo">
<h3>Array Algorithm Simulator</h3>
<div class="demo-controls">
<button class="demo-btn" onclick="simulateAlgorithm('transpose')">Matrix Transpose</button>
<button class="demo-btn" onclick="simulateAlgorithm('spiral')">Spiral Traversal</button>
<button class="demo-btn" onclick="simulateAlgorithm('search')">2D Binary Search</button>
</div>
<div class="demo-output" id="algorithm-output">
Choose an algorithm to see it in action!
</div>
</div>
</section>
<!-- Footer -->
<footer class="footer">
<p>GAME OVER - YOU'VE MASTERED MULTI-DIMENSIONAL ARRAYS!</p>
<p>Remember: With great arrays comes great responsibility for memory management.</p>
</footer>
</div>
<script>
// Interactive functionality
function visualizeArray(type) {
const output = document.getElementById('array-output');
let content = '';
switch(type) {
case '1D':
content = `
1D Array Visualization:
[0] [1] [2] [3] [4]
1 2 3 4 5
Memory addresses are sequential:
Address: 1000 1004 1008 1012 1016
`;
break;
case '2D':
content = `
2D Array Visualization:
Row 0: [1] [2] [3] [4]
Row 1: [5] [6] [7] [8]
Row 2: [9] [10][11][12]
Memory layout (row-major):
1 2 3 4 5 6 7 8 9 10 11 12
`;
break;
case '3D':
content = `
3D Array Visualization:
Depth 0: Depth 1:
[1][2] [9][10]
[3][4] [11][12]
[5][6] [13][14]
[7][8] [15][16]
Think of it as layers of 2D arrays!
`;
break;
}
output.textContent = content;
output.style.animation = 'none';
setTimeout(() => output.style.animation = 'fadeIn 0.5s ease-out', 10);
}
function performanceTest(pattern) {
const output = document.getElementById('performance-output');
let content = '';
switch(pattern) {
case 'row-major':
content = `
Row-Major Access Test:
=====================
Pattern: for(i) for(j) access[i][j]
Cache hits: ████████████████████ 95%
Execution time: ████ 1.2ms
Memory bandwidth: ████████████████ 80%
Result: OPTIMAL - Sequential memory access
maximizes cache efficiency!
`;
break;
case 'column-major':
content = `
Column-Major Access Test:
========================
Pattern: for(j) for(i) access[i][j]
Cache hits: ████████ 40%
Execution time: ████████████ 3.8ms
Memory bandwidth: ████████ 35%
Result: SUBOPTIMAL - Strided access pattern
causes cache misses!
`;
break;
case 'random':
content = `
Random Access Test:
==================
Pattern: random[i][j] access
Cache hits: ████ 15%
Execution time: ████████████████████ 8.9ms
Memory bandwidth: ████ 18%
Result: POOR - Unpredictable access pattern
defeats all caching mechanisms!
`;
break;
}
output.textContent = content;
}
function simulateAlgorithm(algorithm) {
const output = document.getElementById('algorithm-output');
let content = '';
switch(algorithm) {
case 'transpose':
content = `
Matrix Transpose Simulation:
===========================
Original: Transposed:
1 2 3 4 1 5 9
5 6 7 8 -> 2 6 10
9 10 11 12 3 7 11
4 8 12
Algorithm: for each (i,j) swap matrix[i][j] with matrix[j][i]
Time Complexity: O(n²)
Space Complexity: O(1) in-place
`;
break;
case 'spiral':
content = `
Spiral Traversal Simulation:
============================
Matrix: Traversal Order:
1 2 3 4 1→ 2→ 3→ 4
5 6 7 8 ↓
9 10 11 12 5→ 6→ 7 8
13 14 15 16 ↑ ↓
9← 10← 11← 12
Output: 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10
Key: Track boundaries and adjust direction!
`;
break;
case 'search':
content = `
2D Binary Search Simulation:
============================
Sorted Matrix: Target: 11
1 4 7 11
2 5 8 12 Start at top-right: 11
3 6 9 16 Found target immediately!
Algorithm:
1. Start at top-right corner
2. If target < current: move left
3. If target > current: move down
4. If equal: found!
Time Complexity: O(m + n)
`;
break;
}
output.textContent = content;
}
// Add click effects to memory cells
document.querySelectorAll('.memory-cell').forEach(cell => {
cell.addEventListener('click', function() {
this.style.transform = 'scale(1.2)';
this.style.boxShadow = '0 0 20px var(--accent-green)';
setTimeout(() => {
this.style.transform = 'scale(1)';
this.style.boxShadow = '0 0 10px var(--accent-green)';
}, 200);
});
});
// Smooth scrolling for navigation
document.querySelectorAll('.nav-btn').forEach(btn => {
btn.addEventListener('click', function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// Performance meter animation
document.querySelector('.performance-meter').addEventListener('click', function() {
const fills = this.querySelectorAll('.meter-fill');
const fragmented = [60, 80, 45]; // Performance values for fragmented allocation
const contiguous = [90, 20, 85]; // Performance values for contiguous allocation
fills.forEach((fill, index) => {
const currentWidth = parseInt(fill.style.width);
const isContiguous = currentWidth === contiguous[index];
const newWidth = isContiguous ? fragmented[index] : contiguous[index];
fill.style.width = newWidth + '%';
});
const label = this.querySelector('p');
if (label.textContent.includes('contiguous')) {
label.textContent = 'Values shown for fragmented allocation. Click to compare with contiguous allocation.';
} else {
label.textContent = 'Values shown for contiguous allocation. Click to compare with fragmented allocation.';
}
});
// Add loading animations
window.addEventListener('load', function() {
const sections = document.querySelectorAll('.section');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.animationDelay = '0.1s';
entry.target.style.animationName = 'slideInLeft';
}
});
});
sections.forEach(section => observer.observe(section));
});
// Easter egg: Konami code
let konamiCode = [];
const correctCode = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'KeyB', 'KeyA'];
document.addEventListener('keydown', function(e) {
konamiCode.push(e.code);
if (konamiCode.length > correctCode.length) {
konamiCode.shift();
}
if (JSON.stringify(konamiCode) === JSON.stringify(correctCode)) {
document.body.style.animation = 'rainbow 2s ease-in-out infinite';
setTimeout(() => {
document.body.style.animation = '';
alert('🎮 CHEAT CODE ACTIVATED! You found the secret! 🎮');
}, 4000);
}
});
// Add rainbow animation
const style = document.createElement('style');
style.textContent = `
@keyframes rainbow {
0% { filter: hue-rotate(0deg); }
100% { filter: hue-rotate(360deg); }
}
`;
document.head.appendChild(style);
</script>
</body>
</html>