 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain how to calculate address of 2D array. So basically, if you want to calculate address of 2D array, so we have two methods. First is row measure and second one is, right? So let's say we have a 2D array of redemption 3 by 4. So these are 3 rows and these are 4 columns, right? So if we calculate addresses based on algorithm. So in case of algorithm, we consider like indexes are 1, 2, 3 for rows and 1, 2, 3 for columns, right? But if you want to calculate address for any programming library, so then these indexes start from view, so accordingly you can calculate, right? So right now I am considering that this is first row, second row, this is third row. First row and second column, third column and fourth column, right? So now let's calculate address with the help of row measure program, right? So I am going to write a formula. So if we want to calculate address for this dimension, i representing go index and j representing column index and a is a matrix. So let's say this is a, which is 2D array of 3 by 4 dimension. Then dA stands for base address, w stands for word size or you can say size of a particular block. Then n into, so n is the number of columns here, then i minus 1, right? And then j minus 1. So this is row index minus 1, this is column index minus 1. And with the help of this formula, we can calculate the address. So let's say base address of this 2D array is 1 triple 0. So beginning address of this matrix is 1 triple 0. And we are going to calculate address for this location. Second row, third column. So it means we need to calculate address for this location, right? And word size is 2 bytes. It means each location is having 2 bytes of memory, right? So if this beginning location is 1 triple 0. So next location address will be 1, 0, 0, 2. So 1, 0, 0, 0, 0 and 1, 0, 0, 1 will be reserved for this. And after 2 addresses, next location address will be 1 triple 0, 2 and so on. So we need to calculate address for a particular location through this formula because if your 2D array has lots of rules in column. So you cannot calculate address by going to each location, right? So you need a formula where you can place the value line automatically particular address will be available. So now if we want to identify address for this location with the help of a long major order. So this is base address. W is 2. And n is representing number of columns. So here you can see number of columns are 2. So I am putting 4 here. Multiply i is 2. So 2 minus 1 plus 3 minus 1. 3 is column. So it is here, right? So now 1,000 is the base address and 4 into 1 plus 2, right? So this will be calculated. So now 4 into 1 is 4 and plus 2. So next we can do this. So it will be 1,000 plus 2 into, sorry, 6. Now 6 into 2 is 12 and it will be added to 1,000. So it is 1,000 plus 12. So now we are going to calculate address family. So it is 1 triple 0. Next possible address will be 1,002. Then 1,004, 1,006, 1,008, 1,010 and 1,010. So here you can see we are having correct address with the help of this formula. So this formula is basically related to home major order. So it means if you are storing your to the area in a home major order, it means first this row will be stored in then the address row, then address row and so on, right? Another method of storing to the area is column major. So in case of column major order, first this column is stored in then this column, then this column and then this column. So now we are going to modify this formula so that we can calculate address with the help of column major order. So to apply this, I need to change this value. So m will be replaced with m, i will be replaced with j and j will be replaced with i. So this is for column major order, right? So here m is now representing number of rules, right? So let's say if you want to calculate this address, so again we want to calculate address for this location. So now we need to remember that memory is stored away from column. So first this column will be stored in then this column, then this column and then this column. So now we need to put the values according to this, right? So I am erasing these things. As for this formula now, we need to put the values. So here you can see base address is 1000. So I am putting 1000 as base address. Now w is 2, so plus 2. Then n is representing number of rules. So number of rules are 3. Multiply it with j minus 1, j is 3. So 3 minus 1 plus i is 2, 2 minus 1. So now this is the formula. Then 1000 plus 2. So here 3 into 2 plus 1. So if we calculate this, so it will be 6 plus 1, 7. 7 into 14. So final outcome will be this. 1010, right? So now let's calculate this. So it is 1002. It will be 1002. Then 1004, 1006, 1008, 1010, 1012, and 1014. So you can see if we store this new theory or a major order. So this will be the address for this. So this way I hope you understood. Both the formulas related to room is the order. So remember one thing. Whenever we want to calculate the address for, we can store two theory base. One is room is the order. So accordingly you need to calculate the address of a particular location. So I hope you understood how we can calculate address of a particular location to room is the order as well as to follow the order with the formula. And if you want to watch more videos related to data structures and algorithm, you can go to the description of this video where you will find links of the list. So you can follow them. Thank you for watching this video.