🚀
ArkyEdu GLOSAR TIC
RO Practice Lab
Algorithms, C++ & Scratch Grade 8 RO Term: Vector (Tablou Unidimensional) în C++

Array (1D Vector) in C++

💬 Share on WhatsApp
📌 Quick Definition (Answer-First)

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.

💻 Reading & Finding Maximum in a C++ Array
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].

⚠️ Common Mistake in Exams & Homework

Initializing minVal = 0 before scanning positive array values (0 stays smaller than every positive score!). Always initialize with a[0].

❓ Quick Questions & Answers

What is an array index out-of-bounds bug?
Reading or writing a[n] when valid indices only go from 0 to n - 1.

Related Glossary Terms

← Back to Full Glossary (A–Z)