Blog
TCL TK Tutorial: Tool Command Language
What is TCL? TCL is shell application that reads TCL command from its standard input or from a...
An array is a data structure that stores an element of the same data type sequentially. A C++ array has a fixed-size.
You can see an array as a collection of variables of a similar data type. Instead of declaring each variable and assigning it a value individually, you can declare one variable (the array) and add the values of the various variables to it. Each value-added to the array is identified by an index.
In this C++ tutorial, you will learn:
Arrays are very important in any programming language. They provide a more convenient way of storing variables or a collection of data of a similar data type together instead of storing them separately. Each value of the array will be accessed separately.
Array declaration in C++ involves stating the type as well as the number of elements to be stored by the array. Syntax:
type array-Name [ array-Size ];
For example, you can create an array named age and store the ages of 5 students as follows:
int age[5];
The array age will store 5 integers representing the ages of different students.
Array initialization is the process of assigning/storing elements to an array. The initialization can be done in a single statement or one by one. Note that the first element in an array is stored at index 0, while the last element is stored at index n-1, where n is the total number of elements in the array.
In the case of the age array, the first element will be stored at index 0, while the last element will be stored at index 4.
Let us use the age array to demonstrate how array initialization can be done:
int age[5] = {19, 18, 21, 20, 17};
The total number of elements within the { } cannot exceed the value stated within the [ ]. The element 19 is at index 0, 18 at index 1, 21 at index 2, 20 at index 3 and 17 at index 4. If you don't state the number of elements to be stored in the array within [ ], the array will only be large enough to hold the elements added within { }. For example:
int age[] = {19, 18, 21, 20, 17};
The above statement will create exactly the same array as the previous one. You can also assign one element to an array using its index. For example:
age[3] = 20;
The above statement will store the value 20 at index 3 of the array named age. This means that 20 will be the 4th element of the array.
There are two types of C++ arrays:
This is an array in which the data items are arranged linearly in one dimension only. It is commonly called a 1-D array. Syntax:
datatype array-name[size];
For example:
#include <iostream>
using namespace std;
int main()
{
int age[5] = { 19, 18, 21, 20, 17 };
for (int x = 0; x < 5; x++)
{
cout <<age[x]<<"\n";
}
}
Output:
Here is a screenshot of the code:
Code Explanation:
This is an array in which data items are arranged to form an array of arrays. A multi-dimensional array can have any number of dimensions, but two-dimensional and three-dimensional arrays are common. Syntax:
datatype array-name[d1][d2][d3]...[dn];
The array-name is the name of the array that will have n dimensions. For example:
A 2D array stores data in a list with 1-D array. It is a matrix with rows and columns. To declare a 2D array, use the following syntax:
type array-Name [ x ][ y ];
The type must be a valid C++ data type. See a 2D array as a table, where x denotes the number of rows while y denotes the number of columns. This means that you identify each element in a 2D array using the form a[x][y], where x is the number of row and y the number of columns in which the element belongs.
Here is an example of how to initialize a 2D array:
int a[2][3] = {
{0, 2, 1} , /* row at index 0 */
{4, 3, 7} , /* row at index 1 */
};
In above example, we have a 2D array which can be seen as a 2x3 matrix. There are 2 rows and 3 columns. The element 0 can be accessed as a[0][1] because it is located at the intersection of row indexed 0 and column indexed 1. The element 3 can be accessed as a[1][2] because it is located at the intersection of row indexed 1 and column indexed 2.
Note that we simply added curly braces to differentiate the different rows of elements. The initialization could also have been done as follows:
int a[2][3] = {0, 2, 1, 4, 3, 7};
};
The following C++ example demonstrates how to initialize and traverse a 2D array:
#include <iostream>
using namespace std;
int main()
{
// a 2x3 array
int a[3][2] = { {0, 2}, {1, 4}, {3, 7} };
// traverse array elements
for (int i=0; i<3; i++)
for (int j=0; j<2; j++)
{
cout << "a[" <<i<< "][" <<j<< "]: ";
cout << a[i][j] << endl;
}
return 0;
}
Output:
Here is a screenshot of the above code:
Code Explanation:
A 3D array is an array of arrays. Each element in a 3D array is identified by a set of 3 indexes. To access the elements of a 3D array, we use three for loops. For example:
#include<iostream>
using namespace std;
void main()
{
int a[2][3][2] = {{{4, 8},{2, 4},{1, 6}}, {{3, 6},{5, 4},{9, 3}}};
cout << "a[0][1][0] = " << a[0][1][0] << "\n";
cout << "a[0][1][1] = " << a[0][1][1] << "\n";
}
Output:
Here is a screenshot of the code:
Code Explanation:
A pointer is a variable that holds an address. Other than using a pointer to store the address of a variable, we can use it to store the address of an array cell. The name of an array constantly points to its first element. Consider the declaration given below:
int age[5];
The age is a pointer to $age[0], the address of the first element of an array named age. Consider the following example:
#include <iostream>
using namespace std;
int main()
{
int *john;
int age[5] = { 19, 18, 21, 20, 17 };
john = age;
cout << john << "\n";
cout << *john;
}
Output:
Note that the first value of the above output may return a different value depending on the address assigned to the first element of the array in your computer's memory.
Here is a screenshot of the code:
Code Explanation:
Array names can be used as constant pointers, and the vice versa is also true. This means you can access the value stored at index 3 of array age with *(age + 3). For example:
#include <iostream>
using namespace std;
int main() {
// an array of 5 elements.
int age[5] = { 19, 18, 21, 20, 17 };
int *p;
p = age;
// output array values
cout << "Using pointer: " << endl;
for (int x=0; x<5; x++) {
cout << "*(p + " << x << ") : ";
cout << *(p + x) << endl;
}
cout << "Using age as address: " << endl;
for (int x = 0; x < 5; x++) {
cout << "*(age + " << x << ") : ";
cout << *(age + x) << endl;
}
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
The elements of an array are accessed using their respective indexes. The index of the element to be accessed is added within square brackets [ ] immediately after the array name. For example:
int john = age[2];
In the above example, we are simply stating that john's age is stored at index 2 of the array named age. This means that john's age is the 3rd value in the array age. Here is a complete C++ example that shows how to access and print out this value:
#include<iostream>
using namespace std;
int main()
{
int age[5] = { 19, 18, 21, 20, 17 };
int john = age[2];
cout << "The age of John is:"<<john;
}
Output:
Here is a screenshot of the code:
Code Explanation:
Here, are pros/benefits of using Array in C++:
What is TCL? TCL is shell application that reads TCL command from its standard input or from a...
C++ Tutorial Summary To learn C++ programming, refer to these tutorials in the given order. This...
What is ServiceNow? ServiceNow is a cloud-based software platform for IT Service Management (ITSM) which...
Training Summary ABAP ( A dvanced B usiness A pplication P rogramming) is the default programming...
What is Continuous Monitoring? Continuous monitoring is a process to detect, report, respond all...
WinZip is a zip tool program that can be used to compress and decompress files with no hassle. It...