-
Hajipur, Bihar, 844101
In C++, an array is a collection of elements of the same data type stored in consecutive memory locations. Arrays make it easier to store and manage large amounts of related data without using multiple separate variables.
For example, instead of declaring 10 different variables to store marks of 10 students, you can declare one array to store them all.
An array is a fixed-size sequence of elements that share the same data type.
Each element in the array can be accessed using an index number, which starts from 0.
int marks[5] = {90, 85, 88, 92, 75};
Here:
int → data type of the array
marks → name of the array
[5] → number of elements the array can hold
dataType arrayName[arraySize];
int numbers[10];
float prices[5];
char letters[26];
You can declare arrays of any valid data type — integers, floats, characters, or even user-defined structures.
There are several ways to initialize an array in C++.
int numbers[5] = {1, 2, 3, 4, 5};
If you provide fewer elements than the array size, the remaining elements automatically become 0.
int numbers[5] = {10, 20};
Output: 10, 20, 0, 0, 0
If you don’t specify the size, C++ will calculate it automatically.
int numbers[] = {2, 4, 6, 8};
Here, the size of the array is automatically set to 4.
Each array element can be accessed using its index number.
#include <iostream>
using namespace std;
int main() {
int marks[5] = {90, 85, 88, 92, 75};
cout << "First mark: " << marks[0] << endl;
cout << "Last mark: " << marks[4];
return 0;
}
Output:
First mark: 90
Last mark: 75
You can update any element in an array by directly referring to its index.
marks[2] = 95;
This changes the third element (index 2) to 95.
Most commonly, arrays are processed using loops.
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << "Element " << i << ": " << numbers[i] << endl;
}
return 0;
}
Output:
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {2, 4, 6, 8, 10};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i];
}
cout << "Sum = " << sum;
return 0;
}
Output:
Sum = 30
You can also take array values directly from the user.
#include <iostream>
using namespace std;
int main() {
int scores[5];
cout << "Enter 5 scores: ";
for (int i = 0; i < 5; i++) {
cin >> scores[i];
}
cout << "You entered: ";
for (int i = 0; i < 5; i++) {
cout << scores[i] << " ";
}
return 0;
}
Sample Output:
Enter 5 scores: 10 20 30 40 50
You entered: 10 20 30 40 50
Arrays occupy continuous memory locations.
For example:
int arr[3] = {10, 20, 30};
If the base address (first element) is at memory location 1000 and each integer takes 4 bytes:
arr[0] → 1000
arr[1] → 1004
arr[2] → 1008
This continuous layout helps C++ access elements quickly using the formula:
Address = Base Address + (Index × Size of each element)
A multidimensional array stores data in a table or matrix form.
The most common is the two-dimensional array.
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
You can visualize it like this:
1 2 3
4 5 6
cout << matrix[1][2];
Output: 6
#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
Fixed size: Once defined, the array size cannot change.
Same data type: All elements must be of the same type.
No automatic bounds checking: Accessing elements outside valid indexes can lead to errors.
Easy to store and manage multiple data elements.
Allows fast access using index numbers.
Supports looping and efficient computation.
Provides a simple structure for working with related data.
#include <iostream>
using namespace std;
int main() {
int arr[5] = {23, 45, 12, 67, 34};
int max = arr[0];
for (int i = 1; i < 5; i++) {
if (arr[i] > max)
max = arr[i];
}
cout << "Maximum element: " << max;
return 0;
}
Output:
Maximum element: 67
Arrays in C++ allow you to store and manipulate multiple values of the same type efficiently.
They are foundational data structures used for storing sequences, managing tables, and performing repetitive operations easily.
Key takeaways:
Arrays use zero-based indexing.
You can use loops for processing large data.
Two-dimensional arrays are useful for matrices and tabular data.
Arrays provide speed and simplicity but have fixed sizes.
Write a C++ program to input 10 integers into an array and display them in reverse order.
Create a program to find the maximum and minimum elements in an array of 8 integers.
Write a C++ program that takes 5 numbers as input and calculates their average using an array.
Develop a program to count how many even and odd numbers are present in an array.
Write a C++ program to find the sum of all elements in an array of size 10.
Create a program to search for a specific number in an array and display its position if found.
Write a C++ program to copy all elements from one array to another.
Write a program that takes 10 numbers and sorts them in ascending order using an array.
Create a C++ program that stores marks of 5 subjects in an array and calculates the total and percentage.
Write a C++ program that merges two arrays into a single array and displays the combined result.