Array (1D Vector) in C++
A 1D array is an indexed collection of elements sharing the same data type stored contiguously in memory under a single name. In C++, native array indices start at 0.
How It Works & Why It Matters
Instead of creating 30 separate variables for student grades, an array int grades[30] holds all values and pairs naturally with a for loop.
Always make sure the array index stays strictly between 0 and size - 1 to avoid out-of-bounds memory corruption.
int n, a[100];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int maxVal = a[0];
for (int i = 1; i < n; i++) {
if (a[i] > maxVal) maxVal = a[i];
}
🏫 Classroom Example
Storing daily temperatures in an array lets you compute the weekly average first and then count how many days exceeded that average.
⚖️ 0-based vs. 1-based Indexing
C++ arrays natively begin at index 0; if you prefer 1..n indexing in contests, declare one extra slot like a[101].
Initializing minVal = 0 before scanning positive array values (0 stays smaller than every positive score!). Always initialize with a[0].