Module 6: Arrays in C++
An array is a collection of elements of the same data type stored in contiguous memory locations. Arrays allow you to store multiple values using a single variable name and access each element using its index.
Example:
1int marks[5] = {80, 75, 90, 85, 95};
1. One-Dimensional Array
A one-dimensional (1D) array stores elements in a single row.
Syntax
1dataType arrayName[size];
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int marks[5] = {80, 75, 90, 85, 95}; 7 8 for (int i = 0; i < 5; i++) 9 { 10 cout << marks[i] << " "; 11 } 12 13 return 0; 14}
Output
180 75 90 85 95
2. Two-Dimensional Array
A two-dimensional (2D) array represents data in rows and columns, similar to a table or matrix.
Syntax
1dataType arrayName[rows][columns];
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int matrix[2][3] = 7 { 8 {1, 2, 3}, 9 {4, 5, 6} 10 }; 11 12 for (int i = 0; i < 2; i++) 13 { 14 for (int j = 0; j < 3; j++) 15 { 16 cout << matrix[i][j] << " "; 17 } 18 cout << endl; 19 } 20 21 return 0; 22}
Output
11 2 3 24 5 6
3. Three-Dimensional Array
A three-dimensional (3D) array stores data in multiple layers. It is commonly used in graphics, scientific computing, and simulations.
Syntax
1dataType arrayName[x][y][z];
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int arr[2][2][2] = 7 { 8 { 9 {1, 2}, 10 {3, 4} 11 }, 12 { 13 {5, 6}, 14 {7, 8} 15 } 16 }; 17 18 cout << arr[1][0][1]; 19 20 return 0; 21}
Output
16
4. Passing Arrays to Functions
Arrays can be passed to functions for processing without copying all elements.
Example
1#include <iostream> 2using namespace std; 3 4void display(int arr[], int size) 5{ 6 for (int i = 0; i < size; i++) 7 { 8 cout << arr[i] << " "; 9 } 10} 11 12int main() 13{ 14 int numbers[] = {10, 20, 30, 40, 50}; 15 16 display(numbers, 5); 17 18 return 0; 19}
Output
110 20 30 40 50
5. Searching Arrays
Searching is the process of finding a specific element in an array.
Linear Search
Linear search checks each element one by one.
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int arr[] = {10, 20, 30, 40, 50}; 7 int key = 30; 8 bool found = false; 9 10 for (int i = 0; i < 5; i++) 11 { 12 if (arr[i] == key) 13 { 14 found = true; 15 break; 16 } 17 } 18 19 if (found) 20 cout << "Element Found"; 21 else 22 cout << "Element Not Found"; 23 24 return 0; 25}
Output
1Element Found
Note: Binary Search is faster than Linear Search but requires the array to be sorted.
6. Sorting Arrays
Sorting arranges array elements in ascending or descending order.
Bubble Sort
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int arr[] = {5, 2, 8, 1, 4}; 7 int n = 5; 8 9 for (int i = 0; i < n - 1; i++) 10 { 11 for (int j = 0; j < n - i - 1; j++) 12 { 13 if (arr[j] > arr[j + 1]) 14 { 15 swap(arr[j], arr[j + 1]); 16 } 17 } 18 } 19 20 for (int i = 0; i < n; i++) 21 { 22 cout << arr[i] << " "; 23 } 24 25 return 0; 26}
Output
11 2 4 5 8
Best Practices
- Always initialize arrays before using them.
- Ensure indices remain within array bounds.
- Use loops to traverse arrays efficiently.
- Pass array size when sending arrays to functions.
- Use standard library algorithms like
std::sort()for better performance in real-world applications.
Common Mistakes
Accessing an Invalid Index
❌ Incorrect
1int arr[5]; 2 3cout << arr[5];
Array indices range from 0 to 4.
✅ Correct
1cout << arr[4];
Forgetting Array Size
1int numbers[5];
Only five elements can be stored.
Practice Problems
- Find the largest element in an array.
- Find the smallest element in an array.
- Calculate the sum and average of array elements.
- Reverse an array.
- Search for an element using Linear Search.
- Implement Binary Search on a sorted array.
- Sort an array using Bubble Sort.
- Sort an array using Selection Sort.
- Add two matrices.
- Transpose a matrix.
Interview Questions
- What is an array in C++?
- What is the difference between 1D, 2D, and 3D arrays?
- How are arrays passed to functions?
- What is the difference between Linear Search and Binary Search?
- Which sorting algorithm is implemented above?
- What happens if you access an array index out of bounds?
- What is the time complexity of Bubble Sort?
- When should you use multidimensional arrays?
Summary
In this module, you learned:
- What arrays are and why they are used.
- How to create and use one-dimensional, two-dimensional, and three-dimensional arrays.
- How to pass arrays to functions.
- How Linear Search works to find elements.
- How Bubble Sort arranges array elements in order.
- Common mistakes, best practices, and practical exercises.
In the next module, you'll learn Pointers in C++, including pointer declaration, pointer arithmetic, pointers and arrays, pointers to functions, dynamic memory allocation (new and delete), smart pointers, and memory management.