cp1-lab variables · control flow · functions · pointers · data structures

1. Variable tracing

Step through a short C program line by line and watch each variable's value change. Tracing by hand is the single most useful debugging habit — the highlighted line is the one about to run, the table shows the state after it executes.

line0
statusready

Each program is pure integer arithmetic, no I/O.

2. Expressions & operator precedence

Type an arithmetic expression in the C grammar (+ - * / % ( )). It is parsed into a tree showing the order of evaluation, and evaluated under two type regimes — int (truncating / and %) versus double.

int result
double result

3. Loops — the iteration table

A counted loop with its three parts: init ; condition ; update. Each row of the trace is one pass of the loop body; the bar chart shows the accumulator growing. Change the bounds and watch how many times the body actually runs.

iterations
final value

4. Recursion & the call stack

A recursive function calls itself on a smaller input until it hits a base case, then the results unwind back up the stack. Watch the call tree expand and the stack grow and shrink. fib shows why naive recursion is expensive (repeated subproblems).

result
calls made
max depth

Tree nodes are calls; leaves are base cases.

5. Pointers & memory

Memory is a row of numbered cells. A pointer is just a variable that holds an address. &x takes an address, *p follows one. Move the pointer and step it with pointer arithmetic — p + 1 advances by one element, not one byte.

p
*p
*(p + 1)

int is 4 bytes; addresses shown in hex.

7. Sorting algorithms

Bars are array values. Press play to animate a quadratic sort: bubble, selection or insertion. The two highlighted bars are the pair being compared; counters track comparisons and swaps so you can feel the cost.

comparisons0
swaps0

8. Linked lists

A singly linked list: each node stores a value and a pointer to the next node. Insert at the head or tail, delete by value, and watch the next pointers re-wire. Unlike arrays, insertion is $O(1)$ but there is no random access.

length0

head → … → NULL. Pointers drawn as arrows.

9. Bit manipulation

An integer as raw bits. Read it in binary / octal / hex, toggle two's-complement signed mode, and apply a bitwise operator against a mask to see & | ^ << in action. Click any bit to flip it.

binary
hex
result

10. Backtracking — the N-Queens search

Place $N$ queens so none attack each other. The solver places one queen per column, backtracking whenever a column has no safe row. Step through to see the systematic try / fail / undo rhythm that defines backtracking.

queens placed0
backtracks0
status

11. Structs & memory layout

A struct groups fields in memory. The compiler aligns each field to its size, inserting hidden padding bytes. Reorder the fields to see how layout and total sizeof change — biggest-first is usually most compact.

data bytes
padding
sizeof

char 1 · short 2 · int 4 · double 8 bytes.