 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain the use of 1D and 2D array in C program. Before starting off my explanation, just one update. If you go to detail your description of this video, you will find links of various playlists related to C programming. Now I am coming to start my explanation. So first I am going to explain the use of 1D array and then I will explain the use of 2D array. So you might have declared variables to store values. So let's say I have one variable that is in A. So if I declare this variable, so in memory, I will be having a memory block where I can store the value which is available inside A. So if I write A equals to 5, so 5 will be assigned into this location. Now if I try to store one more value that is 10 into same variable A. So what will happen? 5 will be erased and 10 will be assigned to that location. So the conclusion is one variable can store one value at a particular point of time. So if you want to store more than one value at same time, so you can't use this approach. So for that you need to declare more than one variable. So let's say I want to store only two values. So I have declared two variables. So here A is having 5 and B is having 10. So this is quite easy. If you have two numbers, you can simply declare two variables. So now you might be thinking what is the requirement of adding. So now you will understand if you have 10 elements or let's say you have 100 elements. So we have two requirements, 10 and 100. So now tell me, is it easy to declare 10 variables? So you will say, yeah, it might be easy because I declare 10 variables. But if I say I need to store 100 elements, so now you will think to declare 100 variables is not so easy. So first we need to identify 100 different names and then we need to declare all those variables in a particular data. So that will be time consuming. So to solve this problem, like if you want to store data in bulk, large number of data you want to store, then don't declare unnecessary variables, just try to use add it. So now I'm using 1D array here. So if I use 1D array, so I can write this. So this is the array declaration. So here you can see in this data type, this A is the name of array and this 10 is the sign. So A array can store 10 elements together at same point of time. Now if you want to store 100 values, so you can simply write this in a square bracket 100. So this time A variable can store 100 values at same point of time. So don't panic if you have a large number of data, just declare 1D array or 2D array as per your requirement. So now if we compare both the scenarios, so both are available as per your requirement, both are optional. There is no necessity to use an array. Like if you have 10 numbers, so you might be thinking is it compulsory to use an array? No, it is purely optional. So it depends upon your requirement. If you can declare 10 or 100 variables, then you can simply use them. But if you want to reduce number of variables, then you can go with that. So you have both the options, you can choose either option that is fulfilling your requirement. So this is brief introduction or you can say brief requirement which is explaining the use of arrays. Now I am going to explain 1D array and 2D array in detail. So till now I hope you understood the requirement of arrays. So I am just repeating it in a single line. So if you want to store more than one element or large number of data into a single variable, then prefer array. So if we declare 1D array like this in A of 10, so what will happen in the memory? So 10 blocks continuously will be allocated. So these are 10 blocks which are allocated continuously. This is name of this array A and here you can store values. Now you might be thinking each location of array is represented through array. So how we will process it, how a memory or how user will identify which location we want to store the value or which location we want to access because each location is sharing same name. So to solve this problem we have one more term that is known as index. So these are the indexes starting from 0 and it goes till size minus 1. So here this span is representing size of array. This span is representing size of array and these are the indexes. So now if you want to store any value, you can write this. Let's say A5 equals to 50. So it means A array fifth index will be having value 50. So 50 will be stored here. So this is A array, its index is 5 and 50 is assigned to A. If you write A0 equals to 10, so 0 means first location so 10 will be assigned at first location. Now let's say if I write this x equals to A5, it means the value which is stored at A array at fifth index will be assigned into x value. So you can use this at left hand side as well as right hand side. Now one more thing, this 5 is index. This 5 and 0 is also index. So if you put any value within square bracket and you have data type as well, so then it will be known as size. But if you don't have any data type and still you are putting some value in this square bracket, then it will be known as index. So this way you can easily identify like we are putting index or we are putting size. So this is the memory allocation of 1DI. Now if you want to read values from user, then in that case you can simply implement loop. So you can write this. So this is a single loop which is leading 10 elements from user and those 10 elements will be stored at different indexes of 1DI. So this is quite simple. It will repeat 10 times from 0 to 9. Then here this AI will be replaced with A0, then A1 and so on. So first number will be stored at A0 position, second number will be stored at A1 position and so on. So here you can realize we have common variable name for array. So because of that we can repeat scanup statement with the help of loop. But if you have 10 different variables like A, B, C, D, E, F and so on. So then in that case you can't repeat scanup statement with for loop because name of variables are different. So in case of array you have another advantage in reading and printing elements of array with the help of for loop. So I hope this way you understood how we can manage 1D array. Now I am going to explain the requirement of 2D array. Again I am using these things and if you want to watch videos related to 1D array or 2D array programming. So I already told you you can go to detail or description of this video and there you will find links of players related to 1D array and 2D array. So you can follow them and you can watch various variety of videos on these topics. So now again I am going to create 3 blocks. So we have 3 things variable 1D array and 2D array. So sequentially I will tell you like why we shifted from variable to 1D array and why we are going to shift from 1D array to 2D array. And I am taking one scenario. So let's say there is a class in that class number of students are 100 and they are studying 5 different subjects. So if I want to declare variables, so let's say I want to store subject 1, 100 students marks. So in this case I need to declare 100 variables or subject 1. So immediately you will say like we can use 1D array. We can write int as 1 and 100. So one variable is capable of storing 100 variables. So we left out this situation and we opted this situation. Now one more requirement. Subject 2 also has 100 students. So in this case again you need to declare 100 variables. So we can solve this using 1D array. So you can declare one more 1D array. And in this way you can declare 3 more for subject 3, then subject 4 and subject 5. So in this case you need to declare 500 variables but in case of 1D array we just declared 5 variables in terms of 1D array. So you can say we declared 5 1D arrays. Those are managing 520. Now again we have more than one variables and we can reduce this number as well. So now I am moving this requirement to 2D array. So in case of 2D array you can write it like this as 500. So only one variable that is named as s can store 500 values together. So this is the declaration of 2D array. And here this first subscript is saying rows and second is for columns. So here 5 rows and 100 columns in each row will be there. So you can imagine 5 by 100 matrix. So that is related to 2D array. So this way we reduce 500 variables into 5 and 5 variables into 1 with the help of 2D array. So if you want to reduce number of variables so you can prefer 1D array. Still you want to reduce number of variables you can go with 2D array. So now I hope you understood the requirement of 2D array. So again I am taking one simple scenario so that I can explain you the memory structure and how we can read values for 2D array from here. So I am increasing this thing and also remember use of 2D array is also optional. Use of 2D array is also optional as 1D array. So I am taking one simple example. So let's say this is my 2D array A34. So this is representing row and this is representing column. So if I draw this structure so this will look like this 0, 1, 2, 0, 1, 2 these are indexes. These are indexes. So these are row index and these are column index. And if you want to identify a particular location so you need to use these indexes like this. So 0, 0, 0, 1, 0, 2, 0, 3. So first we need to put row index and then we need to put column index. Then 1, 0, 1, 1, 1, 2, 1, 3, 2, 0, 2, 1, 2, 2 and 2. So this way this 2D array access the memory. So this is general memory location. In case of actual memory location it happens like this. So this is actual memory allocation. So actually in memory this representation takes place. So it will have 4 elements, it will have 4 elements and 4 inputs. So here you can imagine this first row is this, then second row and then third row. But for calculation purpose we can draw this on paper and we can execute our problems based on this but remember actual memory location will be this. So indexes 0, 0, 0, 1, 0, 2, 0, 3, then 1, 0, 1, 1, 1, 2, 1, 3, then 2, 0, 2, 1, 2, 2 and 2. So this way indexes are allocated in memory. So it is equivalent to this. So we can use these indexes with the help of loop. So here we need to rotate 2 loops so that we can read values from the. So here in this case you need to write loop like this. So first loop will repeat low times and second loop will repeat column times. And inside this inner loop I can write scanf, %d and %a of id. So this way you need to read the elements of the help of 2DL. So first this will start from 0 then control will be transferred to this. So this inner loop will repeat 4 times. So when this inner loop will be repeating indexes will be 0, 0, 0, 0, 1, 0, 2 and 0 because i is always 0 and j is repeating 4 times. After completion of this loop i will be incremented to 1 and j will again repeat 4 times. So in second rotation indexes will be 1, 0, 1, 1, 2, 1, 3 and so on. So this way you can see either you can follow this structure or you can follow this structure indexes will be same. So this way you can read elements for 2D array problem. So I hope with this explanation you understood the requirement of 1D array and 2D array in C programming. And I tried to explain the requirement of 1D array and 2D array as compared to variables. So I told you why we require variables then how we can convert them variables into 1D array and how we can convert variables into 2D array. So this was a bold explanation. Now if you want to see practical demonstration that are implemented with the help of compiler. So for that you can either open my channel and go to playlist and search for various playlist related to 1D array and 2D array. Or second thing you can go to detail or description of this video where you will find links of those playlist so you can directly access those from there. So do watch those videos so that you can understand how actually we can implement 1D array and 2D array in C program. So I hope you understood whatever I explained in this video. Thank you for watching this video.