Nearby lessons
57 of 124C - One Dimensional Array
The 1D array is a single row of elements — the simplest and most used array in C. Learn its memory layout and the standard algorithms: sum, average, largest, smallest, reverse and search.
Structure of a 1D Array
A one-dimensional array needs a single index because it has only one row:
The Memory Layout
Elements sit one after another with no gaps. Because the spacing is uniform, C computes any element's address with one multiplication:
address of arr[i] = base address + (i × size of one element)
Sum and Average
Accumulate into a running total, then divide. Note the cast — without it you get integer division:
Largest and Smallest
Start by assuming element 0 is both the largest and the smallest, then compare the rest against it:
Reversing an Array
Swap the ends and walk inwards. Only loop to the middle — going all the way reverses it twice, back to the original:
Linear Search
Walk the array comparing each element. Record the index and stop early with break:
Counting Even and Odd
A single pass with the modulus operator classifies every element:
Common Mistakes
- Reversing with a full loop —
i < sizeinstead ofi < size/2swaps everything twice and changes nothing. - Integer division in the average —
total / sizetruncates; cast one operand tofloat. - Initialising max to 0 — fails for an array of all-negative numbers. Initialise to
a[0]. - Using
found = 0as "not found" — 0 is a valid index. Use-1. - Starting the max loop at
i = 0— harmless, but comparinga[0]with itself is wasted work; start at 1.
- A 1D array is one row of same-type elements: type name[size].
- Elements occupy contiguous memory, indexed 0 to size-1.
- Address of element i = base + (i * sizeof(type)).
- Sum, max, min, reverse and search are all single loops.
- Track the index, not just the value, when searching.