What is an Array in C++?
A C++ array lets you store many values of the same type in a single variable. It’s like having a row of mailboxes 📬—each with its own index number.
Instead of creating 5 separate variables, you can create one array and access each item using its index.
📦 Declaring and Initializing an Array
type arrayName[size];
For example:
int numbers[5]; // Declaration
int marks[3] = {90, 85, 88}; // Initialization with values
🧪 Example: Basic Array
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << "Element " << i << ": " << numbers[i] << endl;
}
return 0;
}
🔢 Array Index Starts From 0
In C++, array indexing starts from 0. So the first element is at index 0, not 1.
📘 Modify Array Elements
You can change an element using its index:
numbers[2] = 99; // Changes the third element to 99
✅ Looping Through an Array
The for loop is perfect for arrays:
for (int i = 0; i < size; i++) {
cout << arrayName[i];
}
💡 Summary
- Array = a collection of same-type values
- Indexing starts from 0
- You can loop through it easily
Arrays help you store more in one place—like a list of scores, names, or sensor values. 🧠💾