Lesson 14 of 50 beginner

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

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: 50

Line-by-line walkthrough

  1. 1. We include bits/stdc++.h to get all the tools we need.
  2. 2. We read n — how many numbers the user will give us.
  3. 3. int arr[n] creates an array with n boxes, numbered 0 to n-1.
  4. 4. The first for loop reads numbers from the user into each box: arr[0], arr[1], ... arr[n-1].
  5. 5. The second for loop prints in reverse — it starts at n-1 (the last box) and goes down to 0.
  6. 6. arr[0] always gives us the first element, arr[n-1] always gives us the last element.
  7. 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

Open interactive version (quiz + challenge) ← Back to course: CP Zero to Hero