An array is a collection of multiple values of the same data type stored under one variable name. Instead of creating separate variables for every value, we can store related values together in an array.
An array stores multiple values of the same data type in a single variable.
int marks[5];
Here, marks is an array that can store five integer
values.
Arrays are useful when we need to store many related values.
For example, five marks can be stored in one array instead of five separate variables.
The basic syntax for declaring an array is:
dataType arrayName[size];
Example:
int numbers[5];
Here:
int is the data type.numbers is the array name.5 is the number of elements.We can initialize an array when we declare it.
int numbers[5] = {10, 20, 30, 40, 50};
The five values are stored in the five array elements.
An array uses an index to identify each element. C++ array
indexes start from 0.
int numbers[5] = {10, 20, 30, 40, 50};
| Index | Value |
|---|---|
| 0 | 10 |
| 1 | 20 |
| 2 | 30 |
| 3 | 40 |
| 4 | 50 |
Use the index inside square brackets to access an element.
int numbers[5] = {10, 20, 30, 40, 50};
std::cout << numbers[0];
Output:
10
The expression numbers[0] accesses the first element.
int numbers[5] = {10, 20, 30, 40, 50};
std::cout << numbers[4];
Output:
50
For an array containing five elements, the last index is
4.
An array element can be changed by assigning a new value to its index.
int numbers[5] = {10, 20, 30, 40, 50};
numbers[2] = 100;
std::cout << numbers[2];
Output:
100
A loop can be used to print every element of an array.
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << std::endl;
}
Output:
10
20
30
40
50
We can use a loop to take values from the user.
int numbers[5];
for (int i = 0; i < 5; i++) {
std::cin >> numbers[i];
}
Each input value is stored at a different index.
#include <iostream>
int main() {
int numbers[5];
std::cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; i++) {
std::cin >> numbers[i];
}
std::cout << "Numbers are:" << std::endl;
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << " ";
}
return 0;
}
int numbers[5] = {10, 20, 30, 40, 50};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i];
}
std::cout << "Sum = " << sum;
Output:
Sum = 150
int marks[5] = {70, 80, 90, 60, 75};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += marks[i];
}
double average = sum / 5.0;
std::cout << "Average = "
<< average;
Using 5.0 helps produce a decimal result.
int numbers[5] = {25, 80, 45, 90, 30};
int largest = numbers[0];
for (int i = 1; i < 5; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
std::cout << "Largest = "
<< largest;
Output:
Largest = 90
int numbers[5] = {25, 80, 45, 90, 30};
int smallest = numbers[0];
for (int i = 1; i < 5; i++) {
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
std::cout << "Smallest = "
<< smallest;
Output:
Smallest = 25
int numbers[6] = {10, 15, 20, 25, 30, 35};
int count = 0;
for (int i = 0; i < 6; i++) {
if (numbers[i] % 2 == 0) {
count++;
}
}
std::cout << "Even numbers = "
<< count;
The modulus operator checks whether an element is divisible by 2.
int numbers[6] = {10, 15, 20, 25, 30, 35};
int count = 0;
for (int i = 0; i < 6; i++) {
if (numbers[i] % 2 != 0) {
count++;
}
}
std::cout << "Odd numbers = "
<< count;
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 4; i >= 0; i--) {
std::cout << numbers[i] << " ";
}
Output:
50 40 30 20 10
The loop starts from the last index and moves toward index 0.
An array can also store characters.
char letters[5] = {'A', 'B', 'C', 'D', 'E'};
for (int i = 0; i < 5; i++) {
std::cout << letters[i] << " ";
}
Output:
A B C D E
A character array can be initialized using a string literal.
char name[] = "Rahul";
std::cout << name;
C++ stores the characters along with a terminating null character
'\0'.
For modern C++ programs, std::string is generally more
convenient for working with text.
The sizeof operator can be used to determine the total
number of bytes occupied by an array.
int numbers[5] = {10, 20, 30, 40, 50};
std::cout << sizeof(numbers);
To calculate the number of elements in an array in the same scope, we can use:
int size = sizeof(numbers) / sizeof(numbers[0]);
If an array is initialized with fewer values than its declared size, the remaining elements are value-initialized.
int numbers[5] = {10, 20};
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << " ";
}
Output:
10 20 0 0 0
When an array is initialized with values, its size can be omitted. The compiler determines the size from the initializer.
int numbers[] = {10, 20, 30, 40, 50};
This array contains five elements.
void display(int numbers[], int size) {
for (int i = 0; i < size; i++) {
std::cout << numbers[i] << " ";
}
}
int main() {
int numbers[5] = {
10, 20, 30, 40, 50
};
display(numbers, 5);
return 0;
}
An array can be passed to a function along with its size.
int numbers[5] = {
10, 20, 30, 40, 50
};
int search = 30;
bool found = false;
for (int i = 0; i < 5; i++) {
if (numbers[i] == search) {
found = true;
break;
}
}
if (found) {
std::cout << "Value found";
}
else {
std::cout << "Value not found";
}
0 through 4. Accessing an invalid index
can cause undefined behavior.
#include <iostream>
int main() {
int marks[5];
int sum = 0;
std::cout << "Enter marks of 5 subjects:"
<< std::endl;
for (int i = 0; i < 5; i++) {
std::cin >> marks[i];
sum += marks[i];
}
double average = sum / 5.0;
std::cout << "Total = "
<< sum << std::endl;
std::cout << "Average = "
<< average;
return 0;
}
#include <iostream>
int main() {
int numbers[5];
std::cout << "Enter 5 numbers:"
<< std::endl;
for (int i = 0; i < 5; i++) {
std::cin >> numbers[i];
}
int largest = numbers[0];
for (int i = 1; i < 5; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
std::cout << "Largest = "
<< largest;
return 0;
}
std::array or std::vector when their
features are more suitable than a built-in array.| Concept | Meaning |
|---|---|
| Array | A collection of values of the same data type. |
| Index | Position used to access an array element. |
| First Index | Always 0 for a built-in C++ array. |
| Last Index | Size minus 1. |
| Initialization | Giving initial values to array elements. |
| sizeof | Returns the size in bytes of its operand. |
| Loop | Commonly used to process array elements. |
int numbers[5] = {
10, 20, 30, 40, 50
};
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << " ";
}
sizeof can help determine the total size of an array in bytes.Question: What is the index of the first element in a C++ array?