Hello everyone, my name is (name) and we are continuing with this C programming course in which, in the last video we had discussed types of arrays.
In the types of arrays, we had seen the first type, one dimensional array.
If we talk about the second type of array, we call that multidimensional array. We learnt this in the last video, which are the two types of arrays.
Our first type is single dimension and the second type is multi-dimensional array.
After discussing all these things, now, we will straight away come to our topic which is called two dimensional arrays.
This is one type of multi-dimensional array because in this, the square bracket pair is more than one, which means there are two, that is the reason why we call it a two-dimensional array as well.
If we go ahead, we can see that this is one simple example.
It's a diagram in which you can see there's a form of table and there are a number of rows in it and number of columns as well, which are equal, which means there are three rows and three columns.
Like we were talking about index value in a single dimensional array, that it starts from 0.
0, 1, 2, 3, if we have N number of values, our index value comes till N minus 1.
So, in the same way our two-dimensional array, in that two-dimensional array has a different indexing for the row and different indexing for the number of columns.
You can see it’s written as row 0, row 1, row 2 and column 0, column 1, column 2, somewhat in this way the columns are being written.
So, here from 0 our index value starts.
And you can see inside, if you consider the X as the array.
So, if X is the name of the array, you will see ahead 2 square brackets and the first element that is there in that you will see both as 0.
Why? As the row’s index is 0 and the column’s index is also 0.
As we go ahead you would see that, row’s index remained at 0.
But the column’s become 1.
So, in the 1st square bracket,
what our row represents, in that we are writing 0 and in our second square
bracket, we are writing 1.
So, in this way, these are our two-dimensional arrays.
We can represent it in this way visually.
We will go ahead and see how these 2D arrays will be declared.
“A 2D array can be considered as a table which has X number of rows and Y number of columns”.
If we understand this in layman's terms, what does it say? That a two-dimensional array is like a table in which the number of rows we define with X and the number of columns, we define it with Y.
So here in the syntax, we can see that like in the last error, even here, we will write one data type, then we will write the name of the array.
But this time we are using two square brackets, the first square bracket is showing us the rows, which means X is showing the number of rows, and Y is showing the number of columns.
In what? In our table, which is a visual presentation of a two-dimensional array.
So, we say for the sake of it that the two-dimensional array is growing from both the side, that is from the side of the row and from the side of the column as well, that's why it is called 2D.
And its representation is also in a way that you have to put two square brackets, one for rows index and other for columns index.
So, this was about our declaration, and we have seen the example as well.
If we have made an array named roll, so 20 will be our number of rows in our roll array and 12 would be the number of columns.
So, in this way, the declaration in 2D array works.
If we move ahead, we can see how we can initialise the two-dimensional arrays.
Now, there are different ways of initialising it but these two ways are the most common ones.
And we will see them together, how they are different from each other.
alright?
So, you will see here we have made one array of INT data type A, which is a 2D array, of course, because in it, there is 3 as well as 4, 3 is showing the number of rows and 4 is showing the number of columns.
So, as soon as you put the equal to sign and open the curly brackets, what will you do? You will put the entire row in one more curly bracket.
So, here as an example, we will see in the first row the values were coming 1, 2, 3 and 4.
So, what we will do is we will start the curly brackets, we will write 1234 in it, and wherever our row will get over, you can see that here the number of columns is four.
So, the elements in the row will also be four.
So, here after the four elements, we will close the curly bracket and we will put the comma and start the next row.
So, somewhat in this way, we will be initialising our two-dimensional array.
Apart from this there is one more way which you can see here in or, you can see it below.
What happens in this if you have defined it from early on, that you will be needing three number of rows and four number of columns.
In this way also, if you write simply in the curly brackets, direct values.
Our compiler will understand that your number of columns are four, after every four, it will consider a new row and it will do it till the time the three rows are not completed.
So here you can see that 1234 will be considered as one row and from 5 the new rows would be starting and 5678 would be put into a new row.
And after that again 910 11 and 12 will be put into the third new row.
What will happen with this, our both the conditions of number of rows and number of columns, those will be satisfied.
And that's why our smart compiler will be able to interpret it.
So, these were the two ways we can initialise our two-dimensional arrays.
Now, we will go ahead and see that we have declared and we have initialised.
But if we want to use the same values, how can these be extracted? Let's see quickly.
So,” the elements of a two-dimensional array are accessed by using the subscripts that are row index and column index”.
So, we have seen this in the previous single dimensional array that if we are declaring any of the one array, at the time of declaring if we are writing data type before the array’s name.
At the time, our inside the bracket values will represent the size.
And here in a two-dimensional array, what does it represent? It represents the number
of rows and number of columns.
But if the data type is not written in front, in front of the array’s name.
That will not be called a declaration.
We will call it as we are trying to access the two-dimensional array’s elements.
What will happen at that time, the values in the square brackets, they would be representing our index value.
In this example, you would see that we have made a variable named A1.
Whose type is integer and B is a two dimensional array and it's second index, row’s second index and column’s third index.
These two positions wherever they will be matched in our table, there the value which is stored, we will be assigning that value.
Where? Inside the A1 variable.
So, in this way if we want that in our two-dimensional array, we have stored the value or assigned it.
We want to access that.
In this way with the help of an index number we can find it and access it.
So, this was the method of declaring two-dimensional arrays, initialising them and accessing them.
So, these three things we have seen about two-dimensional arrays and, in this way, the multi-dimensional arrays which means, 3D, 4D and further however many dimensions we can see.
All those types of arrays also work in this same way and they are initialised in the same way or declared in the same way.
And we will be accessing them this way.
So, we saw in this video about two-dimensional arrays.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Share a personalized message with your friends.