← All lectures

Lecture 15 · 30 min

Arrays II — 2D & Common Tasks

Copying, two-dimensional arrays of rows and columns, and the classic search / max / count patterns.

Copy with loop2D arraysFinding largestCountingColumn sums
Slide 1 / 14

Copying arrays — you can't use `=`

Unlike simple variables, you cannot copy one array to another with =. You must copy element by element with a loop.

code_example.cpp
int src[5] = {1, 2, 3, 4, 5};
int dst[5];

// WRONG — does NOT copy the array!
// dst = src;  // compile error

// CORRECT — copy element by element
for (int i = 0; i < 5; i++)
    dst[i] = src[i];
← / → navigateF fullscreenEsc exit⌘K search