Interested in Personalized Training with Job Assistance? Know More
Hello everyone, my name is (name) and we are continuing with this C programming course.
In the last few videos, we had discussed arrays, we saw how one array is made and what is the actual need of an array, then we saw what all types of errors are there and the different types of arrays which are there, single dimensional, multi-dimensional, how these arrays are initialised and created.
All these things we had discussed in the last few videos.
Now, we are going ahead and, in this video, we will be discussing multi-dimensional arrays.
If we talk about multidimensional arrays, the 2D arrays which are there, even that is considered a multidimensional array.
Because we know that multi means more than 1.
And the 2D is obviously more than one dimension.
So, even that we consider a multidimensional array.
But if we talk about visualisation.
How our brain can think about a 2D array or about a 3D array.
So, there is a slight difference in it.
Why? Because, if we talk about our single dimensional array our brain can easily visualise it, it is a single chain, a single linear chain in which there are one element in each and there are values stored in it.
If I talk about the 2D array.
So, the representation of a 2D array is in a tabular format in which there are rows and columns, obviously.
So, even this our brain can visualise in a tabular format, but if you talk about a four-dimensional array, here our brain gets a little confused, how the dimension can be visualised here.
So, here the complexities for the brain increases a little bit, if we talk about our multi-dimensional arrays, which means 3D, 4D, 5D, how much ever arrays are there, whichever dimensional arrays that are there, how these arrays can be handled, how they can be initialised and how can we not get confused with it.
So, all these things we will be seeing in this video.
Once we will quickly see and understand what exactly are multi-dimensional arrays? Multi-dimensional arrays are arrays of arrays.
Since we spoke about our 2D arrays, we know that in our single dimensional array, there used to be one element each.
Which means in each block one value used to get stored earlier.
In this same thing if we say that instead of keeping one, we have listed a separate array over there.
We have made an array altogether.
So, for each element instead of having one element over there, there is one array.
So, we call that array of arrays.
So that's why it is called multi-dimensional arrays.
So, if you visualise this you can see here that we have a single dimensional array here, which is linear in structure, it is like a single line, it is like a chain and the elements in it, the blocks in it, in which the values have been stored.
So, in those blocks if we make one array each.
So, that will be our two-dimensional array.
Now, if we talk about the arrays that we had made in the blocks for each element.
So, in those arrays the elements are there, in that if we make more arrays, those we will call as three-dimensional arrays.
In this way the way as and when our dimensions will increase, our last arrays.
which will be as an element, those elements also we will make arrays.
So, in this way our multi-dimensional arrays will work here.
If you understand the way to write here, then things would become very easy.
So, here we will quickly see his general form.
How it can be generally written, no matter how many dimensions you increase here the way of writing will almost be the same, only the number of brackets here will keep on increasing.
So, we will quickly see how it can be generally written.
So, the first thing that we write, obviously, is data type.
We have seen it early on, when we are making arrays in that we also write data type, after data type comes here array’s name, whatever name we have given to the array that will come after writing the data type and after that like for single dimensional arrays, we were using two brackets, square brackets and inside that we used to write the size of the array, in the same way if I want to make multi-dimensional.
The number of dimension’s array we will have to make, those many square bracket pairs will be required.
So, if I talk about a two-dimensional array.
So, here two square bracket pairs would be required.
So, you can say that if 2D arrays do so, here the size 1 square bracket would come and the size two would also come.
In front of that there is no need to use any because we are making a two-dimensional array here.
In the same way, if I say I want to make a three-dimensional array as well.
So, in a three-dimensional array, what we will do, like here size one is written, here the length of the first array will come.
After that we are making the arrays in the inside elements.
Its size will come in the second block.
In the same way the arrays that are made inside it, those will come here in size three.
So, there will be few things here which you might not be able to understand or it might get very complex.
Inside the array’s element we are making an array inside its array.
So, how much more complex it will get.
So, there is no need to make it so complex and we are going to make it extremely easy because we would be seeing it practical here as well.
So, it will not be confusing at all here, it will not be complex.
As soon as we will see the practical or as soon as we will see its structure.
In which way it is written.
So, it will be extremely easy for us and we will be able to make arrays quickly.
So, we will quickly go ahead and we will discuss one example over here after that we will go on to the practical.
So, if you see here this example basically is neither two dimensional nor it is single dimensional.
This is basically a three-dimensional array, which is basically a multidimensional array.
So, we know that multi means more than one.
So, here there are three dimensions, three dimensions means multi-dimensional array.
So, here if we see here how we will get to know that this is a 3D array and not a 4D or a 2D array.
So, you can see that when I've written the data type and in front of the data type I have written the name of the array, after the name of the array we have written three square bracket’s pairs and these square bracket pairs mean how many dimensions, those many pairs.
So, here there are three dimensions, that's why we have put three pairs here.
What is the first pair saying basically? It is telling the first array’s size which is two, in the same way the elements inside it, with those elements what would be the size of the array? Our second pair is telling that and that is 3.
After that the elements of the arrays, if we want to make elements of even those arrays.
There its size would be 4.
So, this has become a little complex to understand, but as soon as we initialise it but as soon as we see the way of initialising it here, it will be extremely easy for us.
So here we will quickly see in the first bracket we have written two, so our outer array which is there.
So, what would be its size? It will be of size two, which means there would come two elements in it.
It is basically our outer array and basically in it you'll see that it has been separated with a comma where two different curly brackets are made.
So, this is our first curly bracket and the one below is our second curly bracket, since we had given size two over here, the most outer array.
So, that's why we have to make two curly brackets pairs.
So, we have listed two elements over here.
If we go ahead, we can see that in the second bracket 3 is written, when we had two elements in the outer array.
So, these two elements would also be making their own arrays.
So, these two elements which are there, what would be the size of its array, it would be 3 and here you can see that this first curly bracket pair that is there, we have listed 3 elements of it over here.
So, even here, there are three elements and even here there are three elements and all three we have separated with a comma and we have grouped it with curly brackets.
And after that, if we move ahead, since it is a 3D array, and there is a third pair here as well and what does the third pair say, the third pair says that the size of that array will be 4 over here.
So, we saw that the outermost array’s size was two.
So, we have put two curly brackets there and we have separated it with a comma and after that we moved ahead, three was the size of our next array, which means the inside array.
So here we have written three values, we have separated it with the comma, in both the arrays.
After that as soon as we came inside, we saw that there are also 4 sizes listed over here.
So, we have seen here the last part or the last array which was there.
In all of them we have listed four elements.
Why have we listed four? Because we know that the size that we gave here was 4, that’s why the element in each array is coming out to be 4.
So, somewhat in this way we also make the 4-dimensional arrays as well.
Now, if we talk about 4D, 5D, 6D or 7D or as many dimensions as we want.
As per that we will keep on making the pairs of curly brackets and If we want to put values inside we will do that.
If we don't want to put any value, we will keep on making arrays one inside the other.
So, in this way our multi-dimensional array works.
Now, we'll quickly move ahead and straightaway go on to our practical, if we go to our practical then you will see that here we have a program and what is this program going to do is we will first make a three-dimensional array and after making that array, whatever elements or values which were there inside it, we will get it printed over here.
So, here it will be a completely basic program in which we will consider a three-dimensional array and we will be printing its elements on our terminal one by one.
So, we will see how the program starts.
We have started with our preprocessor command and after that we have come to our main function.
So, our main function has started from here and in this main function code’s main part is written over here.
So, our main program starts from here, after that we will see the way of initialising which we had discussed in previous few slides.
The same has been implemented over here.
And here we have made one 3D array.
So, how did we make this 3D array? We will understand that here.
You've seen that in the first bracket which was there the value store is 2.
Here the most outer curly brackets is our main array, which is called X.
How many are X’s elements.
Those would be 2.
You can see that this is the 1st element and this is the second element.
As soon as we go ahead and go to the second dimension, you can see that the size listed over here is 3.
Three sizes means there would be three elements.
So, in the outer array, we have already made 2 elements.
And in those elements a 3 size array would be there.
Are you understanding so far?
This array's size is 3, this is its 1st element.
This is its 2nd element and this is the 3rd element.
So, the below one is in the same way, 1st, 2nd and 3rd element.
So, both the element size has become 3 and 3, which means 3 elements and 3 elements.
In the same way if we go ahead, here you will see the 3rd dimension which is there.
Even its size has been given.
The array that is made under it, its size is given 2.
So, if it is 2, you can see that the inside array’s size is also 2.
Which means this is its 1st element and this is its second element.
In the same way this is the 1st and this is 2nd, 1st and 2nd.
Even below respectively in the same way, the array’s size is 2, 2, 2.
We have not made any more dimensions further ahead.
That’s why direct values have come here, otherwise we could have made the arrays inside of it.
Now it depends on our condition, it depends on us, how many dimension arrays we wish to make.
We are discussing 3 dimensional arrays, which is obviously one multi-dimensional array.
So, in this three dimensional array, we had made only 3 pairs of square brackets.
Hence the degree is, which means how deep the arrays have gone is 3 levels of degree, which means 3 arrays we are making one inside the other. -14:21
So, this was about our initialization part.
Now we will come here on the printing part.
In which ways the values would be printed here on our terminal.
So, you know that we had learned this in a two-dimensional array that out block which is there, if we are talking about 2D array.
We see that; rows and columns are made.
And the index values of one row and one column, as per that we can access that particular value or that particular element.
In the same way like a three-dimensional array is, here also its degree will increase obviously, 3 square brackets will come.
And we cannot divide it in rows and columns because it's already gone deep.
That's why we have divided its index values in three parts and as and when we will increase these 3 parts one by one.
We can access our elements easily here.
So, now we have got three square bracket pairs, inside which if we talk about our first array, inside that also there are few elements, even that is array, inside that as well array is there.
In this way the degree of our arrays has become 3.
Three one inside the other arrays have been made, that is why what we will be doing here, we will be using 3 for loops.
Now we know what a for loop is.
If there is one thing that we want to get executed continuously based on a condition, at that time we make use of for loop, in this case as well we'll be using for loop.
Now how will we be using it? Like we have written here that our size is 2, 3 and 2.
So, what will happen first, the size will remain 0, after that for 0 of this size, the 0 index position of our first array, as per that the other elements would get printed.
After that we will make this 1 and after that all other elements corresponding to it will get printed.
So, if you see a loop, in the loop the most outer loop, how is the work happening over there? We have made I, one variable, which starts from 0 because we know that our index value starts from 0, that's why even I starts from 0 over here.
After that, if we see here our size was 2, that's why we have kept I less than 2 conditions over here.
Now we have also done increment operations over here.
Now we have come here in the first for loop now we have come in the for loop.
We also have to change the second value based on the first value, which was our first index position.
According to that we will also have to change the inside value.
That’s why we are putting a second for loop.
And what is the second for loop doing? This is basically again making a J variable.
Whose value, initialization is zero.
And we know that index value starts from 0, that's why we have started J from 0 and we have taken it to less than 3, which means as long as 3 is less than J, till that time this for loop will go on.
As soon as it gets more than or equal to 3.
This for loop will stop working and it will come out and after coming out, again for loop’s value will increase and it will start executing again.
So, in this way we can access each element in our three-dimensional array.
Now we have come to our second for loop.
You can see that in the second for loop,. there is one more for loop.
Why? Because we have three dimensions and to iterate or repeat the third value continuously.
We have to use the 3rd for loop over here.
In which we have made the use of the K variable.
We have initialised K with 0 and till when we are running K? Till the time the value of K remains less than two.
If it gets equal to two the for loop will stop walking and it will come out and the outside one's value will increase and again it will come inside.
So, as per the outer value the inside value will go on continuing.
In this way the flow works over here.
Now we have come inside our third for loop.
So, for our three dimensions or three pairs, we have used the corresponding loops for them.
Now actually we have to do the printing work to our array.
So, how will we print here? So, you will see elements at X and what we have done here is we have used format specifiers and what we have done and we have listed them in the square brackets.
So, here we have used a format specifier to print is equal to element.
And before that we used all three format specifiers, which will basically be printing I, J and K’s values for us.
Why would they be printing? Because if the I’s value is 0.
For I’s value 0, both our for loops will run.
From where to where? Till 0, 1, 2, J will run and K will run till 0 and 1 and again J’s value will become 1.
And for that again our 3rd for loop will run for 0 and 1.
Again J’s value will become 2.
Again our 3rd for loop will run.
And as soon as the 3rd for loop gets over.
We will come to the second for loop and we will see that now the value of J is not less than 3, it is equal to 3.
That’s why our 2nd for loop will also get closed and it will come to our main for loop.
And the main for loop will increase the value.
So, here the corresponding index position of I, J and K for each element that would be printed in brackets.
And after equal sign we will print their actual elements.
How? We would be accessing our index positions with the help of I, J and K variables.
So, these I, J and K will vary each time.
So, we will access each element in our array.
So, we will quickly execute it and we will get to know how actually a three-dimensional array is accessed.
How it is going on here and which all things are important for us.
Now we have come to our terminal and we will run our command here.
So, in the command we will write GCC and we will write our file name. (typing pause 5 sec)
So here comes our file name.
So here the code is getting compiled.
Now the code has been compiled.
Now we will run it, like you can see, we will increase our terminal.
So here the output has come to us.
You can see that when the index position was 0, 0 and 0.
There our value or the stored element was 0.
We will bring this level down so that we can check here, whether our element was really 0.
So, here you can see that on 0, 0, 0 index position, 0 value was stored.
In this way, here from 0, it has become 1, our 3rd index position basically.
Rest of them will remain 0.
Why? Because those are outside values.
So, based on the outside values, the corresponding outside values that are there.
Those will go on, as soon as the inside one gets over, again they will go outside and change the outside value and then again will come inside and keep changing it.
So here in our program on 0, 0, 0 index position there is zero.
After that if we move ahead on 0, 0 and 1.
1 is stored over here, you can see that 1 is stored over here and on 0,1, 0 index position 2 is stored.
So here our position is 0, 1, 0.
After that if we come to 0, 1, 1, so the stored position is 3.
So, here 3 is stored.
We will know that it is stored on 0, 1, 1, in the outer array size is 2.
That's why Its index position would be 0 and 1.
The zero index position would be the outer one.
So, 0 is there with us.
After that when we will come inside.
So, what we did inside, our array on the 0th index position, we have considered that.
Now its index position is 0 and for this, it is one and for this, is 2.
We saw that our one index position, we are considering it.
So, this is on 1 index position.
As we go deeper, we will see one index position’s array.
Whichever element is on 1 index position, that we have to consider.
So, as we come here, this is a 0 index position and this is 1 index position.
And 3 is stored on my one index position.
So, in this way our arrays are stored in multi-dimensional arrays.
And here there are elements at the respective positions, we have printed here with the help of this example.
Now we will move on to our presentation and see how we have worked on our multi-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.
If we talk about the next topic, that would be the pointers.
What are exactly the pointers, how do they work? All these things we will discuss in our next video.
So, we'll meet in our next video till that time you keep on practising multi-dimensional arrays.
Have a friend to whom you would want to share this course?