Get the size of the array
To get the size of the array, you can usesizeof()
Operator:
Example
int myNumbers[5] = {10, 20, 30, 40, 50}; cout << sizeof(myNumbers);
result:.
20
Why does the result appear as 20 instead of 5 when the array contains 5 elements?
This is becausesizeof()
The operator returns the size of the type in bytes.
To find out how many elements there are in an array, you must divide the size of the array by the size of the data type it contains:
Example
int myNumbers[5] = {10, 20, 30, 40, 50}; int getArrayLength = sizeof(myNumbers) / sizeof(int); cout << getArrayLength;
result:
5
Loop through the array using sizeof()
However, by using the above examplesizeof()
Methods, now we can create loops that work for arrays of any size, which is more sustainable.
Rather than writing:
int myNumbers[5] = {10, 20, 30, 40, 50}; for (int i = 0; i < 5; i++) { cout << myNumbers[i] << "\n"; }
It is best to write it as:
Example
int myNumbers[5] = {10, 20, 30, 40, 50}; for (int i = 0; i < sizeof(myNumbers) / sizeof(int); i++) { cout << myNumbers[i] << "\n"; }
Note that in C++ version 11 (2011), you can also use the "for-each" loop:
Example
int myNumbers[5] = {10, 20, 30, 40, 50}; for (int i : myNumbers) { cout << i << "\n"; }
It is important to understand different ways of looping arrays, as you may encounter them in different programs.
Multidimensional array
A multidimensional array is an array of arrays.
To declare a multidimensional array, define the variable type, specify the array name, followed by square brackets, specify how many elements there are in the main array, and then follow another set of square brackets to indicate how many elements there are in the subarray:
string letters[2][4];
Like normal arrays, you can use array literals - comma-separated lists inside curly braces. In a multidimensional array, each element in the array literal is another array literal.
string letters[2][4] = { { "A", "B", "C", "D" }, { "E", "F", "G", "H" } };
Each set of square brackets in the array declaration adds another dimension to the array. An array like the one above is called an array with two dimensions.
An array can have any number of dimensions. The more dimensions the array, the more complex the code becomes. The following array has three dimensions:
string letters[2][2][2] = { { { "A", "B" }, { "C", "D" } }, { { "E", "F" }, { "G", "H" } } };
Access elements of multidimensional arrays
To access elements of a multidimensional array, specify the index number in each dimension of the array.
Access this statementletters
The values of elements in the first row (0) and the third column (2) in the array.
string letters[2][4] = { { "A", "B", "C", "D" }, { "E", "F", "G", "H" } }; cout << letters[0][2]; // Output "C"
Remember: array index starts with 0: [0] is the first element. [1] is the second element, and so on.
Change elements in multidimensional arrays
To change the value of an element, refer to the index number of the element in each dimension:
string letters[2][4] = { { "A", "B", "C", "D" }, { "E", "F", "G", "H" } }; letters[0][0] = "Z"; cout << letters[0][0]; // Now output "Z" instead of "A"
Loop through multi-dimensional arrays
To loop through a multidimensional array, you need to use a loop for each dimension of the array.
The following example outputletters
All elements in the array:
string letters[2][4] = { { "A", "B", "C", "D" }, { "E", "F", "G", "H" } }; for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) { cout << letters[i][j] << "\n"; } }
This example shows how to loop through a 3D array:
string letters[2][2][2] = { { { "A", "B" }, { "C", "D" } }, { { "E", "F" }, { "G", "H" } } }; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { cout << letters[i][j][k] << "\n"; } } }
Why use multidimensional arrays?
Multidimensional arrays are ideal for representing grids. This example shows their practical use. In the following example, we use a multidimensional array to represent a small battleship game:
// We place "1" to indicate that there is a ship there.bool ships[4][4] = { { 0, 1, 1, 0 }, { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 } };
// Through these changes
Track how many ships the player hit and how many rounds the player has played
int hits = 0; int numberOfTurns = 0; // Allow the player to continue until they hit all four shipswhile (hits < 4) { int row, column; cout << "Select Coordinates\n"; // Require players to enter a line cout << "Select line number between 0 and 3:"; cin >> row; // Require players to enter a column cout << "Select a column number between 0 and 3:"; cin >> column; // Check if these coordinates exist for ships if (ships[row][column]) { // If the player hits a ship, delete it and set the value to zero. ships[row][column] = 0; // Increase hit counter hits++; // Tell the player that they hit a ship and how many ships they have left cout << "Hit! Leftover" << (4-hits) << "Ship.\n\n"; } else { // Tell the player that they failed to hit cout << "Missed\n\n"; } // Calculate how many rounds the player has made numberOfTurns++; } cout << "Victory!\n"; cout << "You are " << numberOfTurns << "Winning in the Wheel";
at last
This is the end of this article about C++ getting array sizes and multidimensional array operations. For more related C++ array sizes and multidimensional array content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!