Lesson 13
Arrays
Covers declaration, indexing, traversal, 2D arrays.
Lesson content
What are arrays?
Array stores same-type values together.
Memory sits contiguous. Index starts at 0.
int mark1 = 85;
int mark2 = 90;
int mark3 = 78;
int mark4 = 95;
int mark5 = 88;
// vs
int marks[5] = {85, 90, 78, 95, 88};Arrays shorten and organize code.
School lockers analogy. Each has a number.
Why do arrays exist?
- Grouping similar data together
- Making data easier to process
- Reducing repetitive code
- Simplifying loops and algorithms
Real-world use cases
- Student mark management systems
- Inventory management
- Banking applications
- Game development
- Sensor data collection
- Image processing
- Mathematical computations
- Searching and sorting algorithms
Array declaration
data_type array_name[size];
int numbers[5];Array initialization
data_type array_name[size] = {value1, value2, ...};
int numbers[5] = {10, 20, 30, 40, 50};Accessing array elements
numbers[0]
numbers[1]
numbers[2]- First element: index 0
- Last element: size - 1
Modifying array elements
numbers[2] = 100;Traversing an array
Traversal visits every element in order.
for loop is most common for traversal.
Array size
sizeof finds an array's element count.
int size = sizeof(numbers) / sizeof(numbers[0]);Works only for compile-time known sizes.
Multidimensional arrays
Stores data in rows and columns.
int matrix[2][3];- Tables
- Matrices
- Game boards
- Grid-based problems
Example 1: Declare and display an array
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
cout << numbers[0] << '\n';
cout << numbers[4] << '\n';
return 0;
}Example 2: Read array elements
#include <iostream>
using namespace std;
int main()
{
int marks[5];
cout << "Enter 5 marks:\n";
for (int i = 0; i < 5; i++)
{
cin >> marks[i];
}
cout << "\nMarks:\n";
for (int i = 0; i < 5; i++)
{
cout << marks[i] << '\n';
}
return 0;
}Example 3: Find the largest element
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {12, 45, 8, 67, 23};
int largest = numbers[0];
for (int i = 1; i < 5; i++)
{
if (numbers[i] > largest)
{
largest = numbers[i];
}
}
cout << "Largest = " << largest;
return 0;
}Example 4: Calculate average
#include <iostream>
using namespace std;
int main()
{
int marks[5] = {80, 75, 90, 85, 95};
int sum = 0;
for (int i = 0; i < 5; i++)
{
sum += marks[i];
}
double average = static_cast<double>(sum) / 5;
cout << "Average = " << average;
return 0;
}Example 5: Two-dimensional array
#include <iostream>
using namespace std;
int main()
{
int matrix[2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
for (int row = 0; row < 2; row++)
{
for (int col = 0; col < 3; col++)
{
cout << matrix[row][col] << " ";
}
cout << '\n';
}
return 0;
}Common mistakes and pitfalls
- Accessing numbers[5] in 5-element array. Fix: use index 4
- Forgetting array size. Fix: specify or init correctly
- Using negative index. Fix: start from 0
- Traversing past array size. Fix: stop at size - 1
- Assuming arrays auto-resize. Fix: use std::vector
// Wrong
int numbers[5];
cout << numbers[5];// Correct
int numbers[5];
cout << numbers[4];Best practices
- Use meaningful array names
- Validate indices before accessing
- Use loops instead of repetitive statements
- Avoid hardcoding sizes everywhere
- Use std::vector for unknown sizes
- Initialize arrays before use
When not to use this
- Element count changes at runtime
- Frequent insertion or deletion needed
- You need automatic memory management
- You need sorting or dynamic resizing built in
Prefer std::vector in these cases.
Summary
- Arrays store same-type values
- Memory is contiguous
- Indexing starts from 0
- Arrays have fixed size
- Loops traverse arrays commonly
- 2D arrays hold rows and columns
- Avoid out-of-bounds access
- Use std::vector for dynamic sizing
Frequently asked questions
What is an array in C++?
Same-type elements stored contiguously, indexed.
Why do array indices start from 0?
Index is offset from first element in memory.
Can I change array size after declaring it?
No. Use std::vector for resizable data.
What is a multidimensional array?
Array of arrays. Common example: rows and columns.
What happens if I access an out-of-bounds element?
Undefined behavior. Crashes or wrong output possible.