Arrays in C++
1. Introduction
An array is a collection of elements of the same data type stored in contiguous memory locations.
Instead of creating many separate variables, we can store multiple values using a single array.
Example
Without an array:
int mark1 = 80; int mark2 = 75; int mark3 = 90; int mark4 = 85;
Using an array:
int marks[4] = {80, 75, 90, 85};
Here, marks stores four integer values.
2. Syntax of an Array
dataType arrayName[size];
Example
int numbers[5];
int→ data typenumbers→ array name5→ number of elements
3. Array Index
Array indexing starts from 0.
For an array of 5 elements:
| Index | Element |
|---|---|
| 0 | First |
| 1 | Second |
| 2 | Third |
| 3 | Fourth |
| 4 | Fifth |
Example
int numbers[5] = {10, 20, 30, 40, 50}; cout << numbers[0];
Output
10
To access the last element:
cout << numbers[4];
Output:
50
4. Types of Arrays
There are mainly three common types of arrays:
- One-Dimensional Array
- Two-Dimensional Array
- Multidimensional Array
5. One-Dimensional Array
A one-dimensional array stores elements in a single row.
Syntax
dataType arrayName[size];
Example
#include <iostream> using namespace std; int main() { int numbers[5] = {10, 20, 30, 40, 50}; cout << numbers[0] << endl; cout << numbers[1] << endl; cout << numbers[2] << endl; cout << numbers[3] << endl; cout << numbers[4] << endl; return 0; }
Output
10 20 30 40 50
6. Taking Array Input from User
We can use a for loop to take input.
#include <iostream> using namespace std; int main() { int numbers[5]; cout << "Enter 5 numbers: "; for (int i = 0; i < 5; i++) { cin >> numbers[i]; } cout << "Array elements are: "; for (int i = 0; i < 5; i++) { cout << numbers[i] << " "; } return 0; }
Example Input
10 20 30 40 50
Output
Array elements are: 10 20 30 40 50
7. Finding the Sum of Array Elements
#include <iostream> using namespace std; int main() { int numbers[5] = {10, 20, 30, 40, 50}; int sum = 0; for (int i = 0; i < 5; i++) { sum = sum + numbers[i]; } cout << "Sum = " << sum; return 0; }
Output
Sum = 150
8. Finding the Average of Array Elements
#include <iostream> using namespace std; int main() { int marks[5] = {80, 75, 90, 85, 70}; int sum = 0; for (int i = 0; i < 5; i++) { sum += marks[i]; } float average = (float)sum / 5; cout << "Average = " << average; return 0; }
Output
Average = 80
9. Finding the Largest Element
#include <iostream> using namespace std; int main() { int numbers[5] = {25, 10, 45, 30, 20}; int largest = numbers[0]; for (int i = 1; i < 5; i++) { if (numbers[i] > largest) { largest = numbers[i]; } } cout << "Largest = " << largest; return 0; }
Output
Largest = 45
10. Finding the Smallest Element
#include <iostream> using namespace std; int main() { int numbers[5] = {25, 10, 45, 30, 20}; int smallest = numbers[0]; for (int i = 1; i < 5; i++) { if (numbers[i] < smallest) { smallest = numbers[i]; } } cout << "Smallest = " << smallest; return 0; }
Output
Smallest = 10
11. Two-Dimensional Array
A two-dimensional array is commonly used to represent data in rows and columns, like a table or matrix.
Syntax
dataType arrayName[rows][columns];
Example
int matrix[2][3];
This array has:
- 2 rows
- 3 columns
- Total elements = 6
12. Example of Two-Dimensional Array
#include <iostream> using namespace std; int main() { int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} }; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << matrix[i][j] << " "; } cout << endl; } return 0; }
Output
1 2 3 4 5 6
13. Character Array
A character array stores characters.
char name[6] = "Anu";
A string in C-style character array ends with a special null character \0.
Example:
char name[] = "Anu";
Internally, it is stored approximately as:
A n u \0
14. Array of Strings
We can also create an array containing multiple strings.
#include <iostream> #include <string> using namespace std; int main() { string names[3] = { "Anu", "Ram", "Sita" }; for (int i = 0; i < 3; i++) { cout << names[i] << endl; } return 0; }
Output
Anu Ram Sita
15. Passing Array to a Function
An array can be passed to a function.
#include <iostream> using namespace std; void display(int numbers[], int size) { for (int i = 0; i < size; i++) { cout << numbers[i] << " "; } } int main() { int numbers[5] = {10, 20, 30, 40, 50}; display(numbers, 5); return 0; }
Output
10 20 30 40 50
16. Searching an Element in an Array
A simple way to search an element is linear search.
#include <iostream> using namespace std; int main() { int numbers[5] = {10, 20, 30, 40, 50}; int search; cout << "Enter number to search: "; cin >> search; bool found = false; for (int i = 0; i < 5; i++) { if (numbers[i] == search) { found = true; break; } } if (found) cout << "Element found"; else cout << "Element not found"; return 0; }
17. Advantages of Arrays
- Stores multiple values using one variable name.
- Easy to access elements using indexes.
- Fast access to individual elements.
- Useful for storing large amounts of similar data.
- Makes programs shorter and more organized.
- Useful with loops for processing multiple values.
18. Disadvantages of Arrays
- Array size is normally fixed after declaration.
- It can store elements of only the same data type.
- Inserting and deleting elements can be difficult.
- If the allocated size is larger than required, memory can be wasted.
- Traditional C++ arrays do not automatically check whether an index is within bounds.
19. Real-Life Examples of Arrays
Arrays can be used for:
- Student marks
- Employee salaries
- Product prices
- Daily temperatures
- Monthly sales
- Exam results
- Lists of names
- Matrix calculations
- Game scores
- Sensor readings
Example
Student marks:
int marks[5] = {80, 75, 90, 85, 88};
20. Complete Array Example
The following program takes five marks from the user and calculates the total and average.
#include <iostream> using namespace std; int main() { int marks[5]; int total = 0; cout << "Enter marks of 5 subjects:" << endl; for (int i = 0; i < 5; i++) { cin >> marks[i]; total += marks[i]; } float average = (float)total / 5; cout << "Total = " << total << endl; cout << "Average = " << average << endl; return 0; }
Example Input
80 75 90 85 70
Output
Total = 400 Average = 80
21. Important Points to Remember
- Array stores multiple values of the same data type.
- Array indexing starts from
0. - The last index is
size - 1. - Use a
forloop to process array elements easily. - A 1D array uses one index.
- A 2D array uses two indexes.
- Arrays are useful for storing and processing collections of data.
Quick Syntax
// Declaration int numbers[5]; // Initialization int numbers[5] = {10, 20, 30, 40, 50}; // Access cout << numbers[0]; // Update numbers[0] = 100; // Loop for (int i = 0; i < 5; i++) { cout << numbers[i] << endl; }