C Programming Array Lab
See the memory. Follow the index. Understand the array.
An interactive, browser-based lab that teaches C arrays, 2-D arrays and strings by making memory visible. Students pick a lesson, read the C code, and run it one operation at a time. They see:
- every array element as a box with its index, value and (illustrative) address;
- exactly which element each step reads or writes, and how its index was worked out (
a[i] → a[2] → 30);
- the variables, the output and a dry-run trace, all built from the same execution.
Built for first-year BCA, BSc IT and BSc Data Science students, and for teachers demonstrating on a projector. It is the second lab in a series with C Programming Pattern Lab.
Idea, design and development: Hitesh Patel: Assistant Professor and Coordinator of BSc Data Science, and Founder of Hi-Tech AI Solutions & Services. All creator and organisation details shown in the app live in src/branding.ts.
The specification is docs/PRD.md. Project status, decisions and the workflow for future changes are in docs/PROGRESS.md.
▶ Live app: c-programming-array-lab.vercel.app · free, no login, works on phones
Features (Version 1)
- 26 lessons in three modules (see the table below), including two "Mistake lab" lessons that stop at undefined behaviour.
- Memory view
- 1-D arrays as a row of boxes (a vertical memory strip on phones), with index markers such as
i = 2 or len - 1 - i.
- 2-D arrays as a grid and a row-major memory strip side by side, with the same element highlighted in both.
- Strings show each character with its ASCII code, the
'\0' terminator, and the unused slots (collapsible).
- Element states are written in words as well as colours: READ, WRITE (
7 → 12), AUTO 0 (set by a partial initialiser), ? for indeterminate, end for '\0'.
- Illustrative addresses with the worked formula (
&m[1][2] = 1000 + (1 × 3 + 2) × 4 = 1020) and a 4-byte / 2-byte (Turbo C) int switch.
- Undefined behaviour is never faked. Going out of bounds, reading an indeterminate value,
%s without '\0', overflow and division by zero stop the simulator with a plain explanation and a red "outside the array" box.
- Same simulator features as Pattern Lab
- Code highlight, step explanation (including which parts of
&& / || were checked), variables with counters (reads, writes, comparisons, swaps), and a simulated keyboard for scanf.
- Output panel with a terminal view of the typed input.
- Dry-run trace.
- Controls: Next, Previous, Auto Run, Pause, Reset, End, four speeds, and the ← → keys.
- Settings (array values, search key, matrices, text) are validated, capped and shareable in the URL, e.g.
/lessons/palindrome?text=RACECAR.
- Prediction mode, quizzes with hints (82 questions) and a Practice zone.
- Space-lab design, the same as Pattern Lab:
- mobile-first from 320px to 4K, with no sideways scrolling;
- bundled fonts, code ligatures off;
- reduced-motion support;
- accessible labels.
- Share previews and installable app: Open Graph / Twitter tags,
public/og-image.jpg (1200×630, about 97 KB), icons, manifest, robots.txt and sitemap.xml.
- Privacy: no login, no cookies, no personal data, no visitor counting (yet). Progress lives only in
localStorage.
Lessons
| # |
Lesson |
Module |
Default output |
| 1 |
Declaring & initialising arrays |
1-D |
a[2] = 30 / b[3] = 0 / c[0] = 99 |
| 2 |
Reading values with scanf |
1-D |
You entered: 12 7 30 4 19 |
| 3 |
Printing every element |
1-D |
a[0] = 12 … a[4] = 19 |
| 4 |
Sum and average |
1-D |
Sum = 72 / Average = 14.40 |
| 5 |
Largest and smallest |
1-D |
Maximum = 30 / Minimum = 4 |
| 6 |
Printing in reverse |
1-D |
19 4 30 7 12 |
| 7 |
Linear search |
1-D |
30 found at index 2 |
| 8 |
Counting even and odd |
1-D |
Even = 3 / Odd = 2 |
| 9 |
Copying an array |
1-D |
12 7 30 4 19 |
| 10 |
Bubble sort |
1-D |
4 7 12 19 30 |
| 11 |
⚠ Mistake: going past the end |
1-D |
stops at a[5] |
| 12 |
Declaring a 2-D array |
2-D |
m[1][2] = 6 / z[0][1] = 0 / z[1][1] = 5 |
| 13 |
Printing a matrix |
2-D |
1 2 3 / 4 5 6 |
| 14 |
Reading a matrix |
2-D |
Matrix: / 1 2 3 / 4 5 6 |
| 15 |
Row sums and column sums |
2-D |
Row 0 sum = 6 … Column 2 sum = 9 |
| 16 |
Adding two matrices |
2-D |
6 8 / 10 12 |
| 17 |
Transpose |
2-D |
1 4 / 2 5 / 3 6 |
| 18 |
Matrix multiplication |
2-D |
19 22 / 43 50 |
| 19 |
Characters, ASCII and '\0' |
Strings |
HELLO / E / 69 |
| 20 |
⚠ Mistake: no room for '\0' |
Strings |
HELLO, then stops |
| 21 |
Length with a while loop |
Strings |
Length = 7 |
| 22 |
Printing a string in reverse |
Strings |
OLLEH |
| 23 |
Counting vowels |
Strings |
Vowels = 5 |
| 24 |
Lowercase to uppercase |
Strings |
HELLO WORLD |
| 25 |
Palindrome check |
Strings |
LEVEL is a palindrome |
| 26 |
%c vs %s vs %d |
Strings |
CODE / C O D E / 67 79 68 69 |
The full C source of every lesson is in docs/PRD.md (Appendix A), and a test checks that the app renders exactly that code.
How the simulator works
Lesson settings ─ validateSettings() ─▶ lesson.build(settings)
│ a typed statement tree (no C parsing)
├─ renderSource() → the exact C code shown, with highlightable parts
└─ buildTimeline() → typed events, each with its array reads/writes
│ buildStates() → one immutable snapshot per step
▼
useSimulation(position) → every panel reads the same snapshot
- No C parsing and no
eval. The same tree renders the code and runs it, so they cannot disagree.
- Events:
INIT, ASSIGN, ARRAY_WRITE, INPUT, CONDITION_CHECK, INCREMENT, DECREMENT, BREAK, OUTPUT, NEWLINE, UNDEFINED_BEHAVIOUR, PROGRAM_END.
- Each event carries the code part and line, the variables before and after, every array read and write (expression, evaluated index, old and new value) and a plain-English explanation.
- Step policy:
- Step 0 means "storage exists, nothing set".
- A declaration with an initialiser is a step; one without is not.
- After that, one statement or one loop-header part is one step.
- Reads belong to the step that performs them.
- A false condition is the moment a loop ends or an
if is skipped.
- C rules implemented exactly:
- partial initialisers and short strings are zero-filled;
/ truncates towards zero and % takes the sign of the dividend;
&& / || short-circuit;
%.2f is rounded from the exact float value with ties to even (gcc prints (float)1/8 as 0.12; JavaScript's toFixed would give 0.13);
- every index is checked against its own dimension.
| Area |
Files |
| Engine |
src/engine/ (types, renderSource, buildTimeline, replayState, executeLesson, settings, memory, values) |
| Lessons |
src/lessons/ (oneD.ts, twoD.ts, strings.ts, builders.ts, reference.ts, index.ts) |
| Quiz |
src/quiz/ (types, questions, predictions) |
| UI |
src/components/ (MemoryView, SimulatorWorkspace, CodePanel, ScalarInspector, InputPanel, OutputPanel, DryRunTable, …), src/pages/, src/hooks/ |
| Branding |
src/branding.ts (creator, organisation, the lab series) |
Local development
Requirements: Node.js 22.12 or newer (developed on Node 24 LTS). Optional: gcc for the real-compiler check.
Stack: React 19, TypeScript 6 (strict), Vite 8, Tailwind CSS 4, React Router 7, Vitest 5 + React Testing Library + jsdom, ESLint 10.
| Command |
What it does |
npm run dev |
Start the Vite dev server (http://localhost:5173) |
npm test |
Run all tests once |
npm run lint |
ESLint (including a ban on eval / new Function) |
npm run typecheck |
TypeScript strict type check |
npm run build |
Type-check and build into dist/ |
npm run preview |
Serve the production build |
npm run verify:c |
Compile every lesson with gcc and compare its output with the simulator (skipped if gcc is missing) |
node docs/assets-src/check-overflow.mjs <url> |
Check every page for sideways scrolling and console errors at 9 widths (320–2560px) |
Tests (298, plus the gcc check)
| Kind |
What it checks |
| Control-flow oracle |
For every lesson at 40+ settings, the code part after each step is the one C must run next, worked out from the program structure (for, while, if/else, break) |
| Independent references |
Each lesson's output is recomputed in plain TypeScript and compared, including hand-picked edge cases (exact %.2f ties, negatives, duplicates, palindromes) |
| Exact sources |
The rendered C code equals Appendix A of the PRD character for character; the default outputs equal hand-written ones |
| Undefined behaviour |
Regular lessons never reach it at any tested setting; mistake lessons always do, at the expected element; unit tests cover every kind |
| Memory and snapshots |
Writes change only their own cell, reads change nothing, replaying from step 0 equals every snapshot |
| Quizzes |
41 of the 82 answers re-checked by the engine; prediction answers always come from the real next event |
| UI |
Every step forwards and back with each panel compared to the engine; Auto Run and Pause; settings and URL; mistake stop; keyboard input; 2-D views; pages |
| Real compiler |
npm run verify:c compiles each regular lesson with gcc -std=c11 -Wall -Wextra -pedantic -Werror for several settings and compares stdout byte for byte |
| Mutation checks |
Nine deliberate engine bugs (e.g. column-major order, no short-circuit, toFixed rounding, a missing bounds check) are each caught by the tests |
Deployment
GitHub sbccas/c-programming-array-lab → Vercel (Vite preset, npm run build, dist, Node 24.x). Every push to main deploys to https://c-programming-array-lab.vercel.app; other branches get preview URLs. vercel.json rewrites every path except /api/ to index.html. See docs/PROGRESS.md for the workflow.
Limitations
- Only the included, verified programs can be simulated; the code cannot be edited.
- Settings are capped: 10 array values, 4 × 4 matrices, 15 characters of plain ASCII text.
- Addresses are illustrative.
- Not yet included: the
string.h lessons (planned for V1.1), pointers, functions, visitor counting.
- Progress is stored only in the current browser.