Arrays
Today we will get acquainted with arrays, also called Array in English, and learn to work with large amounts of data.
Array Definition
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.
Array Declaration
Here's how to declare an array:
data_type array_name[size];
In an array, we have two main components:
- Data type: determines the type of elements that will be stored in the array.
- Size: specifies the number of elements that can be stored in the array.
Working with Arrays
Let's look at an example of an array that stores five integer values:
int numbers[5];
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.
Assigning Values to Array Elements
We can assign values to array elements as follows:
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
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.
Getting Values of Array Elements
We can also get values of array elements using their indices:
cout << numbers[0] << endl;
cout << numbers[1] << endl;
cout << numbers[2] << endl;
cout << numbers[3] << endl;
cout << numbers[4] << endl;
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.
Execution Result
The result will be as follows:
10
20
30
40
50
Using Loops with Arrays
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:
for (int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
In this example, we loop through the numbers in our array and use i to access array indices.
Complete Program with Array
#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;
}
Array Initialization During Declaration
You can also initialize an array immediately upon declaration:
int numbers[5] = {10, 20, 30, 40, 50};
Or even like this (size will be determined automatically):
int numbers[] = {10, 20, 30, 40, 50}; // Size = 5
User Input into Array
#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;
}
Arrays are a fundamental data structure that allows you to efficiently work with collections of elements of the same type.