In this article we are going to talk about C++ Arrays And Multidimensional Arrays, so c++ arrays are collection of the elements that are the same type. if you remember our article on variables, when we want to store one value we are going to use variable. If we want to store two or three values we need to use more variables. but it is not a good way, by this reason we have arrays. also the elements of array are stored in the continues memory location.
Also you can check more articles on c++ programming language
1: C++ Introduction & Program Structure
2: C++ Variables And Data Types
So for creating of array , first of all you need to specify the data type of the array, in the second step you need to give a name for the array, and after that initialize the elements of the array.
1 |
int arrayName[] = {arrayElements} |
For example we can write like this.
1 |
int numbers[6] = { 1,2,3,4,5,6 }; |
In the above example, we have also specified the length of the array, but it is optional, you can leave it blank like this.
1 |
int numbers[] = { 1,2,3,4,5,6 }; |
So now if you want to access the specific element of the array, you need to use the array index of the element, and make note that the array index starts from the zero index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<iostream> using namespace std; int main() { int numbers[6] = { 1,2,3,4,5,6 }; //int numbers[] = { 1,2,3,4,5,6 }; cout << numbers[0]<<endl; cout << numbers[3] << endl; return 0; } |
This will be the result.
1 2 3 |
1 4 Press any key to continue . . . |
Also you can iterate over arrays using for loop like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<iostream> #include<string> using namespace std; int main() { string languages[4] = { "C++", "Python", "Java", "C#" }; for (int i = 0; i < 4; i++) { cout << languages[i] << "\n"; } return 0; } |
This will be the result
1 2 3 4 5 |
C++ Python Java C# Press any key to continue . . . |
Now let’s talk about multi dimensional array, a multidimensional array in C++ is really just an array of arrays. also a two-dimensional array is an array of arrays. a two-dimensional array or 2D array is like a table with rows and columns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<iostream> using namespace std; int main() { //two rows and 4 column int x[2][4] = { { 1,2,3,4 }, { 5,6,7,8 } }; cout << x[0][0] << endl; cout << x[1][1] << endl; return 0; } |
So above code is a two dimensional array, where we have two rows and four columns, or also we can say that we have two arrays and there are four elements in each array. for accessing the element of multi dimensional array, first of all you need to specify the index of the array, and after that the index of the array element.
1 2 3 |
1 6 Press any key to continue . . . |
Also you can iterate over multi dimensional array like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include<iostream> using namespace std; int main() { //two rows and 4 column int x[2][4] = { { 1,2,3,4 }, { 5,6,7,8 } }; for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) { cout << x[i][j] << endl; } } return 0; } |
This will be the result
1 2 3 4 5 6 7 8 9 |
1 2 3 4 5 6 7 8 Press any key to continue . . . |
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email
https://waterfallmagazine.com
Greetings! Very useful advice within this article!
It’s the little changes that will make the most significant changes.
Thanks for sharing!