Today we will get acquainted with arrays, also called Array in English, and learn to work with large amounts of data.
An array is a data structure that allows you to store elements of the same type in a sequence. They are an important part of programming and allow us to efficiently work with large amounts of data.
In simple words, an array is a way to store multiple variables at once. If previously we used only one, two, or three variables a, b, c, now we will learn to have one array that will store all values. You can think of an array as a horizontal box with many cells.
And in each cell, we have variables of a specific type.
Here's how to declare an array:
In an array, we have two main components:
Let's look at an example of an array that stores five integer values:
In this example, we declare an array numbers that can store five integer values. Each element of the array has its own index, starting from 0.
We can assign values to array elements as follows:
In this example, we assign values to elements of the numbers array with indices from 0 to 4.
Important: Array indices start from 0, not 1. So if we have an array of 5 elements, the indices will be: 0, 1, 2, 3, 4.
We can also get values of array elements using their indices:
In this example, we output the values of elements of the numbers array using cout. Notice that we use indices from 0 to 4 to access each element.
The result will be as follows:
Arrays also allow us to use loops to traverse and perform operations on array elements.
For example, here's how we can use a for loop to output all elements of an array:
In this example, we loop through the numbers in our array and use i to access array indices.
You can also initialize an array immediately upon declaration:
Or even like this (size will be determined automatically):
Arrays are a fundamental data structure that allows you to efficiently work with collections of elements of the same type.
data_type array_name[size];
int numbers[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
cout << numbers[0] << endl;
cout << numbers[1] << endl;
cout << numbers[2] << endl;
cout << numbers[3] << endl;
cout << numbers[4] << endl;
10
20
30
40
50
for (int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
#include <iostream>
using namespace std;
int main() {
// Declare an array of 5 integers
int numbers[5];
// Fill the array with values
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Output all array elements using a loop
cout << "Array elements:" << endl;
for (int i = 0; i < 5; i++) {
cout << "numbers[" << i << "] = " << numbers[i] << endl;
}
// We can also output in one line
cout << "\nAll elements in one line: ";
for (int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
int numbers[5] = {10, 20, 30, 40, 50};
int numbers[] = {10, 20, 30, 40, 50}; // Size = 5
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers:" << endl;
for (int i = 0; i < 5; i++) {
cout << "Number " << (i + 1) << ": ";
cin >> numbers[i];
}
cout << "You entered: ";
for (int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}