Arrays and Iteration
Array Sum (Linear Scan)
Walk an array once, accumulating each element into a running total. This is
the canonical single-pass linear scan and the simplest possible loop
invariant: after step i, total equals the sum of arr[0..i].
Algorithm
The canonical input from the lesson spec is
arr=(3 1 4 1 5 9 2 6). After eight passes the running total is 31.
linear scan
Visit each element exactly once in index order.
running total
`total` accumulates the sum as the loop advances.
Basic Implementation
basic.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
set -euo pipefail
arr=(3 1 4 1 5 9 2 6)
total=0
i=0
while [ "$i" -lt "${#arr[@]}" ]; do
total=$((total + arr[i]))
i=$((i + 1))
done
echo "$total"
arr ← [3, 1, 4, 1, 5, 9, 2, 6]
2set -euo pipefail3arr=(3 1 4 1 5 9 2 6)4total=0values this step[3, 1, 4, 1, 5, 9, 2, 6]arrtotal ← 0
3arr=(3 1 4 1 5 9 2 6)4total=05i=0values this step0total[3, 1, 4, 1, 5, 9, 2, 6]arri ← 0
4total=05i=06while [ "$i" -lt "${#arr[@]}" ]; dovalues this step0i0totaltotal ← 3
6while [ "$i" -lt "${#arr[@]}" ]; do7 total=$((total + arr[i]))8 i=$((i + 1))values this step0 → 3total0i3arr[i]total ← 4
6while [ "$i" -lt "${#arr[@]}" ]; do7 total=$((total + arr[i]))8 i=$((i + 1))values this step3 → 4total1i1arr[i]total ← 8
6while [ "$i" -lt "${#arr[@]}" ]; do7 total=$((total + arr[i]))8 i=$((i + 1))values this step4 → 8total2i4arr[i]total ← 9
6while [ "$i" -lt "${#arr[@]}" ]; do7 total=$((total + arr[i]))8 i=$((i + 1))values this step8 → 9total3i1arr[i]total ← 14
6while [ "$i" -lt "${#arr[@]}" ]; do7 total=$((total + arr[i]))8 i=$((i + 1))values this step9 → 14total4i5arr[i]total ← 23
6while [ "$i" -lt "${#arr[@]}" ]; do7 total=$((total + arr[i]))8 i=$((i + 1))values this step14 → 23total5i9arr[i]total ← 25
6while [ "$i" -lt "${#arr[@]}" ]; do7 total=$((total + arr[i]))8 i=$((i + 1))values this step23 → 25total6i2arr[i]total ← 31
6while [ "$i" -lt "${#arr[@]}" ]; do7 total=$((total + arr[i]))8 i=$((i + 1))values this step25 → 31total7i6arr[i]
Trace Output
trace.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
set -euo pipefail
arr=(3 1 4 1 5 9 2 6)
total=0
i=0
while [ "$i" -lt "${#arr[@]}" ]; do
before=$total
total=$((total + arr[i]))
printf 'step %d: arr(%d)=%d total %d -> %d\n' "$i" "$i" "${arr[i]}" "$before" "$total"
i=$((i + 1))
done
printf 'final total = %d\n' "$total"
total ← 3, stdout ← step 0: arr(0)=3 total 0 -> 3
7before=$total8total=$((total + arr[i]))9printf 'step %d: arr(%d)=%d total %d -> %d\n' "$i" "$i" "${arr[i]}" "$before" "$total"values this step3totalstep 0: arr(0)=3 total 0 -> 3stdout0before3arr[i]total ← 4, stdout ← step 1: arr(1)=1 total 3 -> 4
7before=$total8total=$((total + arr[i]))9printf 'step %d: arr(%d)=%d total %d -> %d\n' "$i" "$i" "${arr[i]}" "$before" "$total"values this step4totalstep 1: arr(1)=1 total 3 -> 4stdout3before1arr[i]total ← 8, stdout ← step 2: arr(2)=4 total 4 -> 8
7before=$total8total=$((total + arr[i]))9printf 'step %d: arr(%d)=%d total %d -> %d\n' "$i" "$i" "${arr[i]}" "$before" "$total"values this step8totalstep 2: arr(2)=4 total 4 -> 8stdout4before4arr[i]total ← 9, stdout ← step 3: arr(3)=1 total 8 -> 9
7before=$total8total=$((total + arr[i]))9printf 'step %d: arr(%d)=%d total %d -> %d\n' "$i" "$i" "${arr[i]}" "$before" "$total"values this step9totalstep 3: arr(3)=1 total 8 -> 9stdout8before1arr[i]total ← 14, stdout ← step 4: arr(4)=5 total 9 -> 14
7before=$total8total=$((total + arr[i]))9printf 'step %d: arr(%d)=%d total %d -> %d\n' "$i" "$i" "${arr[i]}" "$before" "$total"values this step14totalstep 4: arr(4)=5 total 9 -> 14stdout9before5arr[i]total ← 23, stdout ← step 5: arr(5)=9 total 14 -> 23
7before=$total8total=$((total + arr[i]))9printf 'step %d: arr(%d)=%d total %d -> %d\n' "$i" "$i" "${arr[i]}" "$before" "$total"values this step23totalstep 5: arr(5)=9 total 14 -> 23stdout14before9arr[i]total ← 25, stdout ← step 6: arr(6)=2 total 23 -> 25
7before=$total8total=$((total + arr[i]))9printf 'step %d: arr(%d)=%d total %d -> %d\n' "$i" "$i" "${arr[i]}" "$before" "$total"values this step25totalstep 6: arr(6)=2 total 23 -> 25stdout23before2arr[i]total ← 31, stdout ← step 7: arr(7)=6 total 25 -> 31
7before=$total8total=$((total + arr[i]))9printf 'step %d: arr(%d)=%d total %d -> %d\n' "$i" "$i" "${arr[i]}" "$before" "$total"values this step31totalstep 7: arr(7)=6 total 25 -> 31stdout25before6arr[i]stdout ← final total = 31
10 i=$((i + 1))11done12printf 'final total = %d\n' "$total"values this stepfinal total = 31stdout31total
Complexity
- Time: O(n)
- Space: O(1)
Implementation notes
- Bash: use the explicit
while [ "$i" -lt "${#arr[@]}" ]loop withtotal=0and a manual 0-based index. Bash has no built-in sum primitive; piping toawk '{ s += $1 } END { print s }'would push the loop into another process the lesson is not teaching, andprintf '%s+' "${arr[@]}" 0 | bcwould defer the running update tobcand hide it. arr=(3 1 4 1 5 9 2 6)documents the fixed-content array; the manuali=$((i + 1))step keeps the iteration without leaning onfor v in "${arr[@]}"that hides the running index.set -euo pipefailat the top keeps every typo and unset variable loud, and arithmetic uses$((...))expansion so the assignments never tripset -e.- The replay shows
i,arr[i], andtotalbefore and after each addition, matching the lesson spec's state-transition table.