Arrays — A Row of Boxes
Learn how to store many values in one variable using arrays — the most important data structure in CP!
Open interactive version (quiz + challenge)Real-world analogy
An array is like a row of mailboxes in an apartment building. Each mailbox has a number on it (starting from 0), and each one can hold one letter (a value). If you want to read the letter in mailbox #3, you just go straight to box number 3. You don't have to open all the other boxes first!
What is it?
An array is a data structure that stores a fixed-size list of elements of the same type. Each element is accessed by its position number (index), starting from 0. Arrays are the foundation of almost every CP problem.
Real-world relevance
Arrays are everywhere! A class roster is an array of student names. A playlist is an array of songs. A scoreboard is an array of scores. In CP, nearly every problem gives you an array of numbers and asks you to do something with them — find the biggest, sort them, count something, etc.
Key points
- What is an Array? — An array is a way to store many values of the same type in a single variable. Instead of creating 100 separate variables like a1, a2, a3... you create one array that holds all 100 values. Think of it as a row of boxes — each box has a number (called an index) and holds one value.
- Declaring an Array — In C++, you declare an array like this: int arr[5]; — this creates 5 boxes that can each hold an integer. You can also declare with initial values: int arr[5] = {10, 20, 30, 40, 50}; The number in the square brackets tells C++ how many boxes to create.
- Indexing Starts at 0 — The first box in an array is box number 0, not box number 1! This is super important. So if you have int arr[5] = {10, 20, 30, 40, 50}, then arr[0] is 10, arr[1] is 20, arr[2] is 30, arr[3] is 40, and arr[4] is 50. The last index is always size minus 1.
- Reading an Array with a Loop — In CP, you almost always read array values from the user using a for loop. First read n (how many values), then loop from i = 0 to i < n and read each arr[i]. This is one of the most common patterns you will write in every single contest!
- Printing an Array — To print all values in an array, use a for loop from 0 to n-1 and print each element. You can print them on one line separated by spaces: cout << arr[i] << ' '; Or each on a new line: cout << arr[i] << '\n'; Choosing the right format depends on the problem.
- Accessing Individual Elements — You can read or change any box at any time using its index. arr[2] = 99; changes the third box to 99. You can also use arr[i] where i is a variable — this is what makes arrays so powerful with loops. The computer can jump to any box instantly!
- Array Size Limits — In CP, arrays can be very large — sometimes up to 1,000,000 elements or more. When you need a big array, declare it outside main() (as a global variable) so it doesn't cause a stack overflow. Inside main(), arrays bigger than about 100,000 can crash your program.
- Common Beginner Mistake — The #1 beginner mistake is going out of bounds — trying to access arr[5] when the array only has 5 elements (indices 0-4). This is called 'array index out of bounds' and can cause wrong answers or crashes. Always remember: if the array has n elements, valid indices are 0 to n-1.
Code example
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n; // How many numbers?
int arr[n]; // Create an array of n boxes
// Read n numbers into the array
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// Print the array in reverse order
cout << "Reversed: ";
for (int i = n - 1; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
// Access a specific element
cout << "First element: " << arr[0] << endl;
cout << "Last element: " << arr[n - 1] << endl;
return 0;
}
// Input: 5
// 10 20 30 40 50
// Output: Reversed: 50 40 30 20 10
// First element: 10
// Last element: 50Line-by-line walkthrough
- 1. We include bits/stdc++.h to get all the tools we need.
- 2. We read n — how many numbers the user will give us.
- 3. int arr[n] creates an array with n boxes, numbered 0 to n-1.
- 4. The first for loop reads numbers from the user into each box: arr[0], arr[1], ... arr[n-1].
- 5. The second for loop prints in reverse — it starts at n-1 (the last box) and goes down to 0.
- 6. arr[0] always gives us the first element, arr[n-1] always gives us the last element.
- 7. We use endl or '\n' to move to the next line after printing.
Spot the bug
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (int i = 1; i <= n; i++) {
cout << arr[i] << " ";
}
return 0;
}
// Input: 3
// 10 20 30
// Expected: 10 20 30
// But this code has a sneaky bug!Need a hint?
Look at where the loop starts and ends. Arrays in C++ start at index 0, not 1. And what happens when i equals n?
Show answer
The bug is that the loops go from i=1 to i<=n, but array indices go from 0 to n-1! When i=n, arr[n] is out of bounds — that box doesn't exist! The fix is to change both loops to 'for (int i = 0; i < n; i++)'. This is the #1 most common beginner bug with arrays.
Explain like I'm 5
Imagine you have a row of cubbies in your classroom, numbered 0, 1, 2, 3, and 4. Each cubby can hold one toy. An array is just like those cubbies! You can put a toy in cubby number 2, or look at what is in cubby number 4. The cool thing is you can quickly go to any cubby without checking all the others first!
Fun fact
The reason array indices start at 0 (not 1) goes back to the 1960s! The index actually represents the 'offset' — how far the element is from the start. The first element is 0 steps from the start, the second is 1 step, and so on. It is more efficient for the computer this way!
Hands-on challenge
Read an array of n numbers. Print the array forward, then backward, then print only the elements at even indices (0, 2, 4, ...). This will help you master array traversal!
More resources
- Arrays in C++ — Complete Tutorial (The Cherno)
- Arrays — USACO Guide (USACO Guide)
- Array Practice Problems (Codeforces)