 Hello, hi everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. Today we are having day 16 of this C and C++ coding bootcamp. And in today's session I am going to discuss about 2D array. So in previous two sessions I discussed about 1D array. So I hope you have already practiced the programs which I demonstrated you. If not then just practice them so that you can become expert in programming. And today I will be explaining you the concept of 2D array, how we can create 2D arrays in C programming and then we will be discussing few programs based on that. Okay, with this I am going to start today's session for you so that you can understand the concepts. So before starting, if you joined this YouTube platform Sanjay Gupta Tech School for the first time and if you want to know about me, so I am Sanjay Gupta and this slide tells everything about me, what I am doing, what I have done, how much years of experience I have. So you can just read and understand who is your instructor. Moving forward, this I show in every session of mine because I love consistency and consistency is the key. So if you have consistency in your life, no matter whether it is learning or job or any other thing, so you will become successful. So if you are following this bootcamp, so be consistent and do regular practice. If you have any problem and you want to ask any doubt, so you can join this telegram group and in this telegram group whatever question you will be asking, I will be helping you and will answer all the questions. So do follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram and Telegram and you will receive all the timely notification and all the important links are available in the video's description. And one thing which I want to share like soon I am going to open limited seats, online offline batches for different technologies. So this is new update that I want to give you before starting the session. So do follow the telegram group of Sanjay Gupta Tech School where everything related to that will be shared. So initially I have decided like few batches I will be making where I will be giving 100% scholarship so that people can learn things free of cost. And later on there will be minimal charge and like whoever want to grow in IT sector, so they will get benefited. So with this note, I just want to start 2D array discussion with you. So we are going to understand 2D array that we use in C programming. So first of all we need to understand the concept. So basically if I compare 2D array with 1D array, so in 1D array like you all know we create a collection of multiple elements and all those elements are of similar data type. Now if you want to combine more than 1D arrays where you have single rows and different elements are available. So sometimes there is a requirement like you have different different rows but you want to club them. So if I take a real life example, so sometimes in mathematics we create matrix, in matrix we have number of rows and number of columns. So if you want to store that matrix, so 1D array is not enough. So to solve that problem what we need to do we need to create a 2D array. So basically a 2D array is a collection of more than one 1D arrays. So in one 1D array we have one row but if we combine more than one 1D arrays, so it will become 2D array. So you can say 2D array is also known as collection of rows and columns. Because multiple rows are there and in those multiple rows you will be storing some values so columns will be there. So here you can see we have a declaration syntax. So first of all you need to define data type, then you need to give the array name and then we have row and column. So you need to specify number of rows and number of columns. So if you want to see the example, so we have 2 examples int A34. So here 3 is basically number of rows and 4 is number of columns. If you see the second example which is float B2 of 3. So 2 is basically number of rows and 3 is representing number of columns. So this way you will be creating 2D array. And as we discussed earlier in previous sessions in case of array, no matter how many elements you store in the array, name of that array will be same always. So if you are creating array of 3 by 4 size, this first one, so total 12 elements will be there and all those 12 elements will be storing under variable A. So no matter you are creating 1D array or you are creating 2D array, name of array will be same for all the elements. So I hope with this you are able to compare how we create 1D array and how we create 2D array in C program. Now this is very much important to understand like how a memory allocation is done. So if you have a 3 by 4 array, 2D array, so as I already told you 3 is representing number of rows. So in the below diagram you can see we have 3 rows and 4 is basically representing number of columns. So in this diagram you can see we have 4 columns. So rows are horizontal and columns are vertical and you can see 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. These are values those are stored and 0, 1, 2, 3 and 0, 1, 2 these are indexes. Now you might be thinking if I want to access particular element from 2D array or if I want to store a value to particular location of this 2D array. So how I can do that? So you need to know the indexes. You need to know the indexes and in case of 2D array index will be framed with the help of row index plus column index. So this is a 2D array representation but in memory this will be looking like this. So if you see this slide so you will be clearly able to understand the concept. So here you can see we have 3 by 4 matrix and in memory actually this happens. So first location will be 0, 0, second location index will be 0, 1, third location index will be 0, 2, fourth location index will be 0, 3. So these 4 elements are available here. Then we have 5, 6, 7, 8. So for this we have 1, 0, 1, 1, 1, 2, 1, 3. So again 4 elements and 4 different indexes. Then we have 4 more elements 9, 10, 11, 12. So here you can see we have indexes 2, 0, 2, 1, 2, 2, 2, 3. So first index will be for row and second index will be for column. So this way you just need to understand how 2D array is represented in memory. So if you are a BTEC student then for you this is very much important. If you are non-technical folk and you are trying to learn a programming language so for you also it is very much important. And if you are a technical person and if you are not aware of the fact of 2D array. So for you also this session is very much important. So I hope with this diagram you are able to understand how actually elements of 2D array stores in memory. And anytime if you see any doubt you can ask in the chat, in live chat. And I have already shared the QR code for the telegram group where you can ask questions. And I will be answering your questions. So with this explanation I think right now I don't have any question. Still if you have doubts you can ask and I am moving to next slide. But if I see any question related to this I will come back and explain. So moving forward now we need to understand how basically we can initialize values in 2D array. So initialize here means how we can assign values to 2D array. So it is very much important to understand how we can assign values to 2D array. So we have three declarations here. First one is int a of 2 by 3. So 2 by 3 2 rows 3 columns. So we have total 6 values and curly basis. So what will happen here pair of 3 3 elements will be created. So first pair will be 1 2 3 and second will be 4 5 6. So how I identified like how many values will be there in the pair. So that I identified through the column size. So column size is 3. It means in one row there will be maximum 3 elements. So 1 2 3 1 row 4 5 6 second row. This way it is happening. Then we have second which is not okay. You can see here it is not okay because second block is blank. We don't know the column size. So if you don't know the column size you won't be able to justify like how many elements will be there in one row. So row size is 2. So it doesn't mean you will be having 4 4 elements. Right. So 2 row size maybe like 5 3. Right. So it may also happen 6 2. It may also happen. So we don't have the column size here. That's why this statement is invalid. And if we see the third statement which is having row blank but column size is there. So column size is 3. It means we know in one row there will be total 3 elements. So 1 2 3 it will be a group of elements first row 4 5 6 second row 7 8 3 right. So if elements are less then it is okay. No problem but if elements are more as per the size then that will be a problem. And at the time of initialization column size is must but row size is option. So I hope with this explanation you are able to understand it clearly. Still if you have doubt you can ask and I will be happy to answer your questions. Right. So whenever you want to initialize any 2d array. So either go through first example or third example. Second is inappropriate. So you want to be able to do that. Right. So still now what we done we just understood how we declared 2d array. We understood how memory allocation takes place. We understood how we can initialize a 2d array. Right. So this is how we can read values into a 2d array. Right. So here you you will realize the importance of nested loop. So if you remember in my previous sessions I explained you nested loop in one of the session. So if I show you the session tracker. So here you can see in day 13 session I explained you how we can implement nested loop. And that nested loop application we are using here in 2d array. Right. And here I provided lots of like 62 videos that I created on nested loop. So those are available here with the link of like that playlist link is available. You can just click and you can just watch all the videos and practice. Right. So this is the way like we can read a 2d array. So here you can see we have nested loop. So first loop always iterates basis on number of rows. And second loop always iterates basis on number of columns. Right. So this this you need to actually understand. Okay. So first like I'm taking example of this previous array declaration which is a 3 by 4. So here you can see row sizes three. So that's why first loop will iterate three times and second loop will iterate four times. Okay. So wishes is asking can you show the memory location for third one. So basically wishes I got your point. You might be confused like first row is having whole like three elements. Second row is having three elements but third row is having two elements. So what will happen with the third element. So for your confusion I must say that third element will be zero here. So it will be like three by three matrix and ninth element will be zero. So I hope now it answers your question. So we were discussing about reading of 2d array. So first loop you can see it is iterating three times and second loop is iterating four times. So loop is starting from zero because because of index index always starts from zero and condition is less than three. So it will iterate for zero one and two when I will become three so it will terminate when J will become four it will terminate. And if you are facing difficulty in understanding this reading process. So first of all you need to understand the working of nested loop. Right. And if you are able to understand working of nested loop then only you are you will be able to understand this code snippet. Right. So here we have first loop that will iterate three times second loop iterate four times when first loop iterate first time first rotation in that case in a loop J loop will be trading four times. So combination will be zero zero zero one zero two and zero three. Right. When this inner loop J loop complete completes its execution then I will plus plus I will become I will become one and J will again iterate four times. Right. And all the values will be stored inside a 2d array. So always remember whenever you are working on 2d array for reading and printing. Right. If you want to read the values or if you want to print the values of 2d array you will always use nested loop. Okay. Moving forward now if you have collected values in 2d array and if you want to print them on UI and in form of a 2d array like matrix you have rows and columns and you want to print them in a matrix format. So this will be the printing of 2d array. So first loop three times second loop four times then inner loop will print the values and after completion of this inner loop execution this printf will change the line. And here actually in place of backslash and I need to use backslash t it was a mistake which I corrected. So printf and backslash t is for tab. So two digits will be having some space. Okay. If you didn't understand now I'm jumping to the online compiler and I'm going to implement this reading and printing process for you. Okay. So right now I'm just going to show you how we can read values from user and how we can display them back on UI. So here I'm showing a message enter or three by four matrix. Then here what I need to do I just need to start a loop. So this is actually the process. Right. So first loop is a trading three times second loop is a trading four times so it is J zero and J less than four. Right. So first is a trading three times for second loop is a trading four times and inside that inner loop you can see we have scanf and that is responsible for reading the values. So this this is a set example. Whenever you want to receive values of 2D array you will be implementing the same code snippet. Right. Now whatever values we are receiving from user so total 12 values we will be reading. Now my requirement is I just need to show those values back on the UI but in matrix format in rows and columns format. Right. So I just need to show you how we can do that. So first of all I'm going to show a printf. So I'm just saying matrix is then for I equals to zero I less than three then for for Z equals to zero J less than four J plus plus. And here I'm writing printf backslash t Poisson D and a of IJ. Right. So the printf is part of this inner loop. So this inner loop will be iterating four times. This inner loop will be iterating four times. So basically four values having some gaps in between them will be displayed because here we are using backslash t which is for tab. Now once this inner loop completes its execution. So I just need to shift the complete row like if first row is printed I just need to move the cursor to the next row. So once this inner loop completes its execution. So here I'm going to write printf backslash and that's it. So with the help of this backslash and what will happen control will shift to the new line and next row will gets printed. Okay. So whenever you want to read a 2D array so you need to write this code snippet. Whenever you want to display the values you will be writing this code snippet from line number 13 to line number 19. Right. So now I'm going to execute this code so I'm clicking on run. So here I need to enter three by four metrics. So I'm entering one two three four five six seven eight nine ten eleven and twelve. So I entered total twelve values and you will see output will be displayed in matrix format. Still there is some mistakes in output like matrix is and then one two three four the this is printing from the beginning. Right. So what I'm going to do here also I'm going to use backslash and so in this printf in the beginning and in the ending I'm using backslash and so that whenever the new values will get start printing so it will be starting from new line. So I'm executing this code again so that I can show you the updated output. So here you can see the result matrix is and then one two three four five six seven eight nine ten eleven twelve. And the gap which you are seeing between one two two three three two four it is because of backslash T. So if you are using backslash T so a tab space you will be having and because of that tab space like in two digits you can have a long space. So I hope with this demo demonstration you are able to understand how to implement 2D array like reading and printing process. Okay. So moving forward now after explanation of this concept I have a few examples that I'm going to show you. So I will be implementing the solution of each and every problem. So first problem says calculate some of all elements of a 2D array or matrix. So if you have a 2D array and you have received all the values. So what we need to do we need to add all the values and we need to print some of those values together. Right. So what I'm going to do. So this this part will remain same here. I'm just adding a variable sum equals to zero and then instead of this print f I'm going to write sum equals to sum plus a of ij. So removing this as well. So what will happen with the help of this loop we are basically calculating some of each and every value that is available in the 2D array. Right. And once this process is completed then I'm going to show you the result. Okay. So once this loop is completed so it is for calculating sum of 2D array. Right. And here what I'm doing I'm printing some of array. So this print f is not part of the 2D array. So once 2D array completes its execution cycle both inner and outer loop then whatever result will be there that result will be available into the 2D array. The sum variable and with the help of this print f that sum will be displayed on output screen one time. Right. So array size is 3 by 4. So total 12 elements I'm going to enter and accordingly you will see the results. So I'm just clicking on run. And here I'm going to enter the values. So I entered all the digits starting from 1 to 12. And if you add all those digits. So sum will be 78. Right. If you have a calculator in front of you or in mobile phone. So you can just add all the digits from 1 to 12. You will see some is 78. Right. So this way you can do some calculations in 2D array. So I hope you are able to understand the concept how actually it is working and let me know in the chat if you are facing any difficulty. I would be happy to help and I will try to explain all your doubts that you are facing. So with this I'm moving on to the next example. Next example is like modification of the previous one. So in previous example what we did we added all the elements but example number two is saying calculate some of individual rows of a 2D array. Right. So if you have three rows so there should be three some. So we have three by four matrix. Okay. Some is basically zero. Now here what we are doing we are basically reading all the values. Right. We are reading all the values and here we are going to calculate the sum. So what will happen if I focus on this loop in a loop. So once this inner loop gets its execution completed whenever this inner loop executes four times. So first row elements will be added into some. So instead of writing this print f here I'm going to shift it here. So what will happen once this loop is completed like zero zero zero one zero two zero three all these four indexes are added then the sum will be available into the sum variable. And once this loop is completed then some will be printed and for next rotation I'm setting some as zero. So I will be incremented I will become one and then again this loop will iterate and accordingly result will be calculated and the result will be displayed as output. Right. So this way some of individual row will be displayed. So now I'm going to show this to you. So I'm clicking on run. So entering the values. So you can see total three sums are there 1026 and 42. And if you want I can okay I can run this again so that I can enter some simple values. So for each element I am entering two. Now here you can see some is eight. Why eight because in each row there will be four two in each row there will be four two and that's why it is happening. Okay. So I hope everybody understood this concept. Now one more thing next example says some of individual columns. So in this case what we need to do we need to change it a little bit. So here I meant entering four and here I'm entering three. So what will happen I loop will iterate four times and J loop will iterate three times. And here I'm writing J I because index will be zero zero one zero and two zero. If I show you the column wise indexes if I show you the column wise indexes so this is first column where one five nine is available. This is first column. So column index will be zero zero one zero and two zero. Right column index will be zero zero one zero two zero. So what will happen here what will happen here so right now what is happening I zero and J will start with zero. So first index will be zero zero then J will increment J will become one. So next index will be one zero next index will be one zero because here I have written a of J I that you need to focus. And one more time J will be incremented so J will become two. So next index will be two zero then one more time J will increment and condition is J less than three. So J will become three three less than three. So condition will be false. So you need to do two changes like this conditional termination condition will be modified and here in place of a I J you need to write a J of I right. So this way column sum will be displayed. So I hope this makes sense and you are able to understand now I'm going to run it for you. So again I'm entering two two two if I'm entering three sorry if I'm entering two in three by four matrix. So what will happen in each column we'll be having three twos. So that's why some is six but some is printing four times because we have four columns if you see the dimension it is three by four. So we have four column and in each column we have three rows. So I entered two. So that's why some is six because in each column we have three rows and each row element is two. So two plus two plus two three time it is six and for four rows. So this way if you want to calculate some of individual columns. So this way you need to modify the code and next we have one more example and this is the last example for today. So it is calculate some of diagonal of a 2D array. So diagonal means wherever row and column is same diagonal means wherever row and column is same. So this print will be shifted to next line. This I'm removing and here I'm going to add one more thing and in this case your metric should be like having same row and same column size. Otherwise diagonal won't be possible. So row column size will seem and here you can write if I double equals to J and inside that you can write this. Okay. So if row and column is same then you will be able to understand. So if row and column both are same so value will be added and here we can write ij or ji doesn't matter because whenever i and j both are same then only this sum will happen. Okay. So I just need to modify this four by four. But first I need to enter the value. So I'm entering two for each location. You can see some is eight because in four by four matrix if we consider a diagonal so diagonal will be having four values and I entered two at every position. So that's why diagonal sum is eight. Right. So here I'm going to modify it to four by four but like all the all the execution happened for 16 elements. I just need to modify this so that next whenever I execute it next so it will show four into four. Rest of the code will remain same. So this way like if you want to implement diagonal sum you can and I have one more example for this which is very interesting with a single loop also you can calculate diagonal sum. So what you can do you can write i equals to zero i less than four i plus plus. So we already know whenever indexes are same that position is diagonal if indexes are same so that position is diagonal. So here what I can write a i i so we know like if indexes are same it means it is diagonal position. So what will happen initially it will be a zero zero then I plus plus I will become one so it will become a one one then a two to a three three. So this way with the help of single loop you will be able to calculate sum of diagonal which is very interesting. And this also lets you know like in square brackets you can put any variable in any order and you can repeat that variable as well. Okay. So I think with these example you are able to understand the concept of 2D array and try to practice whatever examples I implemented at your own. So if you get stuck somewhere just watch this recording so that like the stream is recorded and once I complete the session it will be available on YouTube. So anytime if you face any challenge you can just watch the recording and you can implement the solution. So with this note I just end this session here only tomorrow also I will be having session on 2D array and in that session I will be implementing or solving some more problems. And please share a review or feedback about the boot camp so that like I know how you are feeling and whatever suggestions like you can send to me so that I can improve. And right now like four different boot camps are going on. So I hope if you are related to different different technology if you if you are learning those so you are watching those sessions live or recordings. So keep on doing that and soon I will be sharing more information about online offline limited seats batches and initially I will be offering 100% scholarship. So stay tuned and follow the boot camp. Thank you very much for joining this session live and if you are watching the recording so thanks to you as well. See you tomorrow with same time with new topic new not new like with some examples of this topic which we discussed today. Thank you everyone. Bye.