Accessing Array Elements in C++

Accessing Array Elements in C++

In the C++ programming language, user-defined array element data values ​​are accessed and managed from their storage location using the indexing operator []. Always remember that the starting index location of any stored C++ array element starts at 0 by default. This means that the first array element declared in the program is located or stored at index 0. The second array element in the same sequence is located or stored at index 1. Similarly, other array elements in a continuous sequence are represented and stored in user-defined arrays.

Accessing Array Elements in C++

Syntax of accessing array elements in C++.

array_name[index];

Element of accessing array elements in C++.

  • array_name – This is the name of the user-defined declared array in the current program.
  • index – This is the index storage array (position) location of the array element declared in the current program. You want to access it from its index location in the current program.

Accessing and Printing Array Elements in C++.

So, let’s declare or define a simple array in the C++ programming language and initialize it with its default storage value. We can then access and print each array element as needed.

Example of accessing array elements in C++.

#include <iostream>

using namespace std;

int main() {

// Here we declare a one-dimensional array and initialize an array element with the array element

int array[5] = {9, 11, 200, 40, 22};

// Here we access and print the individual array elements’ values

cout << “First array element index location 0 is – ” << array[0] << endl; // Result – 9

cout << “Second array element index location 1 is – ” << array[1] << endl; // Result – 11

cout << “Third Array Element index location 2 is – ” << array[2] << endl; // Result – 200

cout << “Fourth Array Element index location 3 is – ” << array[3] << endl; // Result – 40

cout << “Fifth Array Element index location 4 is – ” << array[4] << endl; // Result – 22

return 0;

}

Explanation of accessing array elements in C++.

  • In this example, the array array is default initialized with the values ​​{9, 11, 200, 40, 22}; and here we can access each array element one by one using their relative storage index location (0 to 4).

Modifying C++ Array Elements.

In a C++ array program, users can modify the element values ​​of an existing array by assigning or providing a new value to a specific array index. This process modifies or replaces the array element using the array index location, not the array’s index location.

Example of Modifying C++ Array Elements.

#include <iostream>

using namespace std;

int main() {

// Here we declare a one-dimensional array and initialize its array

int array[5] = {300, 400, 500, 799, 900};

// Here we modify the fourth array element (index 3) location

array[3] = 1000;

// Here we access and print the modified array element value

cout << “Preview of the modified fourth element (index 3) at location – ” << array[3] << endl; // Result is – 1000

// Here we print the entire array after the fourth modification value

for (int p = 0; p < 5; p++) {

cout << “Each array element stored at index location ” << p << ” – ” << array[p] << endl;

}

return 0;

}

Explanation of Modifying C++ Array Elements.

  • In this example, the value of the stored fourth array element (index 3) is updated from 799 to 1000.

Accessing and Printing 2D Array Elements in C++.

So, let’s declare elements in a 2D array and access individual array elements in row and column order from their storage index location, and display the result.

Example of 2D Array Elements print in C++.

#include <iostream>

using namespace std;

int main() {

int array[3][4] = {

{9, 3, 1, 2},

{4, 8, 6, 5},

{11, 21, 31, 23}

};

// Here we are accessing a specific array element using its storage location

cout << “Element of stored array index location is [1][1] = ” << array[1][1] << endl;

cout << “Element of stored array index location is [2][3] = ” << array[2][3] << endl;

cout << “\n Element of 2-Dimensional Array!\n”;

for (int p = 0; p < 3; p++) {

for (int q = 0; q < 4; q++) {

cout << array[p][q] << ” “;

}

cout << endl;

}

return 0;

}

Explanation of 2D Array Elements print in C++.

  • Here in this example, we are displaying a 2-dimensional array containing 3 rows and 4 columns and displaying its particular index array location element. In which array[1][1] displays the element at row 1 and column 1.

Accessing and Printing 3D Array Elements in C++.

So, let’s declare the elements in a 3D array and access individual matrix multi-dimensional array elements in row and column order from the storage index location and display the result.

Example of 3D Array Elements print in C++.

#include <iostream>

using namespace std;

int main() {

int array[2] [3] [4] = {

{

{77, 66, 55, 44},

{33, 22, 11, 10},

{10, 9, 8, 7}

},

{

{25, 27, 29, 30},

{33, 77, 99, 44},

{79, 34, 73, 87}

}

};

// here we Accessing a specific desire 3d array element stored location

cout << “Default Element storage location at array[1] [1] [3] = ” << array[1] [1] [3] << endl;

cout << “\n List 3D Array Elements Data\n”;

for (int p = 0; p < 2; p++) {

cout << “Structure of 3D Array ” << p << “!\n”;

for (int q = 0; q < 3; q++) {

for (int r = 0; r < 4; r++) {

cout << array[p][q][r] << ” “;

}

cout << endl;

}

cout << endl;

}

return 0;

}

Explanation of 3D Array Elements print in C++.

  • In this example, we display a 3-dimensional matrix array containing array[2][3][4] as column values ​​and its particular matrix index array location element. In this example, array[1][1][3] displays the array index location element.

Leave a Reply