Lesson 1 of 12

Arrays: give every value a place

Understand indices, initialization, capacity, active length, and the first safe array loop.

Store values you need to revisit

Reading repeatedly into one variable replaces its previous value. A running sum does not need the old values, but printing a list backward or comparing values after finding its average does. An array stores a fixed number of elements of the same type under one name.

In int scores[4] = {12, 7, 20, 9}, the indices are 0, 1, 2, 3. scores[2] is the stored value 20. The index tells you where to look; it is not a request to search for the number 2.

Initialization is not repetition

int a[5]{} initializes all five integers to zero. int a[5] = {6, 2} initializes 6, 2, 0, 0, 0. In particular, {-1} does not fill the whole array with −1. To fill every element with another value, use a loop.

An ordinary local int a[5] without initialization has no dependable values to read. Other element types follow the same pattern, such as double measurements[3] or bool chosen[4]{}. The declared element count is different from an access index.

Capacity is not the current length

A capacity of 1000 reserves 1000 cells. If the current input has n = 4 values, only indices 0 through 3 are active. a[4] is allocated but inactive; a[1000] is outside the physical array. Raw indexing does not automatically check either boundary.

Use a positive compile-time capacity chosen from the constraints, then keep n separately. A runtime-sized declaration int a[n] is not standard C++23, even if a compiler accepts it as an extension. n = 0 can describe an empty active list without declaring a zero-length array.

Translate a valid one-based position p to index p − 1 exactly once. To report an index i as a one-based position, use i + 1. Do not mix those conventions.

Read and print the same active range

Both loops below visit 0 through n − 1. The first input is the length; the next n inputs are elements. Numeric whitespace is flexible, so splitting input across different lines does not change the sequence.

Print elements explicitly; streaming a raw integer-array name does not print its contents. The separator condition produces spaces between values. For n = 0, only the final newline is printed.

C++23
#include <iostream>

int main() {
    const int CAPACITY = 1000;
    int a[CAPACITY]{};
    int n = 0;
    std::cin >> n; // 0 <= n <= CAPACITY.
    for (int i = 0; i < n; ++i) { std::cin >> a[i]; }
    for (int i = 0; i < n; ++i) {
        if (i > 0) { std::cout << ' '; }
        std::cout << a[i];
    }
    std::cout << '\n';
    return 0;
}

Array search

Linear search checks values from left to right and stops at the first match.

30
11
42
13
54
95

Ready. Inspect the first value.

Guided simulation · Change the inputs to test your understanding.

Pause & predict

An array has capacity 10 and active length 3. Which statement is correct?

Choose an answer
Try it yourself

Turn the idea into code

Read 0 ≤ n ≤ 1000 integers and print them in reverse input order. Preserve the stored array.

Give me a hint

Store first, then visit signed indices from n − 1 down to 0. An empty input starts at −1 and skips the loop.

Show the explained solution

The output traversal changes the visiting order without assigning to any cell. Use a signed counter so the stopping value −1 is representable.

C++23
#include <iostream>

int main() {
    int a[1000]{};
    int n = 0;
    std::cin >> n;
    for (int i = 0; i < n; ++i) { std::cin >> a[i]; }
    for (int i = n - 1; i >= 0; --i) {
        if (i < n - 1) { std::cout << ' '; }
        std::cout << a[i];
    }
    std::cout << '\n';
    return 0;
}

Your notes

Ready for the next idea?

Mark complete when you can explain it yourself.

From a problem to a programIdentify the input, output, and constraints, then translate a small plan into C++.Variables, types, and remembered valuesChoose a representation, initialize it, and understand what assignment really changes.Arithmetic without surprisesPredict division and remainder, widen before multiplication, and format decimal answers.Conditions and logical decisionsBuild exclusive categories, combine comparisons, and protect operations with short-circuit logic.Optional: other ways to chooseRecognize switch for exact choices and the conditional operator for choosing a value.For loops: make repetition preciseTrace initialization, condition, body, and update—and avoid an extra iteration.While, do-while, and stopping rulesChoose a loop from its stopping condition and read the exact input format.Break, continue, and early answersDistinguish leaving a loop from skipping one iteration, without losing required input.Five patterns worth understandingUse sums, counts, best-so-far candidates, products, and flags to process a stream.Nested loops: rows and columnsTranslate a shape into two loop roles and count the total work.Take a number apart, one digit at a timeUse remainder and division to process decimal digits, including zero.Multiple cases and a debugging mindsetCombine the patterns, isolate each case, and test the boundaries that can break your idea.Arrays: give every value a placeUnderstand indices, initialization, capacity, active length, and the first safe array loop.Scan, count, search, and compareDistinguish values from positions and preserve the exact tie rule a problem asks for.Change a sequence without losing itUpdate, copy, compare, reverse, and inspect neighbours using the correct bounds.Matrices: think in two coordinatesTraverse tables, total rows and columns, inspect diagonals, and protect grid neighbours.Characters become stringsSeparate characters from numbers, then safely inspect and edit managed text.Read the text the problem actually givesChoose words or full lines, preserve empty cases, and reset only the state that needs it.Scan text: count, filter, transformBuild a result from matching characters without losing leading zeros or skipping shifted text.Join, compare, and find textLearn content comparison, lexicographical order, substring counts, and the not-found sentinel.