Arrays in C++
An array is a collection of elements of the same data type stored in contiguous memory locations. Instead of declaring 10 separate variables, an array can hold all 10 values under a single name. Think of an array like a row of lockers — each locker is numbered and stores one item.
Declaring an Array
data_type array_name[size];
int marks[5]; // array of 5 integers
float prices[3]; // array of 3 floats
char letters[4]; // array of 4 characters
Initializing an Array
int marks[5] = {85, 90, 78, 92, 88};
If fewer values are provided, the remaining elements are set to 0:
int nums[5] = {10, 20}; // {10, 20, 0, 0, 0}
Size can be omitted if values are provided at declaration:
int scores[] = {55, 70, 80, 95}; // size is automatically 4
Accessing Array Elements
Array elements are accessed using an index starting from 0.
int marks[5] = {85, 90, 78, 92, 88};
cout << marks[0] << endl; // 85 (first element)
cout << marks[4] << endl; // 88 (last element)
Iterating Through an Array
#include <iostream>
using namespace std;
int main() {
int marks[5] = {85, 90, 78, 92, 88};
for (int i = 0; i < 5; i++) {
cout << "marks[" << i << "] = " << marks[i] << endl;
}
return 0;
}
Output:
marks[0] = 85
marks[1] = 90
marks[2] = 78
marks[3] = 92
marks[4] = 88Finding the Sum and Average
int nums[] = {10, 20, 30, 40, 50};
int sum = 0;
int size = 5;
for (int i = 0; i < size; i++) {
sum += nums[i];
}
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / size << endl;
Output:
Sum: 150
Average: 30Finding Maximum and Minimum
int nums[] = {34, 7, 89, 23, 55};
int maxVal = nums[0];
int minVal = nums[0];
for (int i = 1; i < 5; i++) {
if (nums[i] > maxVal) maxVal = nums[i];
if (nums[i] < minVal) minVal = nums[i];
}
cout << "Max: " << maxVal << endl;
cout << "Min: " << minVal << endl;
Output:
Max: 89
Min: 7Two-Dimensional Arrays (2D Arrays)
A 2D array is like a table with rows and columns. It is commonly used for matrices and grids.
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
#include <iostream>
using namespace std;
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << grid[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output:
1 2 3
4 5 6Passing Arrays to Functions
Arrays are passed to functions by reference (the function receives the address, not a copy):
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int data[] = {5, 10, 15, 20};
printArray(data, 4);
return 0;
}
Output:
5 10 15 20Common Array Mistakes
| Mistake | Description |
|---|---|
| Out-of-bounds access | Accessing arr[5] when size is 5 (valid index: 0-4) |
| Using wrong index | Starting from index 1 instead of 0 |
| Uninitialized array | Declaring without values — contains garbage data |
Key Takeaways
- Arrays store multiple values of the same type under one name.
- Array index starts at 0 — the last element is at index
size - 1. - Loops are commonly used to traverse (go through) arrays.
- 2D arrays represent tables with rows and columns.
- Arrays passed to functions are passed by reference, not by value.
