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.
#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.
Ready. Inspect the first value.
Guided simulation · Change the inputs to test your understanding.
An array has capacity 10 and active length 3. Which statement is correct?
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.
#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
Mark complete when you can explain it yourself.