ARRAYS IN C++
A focused, from-scratch guide with plenty of examples
1. Why Do We Even Need Arrays?
Imagine you need to store the marks of 5 students. Using what you already know, you might write:
int marks1 = 90;
int marks2 = 85;
int marks3 = 78;
int marks4 = 92;
int marks5 = 88;
This works for 5 students. But what if there were 100 students? Or what if you didn't know the
number in advance? You'd need 100 separate variable names, and worse — you couldn't easily loop
through them, because a `for` loop can't guess that `marks1`, `marks2`, `marks3`... are related.
An array solves this exact problem: it's a single variable name that holds MULTIPLE values of the
same type, all lined up in order, and — this is the important part — accessed by a position number,
so a loop can visit every one of them automatically.
THE BIG IDEA
An array is like a row of labeled boxes sitting right next to each other in memory, all holding the
same type of value, all sharing one name. You don't refer to a box by a new name each time — you
refer to it by its POSITION in the row.
2. Declaring an Array
General syntax:
dataType arrayName[size];
Example:
int marks[5];
This single line creates 5 connected integer boxes, all named `marks`, sitting right next to each other.
Right now they're empty (technically holding garbage/leftover memory values) — we haven't put
anything in them yet.
marks: [ ? ] [ ? ] [ ? ] [ ? ] [ ? ]
index: 0 1 2 3 4
RULE
The size of a normal C++ array is FIXED the moment you declare it. `int marks[5];` will always hold
exactly 5 integers for its entire life — you cannot add a 6th or shrink it to 4 later. (There are resizable
alternatives called `vector`s — we'll get to those in the intermediate document.)
3. Initializing an Array
Instead of leaving an array empty, you can fill it with values immediately:
Method 1 — list all values at once:
int marks[5] = {90, 85, 78, 92, 88};
marks: [ 90 ] [ 85 ] [ 78 ] [ 92 ] [ 88 ]
index: 0 1 2 3 4
Method 2 — partial initialization:
int marks[5] = {90, 85};
If you provide fewer values than the declared size, C++ automatically fills the remaining boxes with
0. So this creates `{90, 85, 0, 0, 0}`.
Method 3 — declare empty, fill later using a loop (very common in competitive programming):
int marks[5];
for (int i = 0; i < 5; i++) {
cin >> marks[i];
}
This last pattern — declare an empty array, then fill it from user input using a loop — is by far the
most common way you'll build arrays in ZCO/INOI problems, since you rarely know the exact values
in advance.
4. Indexing — The Most Important Rule in This Whole
Document
Every box in an array has a position number called an INDEX, used to access it. Here is the rule that
trips up more beginners than anything else in C++:
THE #1 RULE OF ARRAYS
Array indexing ALWAYS starts at 0, not 1. In an array of size 5, the valid indexes are 0, 1, 2, 3, and 4 —
the LAST valid index is always (size − 1), never the size itself.
Example:
int marks[5] = {90, 85, 78, 92, 88};
cout << marks[0] << endl; // 90 (the FIRST element)
cout << marks[4] << endl; // 88 (the LAST element)
cout << marks[2] << endl; // 78 (the THIRD element, at index 2)
Output:
90
88
78
A simple way to think about it: index tells you "how many boxes to skip from the start," not "which
position counting from 1." Skip 0 boxes → you land on the first one.
5. Accessing and Modifying Elements
You can both read from and write to any index, just like a normal variable — the only difference is
you specify which box using square brackets.
int marks[5] = {90, 85, 78, 92, 88};
marks[2] = 100; // overwrite index 2
cout << marks[2] << endl; // 100
marks[0] = marks[0] + 5; // read AND write the same index
cout << marks[0] << endl; // 95
Output:
100
95
6. Looping Through an Array
This is where arrays truly earn their keep: a `for` loop lets you visit every element without writing
repetitive code.
Printing every element:
int marks[5] = {90, 85, 78, 92, 88};
for (int i = 0; i < 5; i++) {
cout << marks[i] << " ";
}
Output:
90 85 78 92 88
Reading every element from user input:
int n;
cin >> n;
int marks[100]; // declared generously, only first n boxes get used
for (int i = 0; i < n; i++) {
cin >> marks[i];
}
Notice the pattern: `i` starts at 0 and the condition is `i < n` (strictly less than) — NOT `i <= n`. This is
exactly the off-by-one rule from your practice problems: looping "n times starting from 0" always
uses `i < n`.
RULE
In competitive programming, you'll almost always declare an array bigger than you think you need
(e.g. `int arr[100000];` for a problem that says N can be up to 1000) and only use the first N boxes —
because array size must be fixed in advance, but you don't always know N until you read it from
input.
7. Common Array Patterns You'll Use Constantly
Pattern 1 — Sum of All Elements
int marks[5] = {90, 85, 78, 92, 88};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += marks[i]; // same as: sum = sum + marks[i];
}
cout << sum;
Output:
433
Pattern 2 — Finding the Maximum
int marks[5] = {90, 85, 78, 92, 88};
int maxVal = marks[0]; // start by assuming the first element is the
biggest
for (int i = 1; i < 5; i++) {
if (marks[i] > maxVal) {
maxVal = marks[i];
}
}
cout << maxVal;
Output:
92
Notice the loop starts at `i = 1`, not `i = 0` — since we already used `marks[0]` as our starting
assumption, there's no need to compare it against itself.
Pattern 3 — Linear Search (does a value exist?)
int marks[5] = {90, 85, 78, 92, 88};
int target = 78;
bool found = false;
for (int i = 0; i < 5; i++) {
if (marks[i] == target) {
found = true;
break; // no need to keep looking once found
}
}
cout << (found ? "Found!" : "Not found");
Output:
Found!
This scan-through-and-check pattern is called a LINEAR SEARCH, and it's the simplest possible way to
look for a value in an array. Notice the `break;` — once you've found what you're looking for, there's
no reason to keep checking the rest.
8. Common Mistakes — Read This Twice
Mistake 1: Going Out of Bounds
int marks[5] = {90, 85, 78, 92, 88};
cout << marks[5]; // DANGER
RULE
marks[5] does NOT exist — valid indexes for a size-5 array are only 0 through 4. Unlike Python, C++
will NOT stop you or give a clean error. It will silently read whatever random data happens to sit in
memory right after your array, giving you a garbage value or, worse, crashing your program
unpredictably. Always double check your loop bounds.
Mistake 2: Confusing Size with Last Index
For an array of size `n`, the LAST valid index is `n - 1`, not `n`. This single subtraction is the source of
more competitive-programming bugs than almost anything else — get comfortable with it now.
Mistake 3: Forgetting Arrays Are Fixed-Size
If you declare `int arr[10];`, you cannot suddenly store an 11th value. If a problem says N can be up
to 100,000, declare your array with room for at least that many — declaring too small is a very
common source of wrong or crashing submissions.
9. Quick Reference Cheat Sheet
Task Syntax
Declare an array of size 5 int arr[5];
Declare + initialize int arr[5] = {1, 2, 3, 4, 5};
Partially initialize (rest become 0) int arr[5] = {1, 2};
Access an element arr[2]
Modify an element arr[2] = 99;
First element arr[0]
Last element (size n) arr[n - 1]
Loop through and print for (int i = 0; i < n; i++) cout << arr[i];
Loop through and read input for (int i = 0; i < n; i++) cin >> arr[i];
Sum of all elements for (int i = 0; i < n; i++) sum += arr[i];
Find the maximum if (arr[i] > maxVal) maxVal = arr[i];
That's the complete foundation of 1D arrays. Once indexing, looping, and these three patterns (sum,
max, search) feel natural, you'll be ready to combine arrays with sorting (which you've already
glimpsed with bubble sort) — and later, in the intermediate document, we'll cover 2D arrays
(grids/tables of values) and `vector`s, which are like arrays but can grow and shrink at runtime.