Hello YouTubers, I'm Parviz and this is our 8th video in C++ programming language
So the topic of this video is about arrays and also multidimensional arrays
So now, if you remember our video on variables, when you want to store one value, we are going
to use variables. If we want to store two or three values, we need to use more variables
But it's not a good way. By this reason, we have arrays
Arrays are a collection of the elements that are the same type
So it's just a collection of the elements that the types are the same
And the elements are stored in the continuous memory location. Now for example, if you want to create, if you want to store, for example, some four
or five integer values, now for that we need to create four or five variables
Now instead of doing that, we can just use one array to store all the elements
So now let's just use our practical example. Now for this, for creating an array, the first thing you need to specify the data type of
the array. So it's integer. And we are going to just give the name, so numbers
And now in here, we need to just specify the length of the array
So it's optional. You can do this or you can leave it blank
Now in here, I can just say, for example, I want to store 1, 2, 3, 4, 5, 6
Now we have created our array, and we have initialized the items or the elements in the array
Now how you can access to the element of this array. For example, if I want to access to the this first element of the array, how I can do this
Now one important issue that you need to remember, arrays are starting from the zero index
So for example, for accessing the first value of the array, we need to use the zero index
So it is zero index first, second, third, fourth, and fifth. Now we can just say cout, and we are going to just write numbers, and we want to the
zero index. Now if we run this, so now you can see we have one in here
Now you can just let me just copy and paste this. For example, I want to access to the this, for example, this, this four, you can just
start 012, and three. So let me just add three, and you'll see that I am receiving the four value
Now you can see this is the four value. Also you can, for example, you can change an array in here, an element of the array
For example, I want to change index zero to 30. And now if I see out the zero index element, you will see that now I don't have one, instead
we have 30 in here. So you can see the value is 30
Also you can use, for example, you can iterate over the array
Now let me just show you a simple example. Let me remove this
For example, I have a string language. So let me just include a string in here
Okay, now in this array, I'm going to just create some programming language, like for
example, C++, Python, Java, and also C sharp
Okay, now, so we need to, because we are using array. Okay
Now after this, you can, now we can iterate over this. So we can just say for int i, int i to zero, int i zero
And now we are going to check our i value should be less than four
And we want to increment our i value, i++. And now we can see out so we can just say see out language and we are going to just
pause the i in here. And let me just add a new line in here
Like this. Now if I run this
So now you can see we have C++, Python, Java, and C sharp
Now, let's just create an example of a multidimensional array. Now in C++, multidimensional array is really just an array of arrays
So a two dimensional array, also two dimensional array is an array of arrays
Two dimensional array is a 2D array is, it's like a table with rows and column with row
and column. So just it's an array and array, we can call it for multidimensional array
So now let me just show you an example, you will know that what I mean, we're going to
just create a two dimensional array in here. For example, we are going to just specify int x
So we want it should have two rows and also four columns
And now we are going to just specify our element. So 1, 2, 3, 4
And we are going to just say, 5, 6, 7, 8. Okay, now, and here you can see, we have defined a two dimensional array
We have two rows in here, this is our first row, and this is our second row
And we have four, it means four columns. So first, second, and third and fourth column
Or we can say that we have two arrays in here, and every array, we have four elements like this
Now, now how you can access to the elements of this array
For example, I want to access to this first element of my first array
So for this, now you can just say cout. Now if you see in here, this is our first array, and the index of the first array is zero
And this is our second array, the index of the second array is one
Now we can simply say that we want to access to the element of the first index array that
is zero. And the element that we want to access that is zero index, and we are going to just give
we need to add our array in here x and zero. Now if I run this, now you can see that we are receiving first one
Now if you want to access to the element of the second array or second dimension of the
array, for example, I want to access to the, this six, to the six element, or we can say
to the seven element, whatever you want. So first you need to just specify the index of the array itself
So it is the secondary. So we can say that it's our first index and first index array
And after that, you need to specify the element index. For example, it is zero, first and second, and we can just say second
So now if we run this, you will see that we are receiving seven
Now you can see that it is seven. Also, you can iterate over this multidimensional array or two dimensional array
We can just say for, and i to zero, and i is less than two
So this is the first array, and i plus plus. Now we are going to just create another for loop, and it's a nested for loop
So we can just say, and j zero, and j is less than four, j plus plus
And now we can just say c out x i j
And now if we just run this
So now we can see that we have our multidimensional array. So these are the elements
So at the top, and you can see that one, two, three, four, five, six, seven, eight
So like this, you can work with arrays and C++ programming language