Most array code is four shapes in a trench coat: running totals, best-so-far scans, counting matches and two-index swaps.
1 Running total
total = total + a[i] inside a loop — sums, averages and shopping carts all start here.
2 Best so far
Keep a champion in a variable and replace it whenever a[i] beats it.
3 Two indexes
One finger at each end, walking inward — the heart of reversing and many searches.
4 Counting matches
A counter that grows by 1 each time a condition is true.
Patterns you will reuse forever
Running total1total = total + a[i] inside a loop — sums, averages and shopping carts all start here. Best so far2Keep a champion in a variable and replace it whenever a[i] beats it. Two indexes3One finger at each end, walking inward — the heart of reversing and many searches. Counting matches4A counter that grows by 1 each time a condition is true.
4 building blocks make up Patterns you will reuse forever
Press play — two fingers swap the ends, step inward, and stop the moment they meet.
preprocessThe preprocessor pastes in stdio.h.
Memory
no variables yet — press play
Console
output appears as the program runs…
One pass, one champion: every element gets one challenge match against the current best.
process decision input / output start / end The max scan — track the champion through every pass.
Your predicted output — one line per console line
Hint: best starts as a[0] = 3 and only changes when a bigger value appears — 9 beats everything.
Click a pattern, then click its signature move to lock the pair.
5 conceptsTap a card to expand ?running sumdrops into place
?max scandrops into place
?two-index reversedrops into place
?count matchesdrops into place
?off-by-onedrops into place
01Most array problems are patterns: sum, max/min, count, reverse.
02Start a best-so-far scan at a[0], not at 0 — a[0] is a real value.
03Reversing swaps ends inward and stops when the fingers meet (i < j).
04Small, honest loops beat clever one-liners — trace them on paper.
05Every pattern is a for loop with a different body.