Loading W Code...
Foundation of all data structures - learn how arrays work in C++.
5
Topics
30
Minutes
C++
Language
Real-life example: Imagine a bookshelf with numbered slots. Each slot (index) can hold one book (element). If you want to find the 3rd book, you go directly to slot number 2 (since we start counting from 0).
Key Points:
- All elements must be of the same type (all integers, all strings, etc.)
- Size is fixed once declared (in C++)
- Access any element instantly using its index
- Memory is allocated in one continuous block
#include <iostream>
using namespace std;
int main() {
// Method 1: Declare and initialize
int numbers[5] = {10, 20, 30, 40, 50};
// Method 2: Declare first, assign later
int marks[3];
marks[0] = 85;
marks[1] = 90;
marks[2] = 78;
// Accessing elements
cout << "First element: " << numbers[0] << endl; // Output: 10
cout << "Third element: " << numbers[2] << endl; // Output: 30
// Array size
int size = sizeof(numbers) / sizeof(numbers[0]);
cout << "Array size: " << size << endl; // Output: 5
// Loop through array
cout << "All elements: ";
for (int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
// Output: 10 20 30 40 50
return 0;
}1. One-Dimensional Array (1D)
A simple list of elements in a single row.
Example: Storing marks of 5 students.
2. Two-Dimensional Array (2D)
A table with rows and columns (like a matrix).
Example: Storing marks of 5 students in 3 subjects.
3. Multi-Dimensional Array
Arrays with more than 2 dimensions.
Example: 3D arrays for storing data in a cube-like structure.
Memory Layout:
- 1D: [1][2][3][4][5]
- 2D: Like a grid/table
- 3D: Like a cube of data
#include <iostream>
using namespace std;
int main() {
// 1D Array
int arr1D[5] = {1, 2, 3, 4, 5};
cout << "1D Array: ";
for (int i = 0; i < 5; i++) {
cout << arr1D[i] << " ";
}
cout << endl;
// 2D Array (3 rows, 4 columns)
int arr2D[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
cout << "2D Array (Matrix):" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << arr2D[i][j] << " ";
}
cout << endl;
}
// Accessing specific element
cout << "Element at row 1, col 2: " << arr2D[1][2] << endl; // Output: 7
return 0;
}1. Traversal
Visiting each element one by one (using loops).
2. Insertion
Adding a new element. In a fixed array, you shift elements to make space.
3. Deletion
Removing an element and shifting remaining elements to fill the gap.
4. Searching
Finding if an element exists:
- Linear Search: Check each element one by one
- Binary Search: Works only on sorted arrays (faster)
5. Sorting
Arranging elements in order (ascending/descending).
6. Updating
Changing the value at a specific index.
#include <iostream>
using namespace std;
int main() {
int arr[10] = {5, 10, 15, 20, 25};
int n = 5; // Current number of elements
// 1. TRAVERSAL - Print all elements
cout << "Original array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
// 2. INSERTION - Insert 12 at index 2
int insertPos = 2;
int insertVal = 12;
for (int i = n; i > insertPos; i--) {
arr[i] = arr[i - 1]; // Shift elements right
}
arr[insertPos] = insertVal;
n++;
cout << "After inserting 12 at index 2: ";
for (int i = 0; i < n; i++) cout << arr[i] << " ";
cout << endl;
// 3. DELETION - Delete element at index 3
int deletePos = 3;
for (int i = deletePos; i < n - 1; i++) {
arr[i] = arr[i + 1]; // Shift elements left
}
n--;
cout << "After deleting element at index 3: ";
for (int i = 0; i < n; i++) cout << arr[i] << " ";
cout << endl;
// 4. SEARCHING - Linear Search for 20
int target = 20;
int foundIndex = -1;
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
foundIndex = i;
break;
}
}
cout << "Element 20 found at index: " << foundIndex << endl;
// 5. UPDATING - Change value at index 1
arr[1] = 100;
cout << "After updating index 1 to 100: ";
for (int i = 0; i < n; i++) cout << arr[i] << " ";
cout << endl;
return 0;
}1. Find Maximum/Minimum
Scan through array to find largest/smallest element.
2. Reverse an Array
Swap first with last, second with second-last, and so on.
3. Find Second Largest
Track both largest and second largest while traversing.
4. Check if Sorted
Compare each element with the next one.
5. Remove Duplicates
Keep track of unique elements using a new array or in-place.
6. Rotate Array
Move elements left or right by k positions.
7. Two Sum Problem
Find two numbers that add up to a target (asked in every interview!).
#include <iostream>
#include <algorithm> // for swap
using namespace std;
// Find Maximum Element
int findMax(int arr[], int n) {
int maxVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
// Reverse Array
void reverseArray(int arr[], int n) {
int start = 0, end = n - 1;
while (start < end) {
swap(arr[start], arr[end]);
start++;
end--;
}
}
// Check if Sorted (Ascending)
bool isSorted(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
return false;
}
}
return true;
}
int main() {
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6};
int n = 8;
// Find Maximum
cout << "Maximum: " << findMax(arr, n) << endl; // Output: 9
// Check if Sorted
cout << "Is Sorted: " << (isSorted(arr, n) ? "Yes" : "No") << endl; // Output: No
// Reverse Array
reverseArray(arr, n);
cout << "Reversed: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl; // Output: 6 2 9 5 1 4 1 3
return 0;
}Key Points:
1. Changes made inside the function affect the original array
2. You need to pass the size separately (arrays don't know their own size in functions)
3. You can use reference notation (int arr[]) or pointer notation (int* arr)
Why pass by pointer?
- Efficiency: Avoids copying the entire array
- Memory: Only the address is passed (8 bytes on 64-bit systems)
Return Array from Function:
In C++, you cannot directly return an array. Solutions:
1. Pass output array as parameter
2. Use dynamic memory (new/delete)
3. Use STL containers like vector (recommended)
#include <iostream>
using namespace std;
// Method 1: Array notation
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
// Method 2: Pointer notation (same as above)
void doubleElements(int* arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2; // This modifies original array!
}
}
// Calculate sum
int arraySum(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
// Fill array with value
void fillArray(int arr[], int size, int value) {
for (int i = 0; i < size; i++) {
arr[i] = value;
}
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = 5;
cout << "Original array: ";
printArray(numbers, size);
cout << "Sum: " << arraySum(numbers, size) << endl;
doubleElements(numbers, size);
cout << "After doubling: ";
printArray(numbers, size);
fillArray(numbers, size, 0);
cout << "After filling with 0: ";
printArray(numbers, size);
return 0;
}