English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn to use arrays. You will learn to declare, initialize, and access array elements in C ++Declaration, initialization, and access to array elements in programming.
One of the common problems encountered in programming is handling a large amount of data of the same type.
For example, in this case, you are dealing with100 people to be surveyed, and their ages must be stored in C ++To solve this problem in it, you can create an array containing100 elements integer array.
An array is a collection of a fixed number of elements of the same type. For example:
int age[100];
In this case, the age array can accommodate up to100 integer type elements.
After declaring an array, the size and type of the array cannot be changed.
dataType arrayName[arraySize];
For example,
float mark[5];
Here, we declare a float type and an array of size5 This means that the array mark can accommodate5a floating-point value.
You can access the elements of the array using the index.
Assuming you have declared a mark array as described above. The first element is mark[0], and the second element is mark[1], and so on.
The first index of the array is 0, not1In this example, mark[0] is the first element.
If the size of the array is n, to access the last element, the index (n-1). In this example, mark[4] is the last element.
Suppose the starting address of mark [0] is2120d. Then, the next address a [1] will be2124d, a [2The address of ] will be2128d, and so on. This is because the size of float is4bytes.
Arrays can be initialized during declaration. For example,
int mark[5] = {19, 10, 8, 17, 9};
Another way to initialize an array during declaration:
int mark[] = {19, 10, 8, 17, 9};
Here,
mark[0] = 19 mark[1] = 10 mark[2] = 8 mark[3] = 17 mark[4] = 9
int mark[5] = {19, 10, 8, 17, 9{ // Change the4element to9 mark[3] = 9; // Get input from the user and insert the third element cin >> mark[2]; // Accept user input and insert the (i + 1) element cin >> mark[i]; // Print the first element of the array cout << mark[0]; // Print the i-th element of the array cout >> mark[i-1];
C ++Program is used to store and calculate the sum of numbers entered by the user using an array5sum of numbers.
#include <iostream> using namespace std; int main() { int numbers[5], sum = 0; cout << "Input5numbers: "; //Enter the5numbers are stored in an array //Find the sum of the entered numbers for (int i = 0; i < 5; ++i) { cin >> numbers[i]; sum += numbers[i]; { cout << "Sum = " << sum << endl; return 0; {
Output result
Input5numbers: 3 4 5 4 2 Sum = 18
Suppose you declare an array consisting of10elements. For example
int testArray[10];
You can access from testArray[0] to testArray[9members of the array].
If you try to access array elements outside the array bounds, such as testArray[14], the compiler may not display any errors. However, this may lead to unexpected output (undefined behavior).
Before proceeding, please check the following C ++Array article:
In C++ In this context, arrays are very important, and we need to learn more about the details of arrays. The following lists C++ Programmers must be clear about some important concepts related to arrays:
Concept | Description |
---|---|
Multidimensional Arrays | C++ Supports multidimensional arrays. The simplest form of multidimensional array is a two-dimensional array. |
Pointer to an Array | You can generate a pointer to the first element of an array by specifying the array name without an index. |
Passing Arrays to Functions | You can pass a pointer to an array to a function by specifying the array name without an index. |
Returning Arrays from Functions | C++ Allows arrays to be returned from functions. |