# 6. Arrays Representations

https://www.youtube.com/watch?v=s1hZP2HxDLI

[00:00] In this video we'll learn about an array, what is an array, and how to declare an array and initialize an array, then what are the methods of accessing elements of an array.
[00:13] So let us start with introduction to an array.
[00:16] Before talking about arrays, let me explain about simple variables.
[00:20] Uh variables are supported by every programming language, and the variables will have some data type.
[00:24] For example, if I declare a variable of type integer and whose name is X, and in this I want to store the value 10, then you will know very well that at run time variable X will get some memory.
[00:37] If we assume that integer takes two bytes, then two bytes of memory will be allocated for X.
[00:44] Let us say first byte address is 100, then the next byte address is 101, and together in these two bytes the value 10 will be stored.
[00:53] This we are already familiar with.
[00:56] So this type of variable is a single valued variable which can store just one single
[01:00] value.
[01:03] And this single valued variable we can also call it as scalar variable.
[01:06] So this is a scalar variable.
[01:09] Scalar means which is having just magnitude.
[01:10] So this is magnitude, the value is a 10.
[01:12] That's all.
[01:15] Then what is an array?
[01:17] We can store multiple values, that is list of values or say set of values.
[01:23] So array is a collection of elements, and all the elements are of same type.
[01:28] So array is a collection of a similar data elements grouped under one name.
[01:33] Suppose instead of just one value, if I have to store list of values, then I can declare an array.
[01:41] Let us say integer type array I want and the name is A, and the size is five.
[01:47] So this will give me a array of size five where I can store five integers.
[01:57] Five integers I can store and the indices 0 1 2 3 4.
[02:03] Now here this is a scalar variable where I can store just one value.
[02:07] This is a vector variable which is also having a dimension.
[02:09] It's a single dimension.
[02:12] So I can store total five values in this one.
[02:15] So we can call this variable as a vector variable.
[02:20] So arrays are vector variable.
[02:24] This is a single integer under name X.
[02:25] These are five integers under name A.
[02:31] The memory allocated is 100 and 101.
[02:33] For five integers, total 10 bytes of memory will be allocated if the integer is taking two bytes.
[02:41] For example, the first byte address is 200, then the next byte is 201, so this is 202 and 3 204 and 5 206 and 7 208 and 9.
[02:56] So the memory allocated will be contiguously.
[02:59] So all the locations will be side by side.
[03:01] They are contiguous.
[03:04] For five integers, total 10 bytes of memory is allocated together as a single block.
[03:10] Now all those five integers are having same name, that is A.
[03:14] Then how to differentiate those five integers?
[03:15] We can differentiate them with their indices.
[03:17] Now A of 0 means this is first integer, second integer, third and fourth and fifth.
[03:22] So I can access them or differentiate them with the help of index.
[03:27] Suppose I want to store a value 15 here, then I can say A of 2, that is index 2, assign value 15.
[03:39] So this will store the value 15 here.
[03:41] So using the name and the index, we can access any of those integers, any of those elements.
[03:47] So that's how array is a collection of elements or a list of elements which are of same type or are of type integer and this is supported by every programming language as a basic feature of a language.
[04:00] This is there in C language as well as C++.
[04:05] Now, let us see what are the different methods of declaration and what are the various ways of initializing a array.
[04:11] First method is if I am declaring an array with the name A of size five, then this will allocate the memory space for five integers.
[04:24] Indices are 0 to 4.
[04:28] And this is just a declaration.
[04:30] So, the values inside this one will be garbage values because nothing is initialized, so the values will be garbage.
[04:36] Means unknown values, random values will be there that are not initialized by us, so they are not useful for us.
[04:43] So, we say garbage values.
[04:46] Now, next method, if I want to initialize the values, then I should say this is a part of declaration and here I can initialize the values like 2 4 6 8 and 10.
[04:59] Suppose I am initializing also, then a array will be created during run time and all the values will be initialized
[05:06] In this one.
[05:09] So, this looks like 2, 4, 6, 8 and 10.
[05:13] This is the second method.
[05:14] So, this is the declaration plus initialization.
[05:17] So, if you if you want to have some values initially in an array, then you can mention all the values.
[05:22] Now, third method, you can mention the size, but if you don't want to initialize all the values, just few values you want to initialize, then an array of size five will be created with the indices 0 to 4.
[05:37] As only two values are mentioned, so only two four will be initialized and the remaining elements will be initialized with the zero.
[05:47] Because once the initialization process starts, it will try to initialize all the elements.
[05:51] So, remaining values are not given, so it will initialize them with zero.
[05:56] So, even I can say like this, integer A of size of five, and just zero.
[06:03] Then, this will create an array.
[06:10] and initialize first one with zero as it will continue initializing rest of them also.
[06:15] So, it will initialize all of them with zeros.
[06:20] And one more method is there for creating an array.
[06:24] Just you can mention the values, 2 4 6 8 and 10.
[06:29] Then, depending on the number of elements you have mentioned in the initialization list, the array size will be same as that one.
[06:37] So, an array of this five size will be created, and it will be initialized with all these elements.
[06:45] So, if I add one more element to this one, like let's say 12, then total size is 1 2 3 4 5 6.
[06:49] So, array of size of six will be created, and this will be initialized with these elements.
[07:00] So, these are the various ways of declaration and initialization of an array.
[07:07] This is there in both C as well as C++.
[07:12] If you write like this inside the program, then during run time, this is how it will work inside the main memory.
[07:19] Now, next we will see how we can access all the elements of an array.
[07:21] Here I have an array of size five, which is already initialized with these elements.
[07:26] So, during run time, the array will be created like this, and the elements will be stored in these locations, and their addresses also I have mentioned that addresses will be contiguous.
[07:36] Now, how to access those elements?
[07:39] So, if I want to print any value, so I can say printf.
[07:46] Suppose I want to print a value that is at index zero.
[07:47] So, then I can say %d A of zero.
[07:54] This will print the value at index zero.
[07:57] Suppose I want to print the value one, then I should make it as one.
[08:02] So, it means I can access all those values by just changing the index.
[08:04] A of one, A of two, A of three, anything.
[08:09] Now, if I have to traverse through an array, so traversing means visiting all
[08:14] The elements once.
[08:16] So, if I have to traverse all the elements, then I can take the help of for loop, which will help me change these indices from zero to four.
[08:22] So, this can be changing from zero to four.
[08:27] So, first time zero, this will be the element, the next one, then two, then three, then four.
[08:31] So, it will allow me to access all the elements by traversing through those elements.
[08:36] So, for loop is used for accessing the elements in an array.
[08:40] So, for I assign zero, I is less than five and I plus plus, and here I can mention I.
[08:50] Now, I can access all those elements.
[08:54] So, for loop is used for traversing through all the elements of an array.
[09:00] And the element can be accessed with the name of an array and the index.
[09:05] Now, next let us see what are the ways of accessing a particular element in an array.
[09:11] If I want to print this element, then I can say printf
[09:16] %d
[09:19] A of two.
[09:21] This is one method.
[09:23] Then the same thing, printf, I can even say two of A.
[09:28] This is another method.
[09:30] So, even you can use index outside and the name of an array inside subscript.
[09:36] Then, using the pointer arithmetic also we can access that elements that same element if I want to access, then print f so and so, I can say star a plus two.
[09:52] This plus two must be inside the bracket.
[09:55] Star must be for entire thing that is a plus two.
[09:57] So, I can mention any index here and I can write star here and I can access that particular location.
[10:05] So, array elements can be accessed by using the subscript, that is index, as well as they can be accessed with the help of pointer arithmetic.
[10:16] So, these are all the basic things about
[10:17] An array.
[10:19] So, we'll learn more about arrays in the next video.
[10:24] In this video, we will look at the demonstration of declaration and initialization of arrays.
[10:30] We have already seen in previous video, there are various methods of declaration and initialization.
[10:36] So, we'll try all of them here.
[10:38] First method, declare an array of some size.
[10:40] I have declared an array of size of five and I'm not initializing it.
[10:45] Next, I will declare an array of some size five and also I will initialize values.
[10:52] I have initialized five values.
[10:55] Third method, I will declare an array of size 10, but I will initialize only few values.
[11:04] I have initialized only three remaining seven values will be made as zeros.
[11:11] The next, I will declare an array of size five and I will assign only zero.
[11:19] Then, as the first element is zero, the rest of the elements will also be filled with zeros.
[11:24] Then, one more method, I will not mention any size.
[11:28] I will write down the elements.
[11:35] I have given six elements.
[11:41] Now here I write return zero.
[11:46] I'll put a bookmark here.
[11:52] See, I'm getting here warnings that all these variables I'm not using them anywhere in the program.
[11:58] So if the variables are not used then compiler is giving a warning.
[12:03] So the warnings are instant and the errors are also you have observed they are instant.
[12:06] So that is the benefit of using this editor, this Xcode IDE.
[12:12] I will run the program and show you how these arrays are created in the memory.
[12:16] I'll put a breakpoint here.
[12:20] Then I will run the program.
[12:27] Here you can see in the debug area that is in the watch.
[12:31] All the variables are shown here.
[12:33] A is of size five and all these values are zero and the last value is some garbage.
[12:38] So actually these zeros are also garbage.
[12:40] It can be any number.
[12:43] Now next array if you look at, it is filled with values one to five.
[12:47] So here we have initialized array with one to five.
[12:50] I'll close them.
[12:54] Now next array C, I have taken an array of size 10 but I have initialized only three numbers.
[12:58] Rest of the numbers will be zeros.
[13:01] Automatically they are initialized with zero, they are not garbage.
[13:06] And the D, all are initialized with zero, there is no garbage value here.
[13:11] These are the values that we have initialized them.
[13:14] And E you can see I have not mentioned the size but I have given six numbers here.
[13:20] So an array of size six is created. It
[13:22] is showing an a array of size here
[13:25] inside this bracket and there are six
[13:27] elements zero through five.
[13:30] I'll stop this.
[13:33] I will remove these declarations and I
[13:34] will show you how the addresses of array
[13:37] locations are contiguous.
[13:40] I will print the addresses of array A.
[13:43] So, for that I need a for loop.
[13:45] So, using for loop
[13:54] Here I will print the addresses of all
[13:56] the locations.
[13:58] So, for printing
[14:01] addresses I should use percentile U
[14:06] and space
[14:09] new line
[14:11] then I will print the address of
[14:15] A of I.
[14:17] Ampersand will print the address.
[14:21] So, for printing addresses I should use U.
[14:21] I'll remove the breakpoint.
[14:24] Now, let us run the program.
[14:28] See, these are the addresses of all the locations.
[14:30] There are total five locations.
[14:31] So, first address that is first integer address is 56 and here the integer is taking four bytes.
[14:33] So, plus four is 60.
[14:36] The next plus four is 64.
[14:38] The next plus four is 68.
[14:40] The next plus four is 72.
[14:42] So, all these addresses are contiguous.
[14:46] All the location that is A0 to A four, the locations are side by side and in this compiler integer is taking four bytes.
[14:48] But I when I am discussing on whiteboard, I am assuming that integer is taking two bytes so that is easy for me for explaining the things.
[14:52] So, that's all in this video.
[14:55] We have seen how we can declare and initialize arrays.
[14:57] Now, let us look at static versus dynamic array.
[15:00] So, static array means the size of an array is static.
[15:26] And the dynamic array, the size of an array is dynamic.
[15:30] See, once an array is created, its size cannot be modified.
[15:34] So, in our programs, when we declare an array, for example, I have a function void main,
[15:40] and inside this function, if I have declared an array integer of size A five,
[15:47] then array of size of five will be created.
[15:50] So, the question is where it is created.
[15:53] As it is mentioned inside the main function as a variable, just a vector variable, then the memory for this array will be created inside stack.
[16:03] So, an array of size five will be created inside the main memory as a part of activation record of main function.
[16:14] So, this array will be created inside stack.
[16:17] So, why I'm calling it a static?
[16:19] Because the size of this array was decided at compile time.
[16:24] Though the memory will be allocated during run time only.
[16:28] Memory cannot be allocated at compile time.
[16:31] So, memory will be allocated during run time, and the size of that memory was decided already at the compilation time.
[16:39] Definitely, the size of an array has to be decided at compile time in C language.
[16:43] In C language, when you are mentioning the size of an array, it must be a constant value.
[16:48] It cannot be a variable.
[16:51] It must be a constant.
[16:53] So, you must write some constant value there.
[16:56] But, in C++ we can create an array of any size at run time.
[17:00] So, at run time, a size of an array can be decided, and it will be created inside the stack only.
[17:09] Whereas, in C language, the size has to be decided at compile time only for the arrays inside the stack.
[17:14] Let us see, in C++ suppose I have one variable n and c in n.
[17:22] So, it means I'm reading some input from the keyboard, the size of
[17:28] array as n.
[17:32] Then, I can declare an array of some size n.
[17:37] So, whatever the input comes from the keyboard, I can create an array of that particular size.
[17:41] So, size of an array is decided at run time.
[17:44] You can enter any value in n at run time.
[17:47] So, an array of that size will be created inside the stack.
[17:52] So, size of an array can be decided at run time in C++.
[17:54] But, in C language, it has to be mentioned at compile time only.
[17:57] This is the difference.
[17:59] And both these arrays are created inside the stack only.
[18:03] So, there's no space to show you B also.
[18:05] So, assume that B is also created inside the stack.
[18:09] So, this part is in C language and in C++ this is also allowed.
[18:14] Now, next, we will learn how to create an array inside heap.
[18:21] So, whose size and the type everything is decided at run time.
[18:24] Now, one thing you remember, for accessing anything from heap, we must
[18:30] have a pointer.
[18:32] So, first of all, I should declare a pointer.
[18:35] So, I will declare the name of a pointer as a P.
[18:37] This is a pointer.
[18:40] Now, where this P will get the memory?
[18:44] As A is a variable of type array, P is also a variable.
[18:47] So, this will also get the memory inside stack.
[18:51] Now, remember, in any language, when you declare a variable, the memory for that variable will go inside the stack only.
[18:59] Then, when it will be allocated in heap?
[19:01] Let us see.
[19:04] Now, that pointer I will use it for creating memory in heap.
[19:06] So, then I should say P assign new int of size five.
[19:12] Then, the memory for five integers will be created inside heap.
[19:17] And this pointer P will be pointing on to that address.
[19:20] Suppose the beginning address of this contiguous memory is a 500, assume that, then 500 will be stored in this pointer.
[19:30] So now the program can access that heap by coming on to the pointer and taking the address and it can go there and access it.
[19:36] So heap memory cannot be accessed directly, it has to be accessed indirectly.
[19:42] Now one important thing I have to show you here, when you get the memory from heap, whenever you say new, then only you get the memory from heap.
[19:48] Otherwise, all the variables, whatever the variables you declare, they will get the memory inside the stack only.
[19:55] Now new is a operator that is in C++, then how to create the same thing inside C?
[20:02] So for that, I have to say function malloc or malloc function, and I should mention the size.
[20:08] So I want five integers, so five into Now size of an integer, it may be two bytes or four bytes, depends on the system or the compiler.
[20:16] So I should mention here size of int.
[20:20] So this is the common practice that is done in C language.
[20:24] Instead of mentioning two bytes, we mention size of so that, depending on the system or
[20:30] compiler, it takes that size.
[20:33] Then malloc function will allocate just
[20:35] a raw memory, just the block of memory.
[20:39] Now that memory we want to use it as
[20:41] integer, so we have to type cast it as
[20:43] integer pointer.
[20:45] So this is in C language and this is in
[20:48] C++.
[20:51] New is the operator used in C++ and
[20:55] malloc function is used in C language.
[20:58] So once again, when you declare a normal
[21:00] array, then it will be created inside
[21:02] stack. When you want an array in heap,
[21:04] then And must have a pointer and then
[21:07] you should allocate the memory either
[21:08] using new operator or using malloc
[21:11] function. It will be created in heap.
[21:13] And the heap memory can be accessed
[21:15] indirectly with the help of pointer.
[21:18] Now, one more important thing.
[21:20] When we have allocated the memory in
[21:23] heap, and after some time during the
[21:26] execution of program at any stage, if
[21:29] that memory is not required, then you
[21:31] must delete the memory also. If you
[21:34] don't delete the memory, unused memory,
[21:37] then it causes memory leak problem.
[21:40] If the unused memory is not released,
[21:43] then it causes memory leak problem.
[21:47] So, how to release the memory? In C++,
[21:50] we have to say delete
[21:54] P.
[21:55] But P is used for an array, so it must
[21:58] be mentioned here as a subscript. And in
[22:01] C language, say free P.
[22:05] So, this is the method of deallocation
[22:07] in C++, and this is the method of
[22:10] deallocation in
[22:12] C language.
[22:14] So, I have shown you the syntax for both
[22:16] the languages. So, the main concern here
[22:18] is how to get the array inside heap.
[22:22] Now, next we will see how to access that
[22:24] array from heap. This is a normal array
[22:27] inside the stack, and this is a array
[22:29] inside heap. Now, how we access this
[22:31] normal array inside the stack? So, for
[22:34] accessing any location, I should say A
[22:36] of zero assign five. So, the value five
[22:38] will be stored here. Now, same thing I
[22:41] can say P of zero assign five. Just like
[22:45] a normal array, I can access this also.
[22:48] So, the value five will be stored here.
[22:51] So, it is as simple as accessing an
[22:53] array inside the stack. You can access
[22:56] an array from heap by using a pointer.
[22:59] So, pointer acts as a name of a array.
[23:03] So, already we have learned that this
[23:05] entire array can be accessed with the
[23:07] help of a loop, for loop, then that
[23:09] array can also be accessed with the help
[23:11] of a loop. As we saw that this can be
[23:13] accessed using pointer arithmetic, so
[23:15] even that can be accessed using pointer
[23:17] arithmetic. So, we don't have to relearn
[23:19] that thing, we already know it.
[23:21] Now, the next important thing here is
[23:24] that
[23:25] wherever you create an array, whether
[23:27] you create it inside stack or you create
[23:29] it inside heap, once the array of some
[23:32] size is created, it cannot be resized.
[23:35] Its size cannot be changed.
[23:39] If at all you want to increase the size
[23:41] of an array, it is possible in another
[23:45] way, but it is possible only in heap.
[23:48] Stack array cannot be resized at all.
[23:51] But a heap array The same array cannot
[23:54] be resized, but we have some alternative
[23:56] for that. So, let us learn how we can
[24:00] increase or decrease the size of an
[24:02] array.
[24:04] In this video, we will see what are
[24:05] methods of
[24:07] creating array that is in a stack,
[24:09] static array, or inside heap.
[24:13] We have already seen this. We can create
[24:15] an array by just mentioning the size.
[24:17] So, this array will be created inside
[24:18] stack.
[24:21] Next, for creating an array in heap, we
[24:23] must have a pointer.
[24:25] To that pointer, we can create an array
[24:27] in heap and assign the address of that
[24:29] array to that pointer.
[24:31] So, here I will write down the code P
[24:32] assign for
[24:36] allocating memory in heap, the function
[24:38] is malloc function.
[24:41] And suppose I want to create five
[24:42] integers, so we know that
[24:47] So, 5 into
[24:49] size of integer. We can use an operator
[24:51] size of integer because here in this
[24:53] compiler it is taking 4 bytes. So,
[24:55] whatever the number of bytes a compiler
[24:57] is taking, it will take that many bytes.
[25:00] This malloc function will return void
[25:03] type of pointer, so it must be type
[25:04] casted before use. So,
[25:07] I will type cast it as integer type.
[25:10] This will create an array in heap.
[25:13] Here, I will change the array size to
[25:14] five, and I will initialize with some
[25:16] numbers.
[25:19] 2 4 6 8 and 10.
[25:23] Now, this array what I have created just
[25:25] now using heap,
[25:26] I will initialize this array,
[25:29] that is P of 0 with
[25:31] 3.
[25:34] I will assign odd numbers.
[25:36] Now, here I will take one integer
[25:38] variable for displaying the values, so I
[25:40] will display the values of array A as
[25:43] well as array P using for loop. So,
[25:45] first for loop, I will print the array
[25:48] A.
[26:07] Yeah, with the for loop, I am printing
[26:09] all the elements of array A from 0 to 4.
[26:14] Same for loop, I will copy here to print
[26:16] the dynamic array that is pointed by P.
[26:19] So, I will write P of I here.
[26:24] Between these two, I just want a new
[26:26] line, so print f {slash} n, so their
[26:30] results are displayed one after another.
[26:33] Next thing is I'm getting a warning
[26:35] here, that is unable to find the
[26:36] definition of malloc function, so for
[26:39] that, I should include a header file,
[26:41] that is stdlib.h.
[26:48] This function is available in stdlib.h,
[26:50] so this is gone.
[26:56] I will run the program and show you.
[26:58] These are the two for loops used for
[27:00] printing the values of array A and array
[27:02] B to confirm really the arrays are
[27:04] created and they are holding those
[27:06] values.
[27:11] Yeah, you can see that 2 4 6 8 are the
[27:13] values that are kept in array A. And 3 5
[27:17] 7 9 11, these are the values that I have
[27:19] assigned them to
[27:20] array B.
[27:23] So, in our programs, wherever we want to
[27:26] allocate array in heap, then this is the
[27:29] method using malloc function. You can
[27:31] allocate memory in heap.
[27:33] I will put a breakpoint here and debug
[27:35] the app program and show you. I see here
[27:37] is a breakpoint on the return statement,
[27:39] then I will run the program.
[27:41] Let us see how these arrays looks like.
[27:44] See, this array A is having values 2 4 7
[27:47] 8.
[27:49] Array A is having values 2 4 6 8 10 and
[27:52] it is of size five. So, here in the
[27:53] brackets you can size five.
[27:56] Then P is just a pointer, it's not an
[27:57] array.
[27:58] And this pointer is having so and so
[28:00] address. This is the address. Then the P
[28:03] is pointing on the first element as I
[28:05] have shown you on the board white board
[28:07] that pointer will be pointing on the
[28:08] first element, but using that pointer
[28:10] you can access all other elements. So,
[28:12] this is P of zero, that is
[28:14] three.
[28:15] And plus one if you say you can access
[28:17] this one or P of two, P of three, P of
[28:19] four, we can access all these elements
[28:20] with the help of this pointer.
[28:23] So, it's showing only one value when it
[28:25] is dereferenced. So, star P gives value
[28:27] three.
[28:30] The address of of the array in heap is
[28:33] given here in the hexadecimal form. This
[28:35] is the hexadecimal number.
[28:39] So, that's all. This is the
[28:40] demonstration of creating array in stack
[28:42] as well as in heap.
[28:45] Here already I have an array of
[28:47] five that is created in heap and pointer
[28:50] is pointing to that array. So, this is a
[28:52] pointer P and this is in stack, this is
[28:55] in heap. We know very well now.
[28:57] So, this is pointing there. Suppose
[28:59] already it is having some elements.
[29:01] That size of five is not sufficient for
[29:03] me. I want a larger size array. Then one
[29:06] method for increasing the size is
[29:10] take one more pointer
[29:13] that is called it as Q
[29:15] and create a new array of
[29:18] required larger size. Let us say I want
[29:21] an array of size 10.
[29:23] Then pointer Q will be pointing upon an
[29:26] array with a larger size.
[29:30] 0 1 2 3 so on.
[29:33] Now, this Q is pointing on a larger size
[29:35] array.
[29:37] So, I cannot increase the size of the
[29:39] same array. Already I said that. So,
[29:42] alternative create a bigger size array.
[29:45] Now, transfer all those elements from P
[29:47] to Q. So, we know the size of P very
[29:50] well that is size is five. So, for
[29:53] I assign zero, I is less than five, I
[29:57] plus plus.
[29:59] Then in Q of I copy the elements of P of
[30:04] I.
[30:06] So, I'm copying them using for loop. So,
[30:08] all these values will be copied here. 5
[30:10] 8 9 6 4. So, the values are copied
[30:13] there.
[30:15] And even there is one function in C
[30:16] language that is called as mem copy that
[30:18] is memory copy. You can use that and
[30:21] mention these two arrays. So, those
[30:23] contents will be copied here.
[30:25] So, using for loop I'm showing them.
[30:28] Next, we want to increase the size of
[30:31] this P. Actually, P is a pointer. P is
[30:34] pointing on so and so array now. Now,
[30:36] you have to make this P point on this
[30:38] one.
[30:39] But before making P point on that one,
[30:41] let us delete that array. So, here say
[30:45] delete P.
[30:48] As I said, if you don't delete unused
[30:51] memory, then it will cause memory leak.
[30:54] Means, the shortage of memory, reduction
[30:56] in the size of the memory. So, it is
[30:58] unused now. We don't need now. So,
[31:01] delete that one. So, this memory is
[31:02] deleted.
[31:04] Then make a P point on Q.
[31:07] So, wherever Q is pointing, P will also
[31:11] point to there. So, now new location
[31:13] where P is pointing is this one.
[31:17] Then, now this array is pointed by both
[31:20] P as well as a Q. Now, let us remove Q
[31:23] from there.
[31:27] Q assign null.
[31:30] So, Q will no more point on that one.
[31:33] This is null. So, this is not pointing
[31:35] on this one. It's pointing on this P.
[31:38] Now, if I say P, this is the array now.
[31:42] That array is gone. It is deleted. So,
[31:44] it is removed from the memory.
[31:46] And P array size is now 10.
[31:49] Now, let us take the example like this.
[31:51] Like the P was pointing on this array.
[31:53] So, P is having car.
[31:56] Now, P is having a small car. Now, that
[31:59] car is not sufficient. P needs a bigger
[32:01] size car. So, P purchases a new car. So,
[32:05] Q helped in purchasing a new car. Now, P
[32:08] was having both the cars at that time.
[32:10] Then, all these things are transferred
[32:12] in a new car and that car is sold out.
[32:15] Now, P is owning this car.
[32:19] So, for shifting from one car to another
[32:21] car, Q has helped because P has to sell
[32:24] away that car also.
[32:26] Or else, you can take the example, this
[32:28] room is not sufficient for conducting a
[32:30] class. The students are growing in
[32:32] number. We need a bigger room. So, we
[32:34] cannot increase the size of the current
[32:36] room. So, we have to shift to a new
[32:39] bigger room. So, this is a new bigger
[32:41] room.
[32:42] And all the students from that class are
[32:43] moved to this class. And there's a lot
[32:46] of free space for more students to sit
[32:48] in the class.
[32:50] So, that's the idea followed for
[32:52] increasing the size of an array. So, if
[32:55] you are not doing it, if some API is
[32:58] doing it inside programming, then
[33:00] definitely it is following this method
[33:02] because the same size array cannot be
[33:04] grown.
[33:05] Now, I'll tell you the reason why the
[33:06] array size cannot be grown because the
[33:09] memory for the array should be
[33:11] contiguous.
[33:12] For example, the beginning address of
[33:14] this is 500 and 501, then they should be
[33:18] 2 3 4 5 6 7 8 9, then the next location
[33:23] must be 10 11. Now, we don't know
[33:26] whether that location is freely
[33:28] available or not or it is used by some
[33:31] other object inside the same program.
[33:33] So, there's no guarantee that the next
[33:35] consecutive locations are free or not.
[33:38] If the consecutive locations are not
[33:40] free, then the array cannot be
[33:41] increased. That is the reason we say
[33:43] that array size cannot be increased. So,
[33:46] rather than that, we will have another
[33:48] larger array and shift the elements.
[33:51] So, this is about static and dynamic
[33:54] array. In this video, we will see how we
[33:56] can increase the size of an array
[33:59] when it is created inside heap. So, for
[34:01] that, I will take one pointer
[34:04] P as well as I will keep ready one
[34:06] pointer Q.
[34:07] First of all, using P, I will create a
[34:10] array of size five.
[34:13] So, already we have seen how to create
[34:15] an array inside heap. Using P, I will
[34:18] create a array inside heap
[34:21] using malloc function. Already we have
[34:23] seen how to do that.
[34:26] malloc
[34:28] and of size five
[34:31] size of integer.
[34:34] So, this will create an array of size
[34:36] five.
[34:38] Now, in that P array directly I will
[34:39] fill up the values. I will insert odd
[34:42] numbers.
[34:44] P of zero assign three, then P of
[34:48] one assign five. Likewise, I will fill
[34:50] up all of them.
[34:53] Now, we'll display that array P. So, for
[34:54] that I will take one variable I
[34:57] for running a for loop.
[35:00] Here, I will print all the values of an
[35:01] array P just to confirm the values are
[35:04] stored in an array.
[35:06] I less than five, I plus plus.
[35:09] Now, print F
[35:13] percentile D, then next line.
[35:17] I'll print the values from P of I.
[35:20] So, this should print all the values
[35:21] from zero to
[35:23] four, that is three to 11.
[35:26] Let us run the program.
[35:30] Yes, I got the values from three to 11.
[35:35] Now, suppose this array size five is not
[35:38] sufficient and I want a larger array,
[35:40] then same P I cannot extend its size,
[35:43] then I should take another pointer, that
[35:45] is Q. With the help of another pointer I
[35:47] should create an array of larger size.
[35:50] So, here I will create an array of
[35:51] larger size. malloc
[35:54] I will create an array of size 10. So,
[35:56] size of
[35:59] integer.
[36:01] So, Q will allocate memory in heap for
[36:04] an array of size 10.
[36:07] Now, the values are already there inside
[36:09] P array. Those values I want them to be
[36:11] transferred to Q.
[36:13] So, using for loop I can do this. For I
[36:16] assign zero
[36:18] I is less than five I plus plus.
[36:23] Then in Q of I I should copy the values
[36:26] from P of I.
[36:29] This will copy all the elements to Q.
[36:31] Now, this for loop that is
[36:34] used for printing the elements from P, I
[36:37] will just change it to
[36:38] Q.
[36:39] So, let us see whether the elements are
[36:41] copied in Q or not. I will run the
[36:43] program.
[36:44] I should get all the elements that are 3
[36:46] 5 7 9 11 that are
[36:49] copied in Q also. Those elements should
[36:51] be printed.
[36:56] Yes, I'm getting those elements.
[36:59] Now, after copying at this line, I will
[37:02] say P assign Q. Now, P is pointing on Q.
[37:08] And I will make Q as null that Q array
[37:14] that Q pointer is made as null, so Q is
[37:16] not pointing anywhere. See, right now Q
[37:19] is not pointing anywhere, but if I try
[37:21] to print the array
[37:22] that is pointed by Q, then see what
[37:25] happens.
[37:28] I got an error because Q is null
[37:31] pointer. So, here inside the debug area,
[37:33] you can see that P Q is null right now.
[37:38] And P is pointing on some other
[37:39] location.
[37:41] So, I cannot use Q now.
[37:50] Now, let us print the values using P.
[37:57] Yes, the values are printed.
[37:59] But, one thing we have missed that
[38:01] that P array before P is pointing on Q,
[38:04] P should delete that previous array. So,
[38:07] for deleting free P.
[38:15] So, after freeing that memory, P should
[38:18] point on Q, then Q should be made as
[38:19] null, then I can access the array of
[38:22] size 10 with the help of Q pointer. So,
[38:24] though I don't have extra values, I have
[38:26] only five values stored
[38:29] in the new array also. So, I'm stored
[38:31] displaying only five elements.
[38:34] Let us put a breakpoint here in the
[38:36] beginning of the program and debug this
[38:38] program. So, let us trace it line by
[38:40] line and see how things are working.
[38:46] Now, P is pointer pointing to a address
[38:49] that is last four three digits if you
[38:51] see.
[38:52] If you look into this debug area, that
[38:54] is watch,
[38:55] pointer P is having address ending with
[38:58] 4F8. So, it is pointing on this address.
[39:02] Right now, pointer is garbage. It's not
[39:05] having any address valid address because
[39:08] this is statement of creating of memory
[39:10] from heap is not yet executed.
[39:13] Let us continue execution.
[39:19] Now, you can see that pointer P is
[39:20] pointing at an address that is ending
[39:22] 9B0.
[39:28] I'll continue.
[39:30] Now, Q is null right now. Q is null.
[39:33] So, Q is not pointing anywhere.
[39:36] Continue. Now, Q is pointing on an
[39:38] address ending with 470.
[39:43] And the value present at the first
[39:45] address where Q is pointing is zero. And
[39:48] the P is pointing on an address where
[39:49] the value is a three because we have
[39:52] initialized this value three.
[39:54] Q is just now created using this the
[39:57] statement. So, Q is having new address,
[39:59] but there are no values.
[40:02] Now, let us run this loop.
[40:04] The values are being copied.
[40:08] Now, you can see that Q is also having
[40:10] the first value, that is three, and the
[40:12] address of
[40:13] Q, you remember, that is 470.
[40:17] It is 470. And the P is 9B0.
[40:21] Now, it is at this line that is free P.
[40:24] Let us finish this line. So, P is gone.
[40:27] The address is same in P, but that
[40:30] address is invalid because the memory is
[40:31] already deallocated.
[40:34] Now, next line. This is very important
[40:36] line.
[40:38] P assign Q. So, the address of Q will be
[40:41] given to P. That is 470 will be given to
[40:44] P.
[40:46] Yes.
[40:48] P is having address
[40:50] P is having address 470, and Q is also
[40:53] having address 470.
[40:56] Now, next line.
[40:58] Q becomes null, and P is still pointing
[41:01] on the same address. So, P is pointing
[41:03] on a new array.
[41:04] So, when I print these values, they are
[41:06] printed They are printed using pointer
[41:09] P.
[41:11] So, that's all. I have demonstrated how
[41:14] a array size can be increased. I have
[41:16] written the code, and I have shown you.
[41:19] See, I have done board work, and that
[41:21] same board work I have given a
[41:23] demonstration of that one.
[41:26] See, whatever we saw on whiteboard, that
[41:28] same thing I have demonstrated, and I
[41:30] have shown you.
[41:32] Now, wherever you require, you have to
[41:34] follow these steps for increasing the
[41:36] size of an array.
[41:39] That's all in this video.
[41:41] Now, let us talk about two-dimensional
[41:43] array. See, already in the previous
[41:45] video, we have seen a single-dimension
[41:47] array.
[41:48] In programming languages, we can have
[41:51] multi-dimension arrays. So, basically,
[41:54] we use one dimension or two dimension,
[41:56] or at most, we go for three dimension.
[41:58] But, languages allow us to declare
[42:01] N-dimension arrays. So, commonly used
[42:03] one second one is 2D array. This is
[42:06] mostly useful for implementing matrices
[42:08] or for tabular data.
[42:11] Now, let us see how to create a
[42:13] two-dimensional array in C and C++.
[42:17] There are three methods of uh creating a
[42:19] two-dimensional array. So, let us look
[42:21] at them.
[42:24] First method is a normal declaration of
[42:27] two-dimension array
[42:29] along with
[42:30] name of an array, data type of an array,
[42:33] and dimensions. Let us say I want a
[42:36] array of size 3 by 4.
[42:39] So, we visualize that a array of 3 by 4
[42:42] size will be created inside main memory
[42:45] with a three rows and four columns.
[42:50] The indices will be
[42:52] 0 1 2 for rows and 0 1 2 3 for columns.
[42:58] We represent a two-dimensional array in
[43:01] rectangular form, that is like a box.
[43:03] But really, the memory allocated will be
[43:06] linear. For example, if you want to look
[43:08] at the addresses, let us assume that the
[43:11] first byte's address is 200 and 201,
[43:15] then this is 2 and 3, 4 and 5, 6 and 7.
[43:19] So, this is memory for each integer. As
[43:23] we are assuming that integer take two
[43:24] bytes, so two two bytes for each. This
[43:26] is 206 and 7, and this will be 8 and 9,
[43:31] 10 and 11,
[43:33] 12 and 13.
[43:34] So, this means that in real, the memory
[43:37] will be allocated like a single
[43:40] dimension array
[43:42] with 3 into 4 12 integers.
[43:49] So, memory will be allocated like a
[43:51] single dimension array, but the
[43:53] compilers allow us to access that single
[43:57] dimension array as a 2D array with row
[44:01] number and column number. So, any
[44:03] location if you want to access, then we
[44:05] can say A of Let us suppose here I want
[44:08] to write value 15. Then I can say of 1
[44:11] 2. This is one row number two column
[44:15] number value 15.
[44:18] So we can access 2D array with the two
[44:20] indices that is a row number and column
[44:22] number.
[44:23] How compiler will allow us to access as
[44:25] a two dimension though inside the main
[44:27] memory single dimension? We will learn
[44:29] it in the coming videos.
[44:33] So if you want to initialize this 2D
[44:35] array, then we can directly mention the
[44:37] list of elements here like three rows
[44:40] and four columns. So first row is having
[44:42] four elements 1 2 3 4 and the second row
[44:45] is having elements 2 4 6 8. Four
[44:48] elements are there and third row is
[44:51] having the elements 3 5 7 9.
[44:57] So a 2D array will be initialized.
[45:01] So declaration and initialize, this is
[45:04] common in C and C++ the same syntax is
[45:07] used and the array will be created
[45:09] inside stack.
[45:11] Because it is just like a variable,
[45:13] there is no new operator used. So once
[45:17] you use new operator, then only the
[45:19] array will be created in heap. So this
[45:21] is the first method of creating an array
[45:23] inside stack.
[45:25] Now let us look at second method how we
[45:27] can create a two dimensional array.
[45:30] We can take an array of pointers. So
[45:33] this is a pointer and this is an array
[45:36] of size three. So suppose I want the
[45:39] same size 3 by 4, so I have taken array
[45:42] of size three. As this is an array, so
[45:45] normal variable it will be created
[45:46] inside stack. But this is not an array
[45:49] of integers, this is an array of integer
[45:52] pointers. So I will draw that picture
[45:54] for that one. So this A will be created
[45:57] like this.
[46:00] And these are all pointers. So, this is
[46:03] index zero, index one, and index two.
[46:07] Now, I have just a
[46:09] array of three pointers.
[46:12] Now, where is two-dimensional array? So,
[46:14] I will create two-dimensional array in
[46:17] heap. So, what actually I want? I want
[46:21] an array of size four. So, that this
[46:25] pointer is pointing there. Then again,
[46:27] an array of size four. Now, again, an
[46:30] array of size four. So, I should have
[46:33] three arrays of size four
[46:36] created in heap.
[46:40] So, this is nothing but array of array
[46:43] of arrays. So, there are three arrays,
[46:47] and I have this array pointing to those
[46:49] three arrays, and together they form
[46:52] same two-dimensional array.
[46:56] So, this is the second method of
[46:57] creating two-dimensional array. Let us
[46:59] see the syntax how I can create these
[47:02] arrays. So, I have
[47:04] an array of size three. So, continuation
[47:07] of this one, I should say A of zero
[47:09] assign new int of size four.
[47:15] So, an array of size four will be
[47:16] created whose address will be stored in
[47:19] A of zero. Similarly, I should do it for
[47:21] A of one assign new int of size four,
[47:26] and I should do it for next location A
[47:28] of two assign new int of size four.
[47:34] So, I should create memory in heap for
[47:37] all those three arrays, and this array
[47:40] will be present in the stack. So, it
[47:42] will be pointing there. Now, even this
[47:44] is structure I can access just like a
[47:46] normal array. Suppose, I want to store a
[47:48] value here 15 1,2, then I can say that
[47:52] array A, which is the pointer, A of one
[47:56] means this one.
[47:58] It's a two means this one. I can store
[48:01] value 15.
[48:03] So in the same way I can access it.
[48:06] So these were the two methods. Now I'll
[48:08] show you third method. But now let us
[48:10] look at third method.
[48:12] See third method, I can take a double
[48:15] pointer.
[48:17] See one was a two-dimensional array
[48:19] directly inside the stack. Now this is a
[48:22] array of pointers inside the stack, but
[48:24] actual arrays in the heap. Now almost
[48:27] everything in heap now. So for that I
[48:30] will use double pointer. So declaration
[48:33] of double pointer integer star
[48:36] star A asterisk asterisk A. So this A is
[48:41] a pointer, double pointer. And this is
[48:44] like a variable, there is no new
[48:46] operator here, new is not used. So this
[48:49] will be created inside stack.
[48:52] Now
[48:53] I need this array as well as these
[48:56] arrays. So for this first array I should
[48:59] say A assign new
[49:03] integer of size three. I'm creating this
[49:06] array. So you remember this array was
[49:08] nothing but array of integer pointers.
[49:10] So yes, I should mention star here. So
[49:14] this is an array of pointers of type
[49:16] integer. So an array of pointers of type
[49:19] integer will be created of size three
[49:22] and this will be pointing on this one.
[49:24] So if I take the indices, these are
[49:25] zero, one, and two.
[49:28] So this itself is created in heap now.
[49:31] Array of pointers is created in heap.
[49:33] Now I will prepare other arrays. So I
[49:36] have to create these three arrays
[49:39] and assign them to these three pointers.
[49:44] So the indices are
[49:46] zero to three.
[49:50] Now how to create those three? So, same
[49:53] method as I did here, I should say A of
[49:56] zero assign new
[49:58] int of size
[50:00] four.
[50:01] Then, A of one
[50:04] A of two assign new.
[50:06] So, that's all. The entire structure is
[50:09] in heap now. Only this double pointer is
[50:12] inside stack because this is new, this
[50:14] is new, this is new. So, this is created
[50:17] with new and all three are created with
[50:18] new. Everything is in heap.
[50:21] So, that's all. These are three methods.
[50:24] So, these are the three methods of
[50:25] creating a two-dimensional array.
[50:27] Completely inside the stack, partial in
[50:29] the stack, partial in heap, everything
[50:32] in heap now.
[50:34] Now, one more thing. See, every time I
[50:36] have used new operator. So, if you want
[50:38] to know how it is done in C language,
[50:40] then you know very well that malloc
[50:42] function is used for allocating the
[50:44] memory. So, wherever we have used new,
[50:47] we have to use malloc function there.
[50:49] Because in C language, malloc means
[50:51] memory in heap. Now, last thing. How to
[50:54] access this two-dimensional arrays?
[50:56] These two-dimensional arrays can be
[50:59] accessed using nested two for loops. So,
[51:03] for accessing this array or any of these
[51:06] arrays, I can use indices I, let us say
[51:10] starts from zero, I less than three
[51:12] because there are three rows and
[51:15] next one more inner for loop, J assign
[51:18] zero, J is less than four because there
[51:20] are four columns, J plus plus. So, in
[51:23] all my examples, I've taken three rows
[51:25] and four columns. So, three rows and
[51:27] four columns. So, accessing all these
[51:29] elements, so I can say A of I J.
[51:35] Whatever I want to do here.
[51:37] Whether I want to print them or access
[51:39] or modify them or initialize them,
[51:41] whatever I want to do, I can do it. But,
[51:43] this will help me traverse through all
[51:46] the elements in a two-dimensional array
[51:48] row by row. So, it will access like
[51:50] this.
[51:52] First this element, next next next, then
[51:54] it will come to next row, all the
[51:56] elements in a row, then next all the
[51:58] elements in a row. Now, for accessing
[52:00] all the elements in a row, this is the
[52:02] loop. For different rows, changing the
[52:05] rows, this is the loop. Outer loop is
[52:07] there. So, that's all. These are the few
[52:10] basic things that one must know about a
[52:12] two-dimensional array. So, I have
[52:14] explained them. Now, let us continue
[52:16] with the other topics in an array.
[52:19] In this video, I'll give you
[52:20] demonstration for creating
[52:21] two-dimensional arrays. In the previous
[52:23] video, in the whiteboard work, I have
[52:25] shown you what are the methods of
[52:27] creating a two-dimensional array.
[52:30] So, just we look at those methods and
[52:32] verify them here and see how to do that.
[52:34] In our remaining part of course,
[52:36] wherever we require two-dimensional
[52:38] arrays, we may be using any one of these
[52:40] methods. So, let us see the method. The
[52:42] first method I have shown you is we can
[52:44] create an array inside the stack. So, a
[52:46] normal method that's provided by the
[52:48] compiler, if you follow that method,
[52:50] a array will be created inside stack.
[52:53] Suppose I want a array with a three rows
[52:57] and four columns. So, this is a
[52:59] two-dimensional array of dimensions
[53:00] three by four. If you want to initialize
[53:03] then every row I should initialize it.
[53:05] So, the first row is having values
[53:08] one to four.
[53:10] Second row is having values
[53:14] even numbers, then third row I will take
[53:17] the values
[53:19] odd numbers.
[53:21] So, there are three rows and each row is
[53:23] having four four elements because there
[53:25] are four columns. So, this is the way we
[53:27] can create a two-dimensional array. This
[53:29] will be created inside stack. Remember
[53:32] this.
[53:34] Then the second method is
[53:35] I can take an
[53:37] array of pointers
[53:40] of size three. So, this is a is for
[53:43] rows.
[53:44] Now, columns are not created. So, this
[53:46] array of pointers will be created inside
[53:49] stack.
[53:50] Then,
[53:51] to each location in this array, I can
[53:54] create an array in heap and assign it.
[53:57] So, for each location, I will create an
[53:59] array inside heap and assign it.
[54:03] Then,
[54:04] create an integer array malloc of size
[54:07] four
[54:09] and multiplied by size of an integer.
[54:13] So, this is for the first location.
[54:16] Then, I have three such locations. So, I
[54:18] will
[54:20] I will copy this code
[54:22] and paste it for the
[54:25] remaining locations.
[54:27] This is for one and this is for two.
[54:31] Now, this is for one and this is for
[54:32] two.
[54:36] So, this is how two-dimensional array is
[54:37] created. Already, we have seen the board
[54:39] work for this one.
[54:41] Now, the third method I have shown you,
[54:42] for that, I will take a double pointer
[54:45] here.
[54:46] Integer
[54:47] double pointer C.
[54:49] In C language, we should declare the
[54:51] variables in the beginning. So, I have
[54:53] declared it in the beginning.
[54:54] Now,
[54:56] it's just a double pointer. So, first of
[54:57] all, I should create a array for rows.
[55:01] So, C assign
[55:09] integer double pointer
[55:12] malloc
[55:13] It's a size of three, that is size of
[55:18] integer pointers.
[55:21] So, first is an array of pointers.
[55:25] Mhm.
[55:26] The next is C of zero. I should
[55:29] assign a array from heap. So, I should
[55:32] create an array in heap and I should
[55:33] assign it.
[55:35] So, array in heap is of size four now.
[55:38] So, this is for first row.
[55:40] 4 into size of
[55:43] integer.
[55:45] Then same code I will copy it and use it
[55:47] for rest of the rows.
[55:53] Yes.
[55:55] This is for one and this is for two.
[55:59] See, this first row itself rows itself
[56:03] are created in heap and the rest of the
[56:06] arrays, three arrays are also created in
[56:08] heap.
[56:09] And here
[56:11] rows were created inside the stack and
[56:14] these arrays, that is three arrays are
[56:17] created in heap.
[56:19] So, that's all. Syntactically I have
[56:21] shown you how the code works. So, I have
[56:24] written the code and all this code is
[56:26] error-free.
[56:27] Now, one thing we'll do, I will take for
[56:30] loop and I will try to print all these
[56:32] arrays one by one.
[56:36] I will take integer I and J using these
[56:40] I will print the elements of these
[56:42] arrays. Though I have elements only in
[56:44] first array, I don't have elements in
[56:46] second and third array, but just let us
[56:48] print them and see
[56:50] whether it works or it gives an error.
[56:53] So, using for loop
[56:56] I is less than three, I plus plus
[56:59] then
[57:01] for
[57:04] J is sign zero
[57:06] J is less than four because this is for
[57:08] columns, J plus plus
[57:12] then print F
[57:15] percentile D
[57:20] A of I comma J.
[57:24] So, values from this array will be
[57:25] printed, that is
[57:27] A array that is created inside stack.
[57:30] Now, for a new line I will say printf
[57:32] /n. So, after the end of every row, let
[57:36] it take a new line space.
[57:39] Let us run this one.
[57:43] Yes, the values are printed. The values
[57:44] that I have given here, the same values
[57:47] are printed row by row.
[57:52] Now, the same for loop, I will cut it
[57:54] from here
[57:55] and paste it after creation of B.
[58:01] And I will print array B. B is not
[58:04] having any elements, but it should get
[58:06] garbage values. But, it should not give
[58:08] any error that invalid memory.
[58:13] Yes, it is printing garbage values are
[58:16] printed.
[58:23] Then, same two for loops, I'll use it
[58:25] for C to verify whether it will allow us
[58:27] to access that memory or not. If the
[58:30] arrays are not created, then it will not
[58:31] allow us to access. This is created
[58:33] using double pointers. So, I'm able to
[58:36] access this locations using C of I, J.
[58:40] Yes, it is working. Some values are
[58:42] zeros, and some values are random. So,
[58:44] all those values are garbage values only
[58:46] because we have not initialized.
[58:49] That's all. We have seen the methods how
[58:51] to create two-dimensional array. Just
[58:53] for confirmation, I have written the
[58:54] code.
[58:56] We may be using it if required.
[59:02] That's all in this video.
[59:05] Now, let us learn how a compiler handles
[59:08] array or manage array.
[59:11] So, what are the issues that it has to
[59:13] deal with? So, for explaining that, I
[59:15] will take an example of simple variable,
[59:17] that is scalar variable.
[59:19] See, in our programs, we use variables
[59:23] as names for representing some data.
[59:27] But, when compiler converts into machine
[59:29] code, machine code will not have
[59:30] variable names.
[59:33] During execution for this variable, let
[59:35] us say two bytes of memory is allocated
[59:38] and the addresses are 100 and 101.
[59:41] Suppose during execution this is the
[59:43] memory allocated.
[59:46] This variable X represents this address.
[59:50] And the value 10 has to be stored at
[59:52] that location means at this address 100.
[59:57] So, basically machine code should refer
[01:00:00] the location with their addresses, not
[01:00:03] by name.
[01:00:04] Then compiler has to convert name into
[01:00:06] an address.
[01:00:08] When the address can be known? When the
[01:00:10] memory is allocated. When the memory is
[01:00:12] allocated? During execution. So, it
[01:00:14] means the memory for the variable is
[01:00:16] allocated at execution, then only the
[01:00:18] address can be known. Then the how
[01:00:20] compiler will write down the address at
[01:00:22] compile time.
[01:00:23] So, that's what we will learn about this
[01:00:25] issue in array, that is a vector
[01:00:29] variable. Here we have an example array
[01:00:31] which is already initialized in my
[01:00:34] program. Then during execution time,
[01:00:37] memory will be allocated for the array
[01:00:39] and the values will be stored in
[01:00:40] corresponding locations. Assume that
[01:00:42] these are the addresses.
[01:00:44] Now, in my program if I have written, I
[01:00:47] want to access A of
[01:00:50] three and there I want to store the
[01:00:53] value 10. So, it means I want to store
[01:00:55] the value 10 here.
[01:00:57] Then
[01:00:58] this is statement in my program has to
[01:01:00] be converted into machine code which is
[01:01:02] referring to this address that is 206.
[01:01:06] The address is 206. So, compiler has to
[01:01:09] refer to that location that is 206. So,
[01:01:13] how this code of our program will get
[01:01:16] converted into machine code? So,
[01:01:18] actually compiler needs address of a
[01:01:21] location
[01:01:23] A of three.
[01:01:25] So, how it is obtained? So, at compile
[01:01:28] time the address cannot be known. So,
[01:01:29] compiler will write down a formula for
[01:01:31] obtaining the address.
[01:01:33] So, how that formula looks like? Let us
[01:01:35] see.
[01:01:36] See, this is the name of an array and
[01:01:38] this is the first address of an array.
[01:01:40] So, this first address we can call it as
[01:01:42] L naught or location zero. And the name
[01:01:45] of an array is called as base address of
[01:01:48] an array. So, any location in an array,
[01:01:50] index in an array can be accessed with
[01:01:52] the help of base address. Let us say L
[01:01:54] naught. So, L naught is the base address
[01:01:57] that is 200. So, this is the starting
[01:01:59] address of an array. Then, which index I
[01:02:01] want to access? This one. Its address is
[01:02:03] how much? Six. So, this address I want
[01:02:06] to access that is three
[01:02:08] into two.
[01:02:10] This gives me 206.
[01:02:14] So, why three? This is the index that I
[01:02:16] want to access. Then, why two? Because
[01:02:19] each integer takes two bytes, two bytes.
[01:02:22] So, it means from the base address of an
[01:02:24] array, location zero, I want to access
[01:02:27] the third index where each index it
[01:02:29] takes two bytes. So, the final address
[01:02:32] will be 206. So, using this formula, the
[01:02:35] address 206 can be obtained. So, I will
[01:02:37] convert that into a formula. This is L
[01:02:41] naught plus three into size of an
[01:02:45] integer.
[01:02:47] So, address of
[01:02:50] A of three
[01:02:52] is obtained like this. Now, for address
[01:02:54] of any location
[01:02:57] A of I how it can be obtained?
[01:03:01] L naught plus I into index into Now,
[01:03:05] this is a example of integer array, so
[01:03:08] two bytes. If it is float array, it may
[01:03:10] be four bytes. Character array may be
[01:03:12] one byte. Double array, it may be eight
[01:03:14] bytes. So, So, on the data type, the
[01:03:17] number of bytes taken by each element
[01:03:19] may vary.
[01:03:20] So, I have to take the size of the data
[01:03:23] type. So, we use a variable that is W.
[01:03:27] So, this is base address.
[01:03:31] This is index.
[01:03:34] And this is size of data type.
[01:03:38] So, this is the formula used by compiler
[01:03:41] for converting any
[01:03:44] index to a address. Now, for obtaining
[01:03:48] the actual address during run time, our
[01:03:50] program must know the base address of an
[01:03:53] array. So, base address of an array will
[01:03:55] be updated once the program starts
[01:03:59] running and once the memory for array is
[01:04:02] allocated. So, the address of this
[01:04:04] location is known during run time and it
[01:04:06] is updated at run time and it is called
[01:04:08] as data binding. So, if you want to
[01:04:11] study about this one, you can study
[01:04:12] about systems programming or assembly
[01:04:15] language programming. There you can
[01:04:17] learn about these things.
[01:04:19] So, here we have learned how a compiler
[01:04:22] generates the formula. So, this is a
[01:04:24] formula for address and this is not
[01:04:27] actual address. So, this is a logical
[01:04:29] address.
[01:04:30] And this is not a direct address. This
[01:04:32] is related to base address. So, this is
[01:04:35] also a relative address. So, the formula
[01:04:38] is a relative formula based on base
[01:04:42] address of an array. Some compilers
[01:04:44] allow declaration of an array from
[01:04:46] starting index one also. Then how the
[01:04:49] formula will change? Let us look at it.
[01:04:52] I have modified the declaration.
[01:04:54] Suppose, in some XYZ language, a array
[01:04:58] can have indices starting from one to
[01:05:00] five. Then, this will be index one and
[01:05:03] the last index will be five.
[01:05:05] Now, if I want to access any location in
[01:05:08] an array that is A of three, this
[01:05:09] location I want to modify to 10, then
[01:05:12] how the formula is written by compiler.
[01:05:15] So, for the address of a location
[01:05:19] A of 3,
[01:05:21] the base address, that is 200,
[01:05:25] plus
[01:05:26] C third index is this one, so it has to
[01:05:28] reach here, that is 204.
[01:05:30] So, it should be 3 minus 1, one less,
[01:05:33] because the indices are starting from
[01:05:35] one onwards.
[01:05:36] Into two, so this will be 204.
[01:05:40] So, for any
[01:05:42] index, address can be obtained by A of I
[01:05:47] is this is location zero, L naught, and
[01:05:50] this is index I minus 1 into this is the
[01:05:54] word size, that is the data type size,
[01:05:57] that is the W.
[01:05:59] So, this is the formula if the indices
[01:06:01] are starting from one onwards.
[01:06:04] So, this is not there in C C++, but this
[01:06:06] used to be there in old languages.
[01:06:10] They used to allow array declaration
[01:06:12] from any starting index. So, programmer
[01:06:14] can have an array start from zero also,
[01:06:17] one also.
[01:06:19] But, in C C++, it is strictly from zero
[01:06:22] only.
[01:06:23] Reason, why? Why in C C++ we cannot have
[01:06:27] array indices starting from one onwards?
[01:06:29] Let us see this.
[01:06:31] We have seen the formula for array
[01:06:33] indices starting from zero also. So,
[01:06:35] just I will recall that formula, that
[01:06:37] was I into W. And this is index starting
[01:06:41] from one onwards, so this is the
[01:06:42] formula. So, the difference is there is
[01:06:45] one arithmetic operation extra, that is
[01:06:48] minus one is extra. So, if you count the
[01:06:50] number of operations, 1 2 3.
[01:06:53] And here, there are only two operations.
[01:06:55] So, one operation is extra.
[01:06:57] So, as this is an array, for accessing
[01:07:00] each location, I have to use the
[01:07:03] formula.
[01:07:04] So, if there are n location, then this
[01:07:07] operation will be performed for n times
[01:07:10] if I'm accessing all the locations. So,
[01:07:12] though it's a matter of a single
[01:07:14] operation that is just one operation
[01:07:16] extra, but it will be slower depending
[01:07:19] on the size of an array. If the size is
[01:07:20] very large and I'm accessing all the
[01:07:22] elements, then it will be really slow.
[01:07:24] It will be affecting the time taken by a
[01:07:27] program. That is the reason C C++
[01:07:30] languages they allow array indices
[01:07:32] starting from zero only because the
[01:07:34] formula have just less number of
[01:07:37] operations and it is faster than this
[01:07:40] formula.
[01:07:42] So, that's all about single dimension
[01:07:44] array. Now, we will learn about
[01:07:46] two-dimensional array. Let us see how a
[01:07:49] two-dimensional array is managed or
[01:07:51] handled by a compiler.
[01:07:54] In our program, we declare a
[01:07:55] two-dimensional array like this. Suppose
[01:07:58] I'm declaring an integer type array of
[01:08:00] dimensions three cross four, three rows
[01:08:03] and four columns. Then, this is how 2D
[01:08:06] array we draw on paper.
[01:08:08] We visualize that this is how we got a
[01:08:10] two-dimensional array. Any element we
[01:08:12] can access with the help of its row
[01:08:14] number and column number. So, this is A
[01:08:16] of two comma one. But, the reality is
[01:08:20] during execution the actual memory
[01:08:22] allocated will be linear. It will not be
[01:08:25] in terms of rows and columns, but it
[01:08:28] will be like a single dimension array.
[01:08:31] So, total three fours 12 locations are
[01:08:34] there. So, here we will have 12
[01:08:36] locations of type whatever you have
[01:08:38] mentioned here. So, it's an integer, so
[01:08:40] two two bytes I'm taking here.
[01:08:43] Now, these are total 11 location and
[01:08:45] assuming that the starting address is
[01:08:46] 200, so so on two two bytes I have
[01:08:49] increased and the last bytes addresses
[01:08:52] 222
[01:08:53] and 23. Now, the problem is how the
[01:08:56] elements of two-dimensional array are
[01:08:59] stored in single dimension array. How
[01:09:01] the mapping is done?
[01:09:03] So, there are two methods of mapping.
[01:09:05] First one is row major mapping
[01:09:09] and second one is
[01:09:11] column major mapping.
[01:09:13] So, these are the two representations of
[01:09:16] a 2D array mapped on a single dimension
[01:09:20] array.
[01:09:21] So, we call it as row major mapping or
[01:09:24] row major representation, column major
[01:09:26] mapping or column major representation.
[01:09:28] So, we'll learn about both. So, first
[01:09:30] let us look at how a row major mapping
[01:09:32] is done.
[01:09:33] Let us look at row major mapping. See,
[01:09:36] we assume that we have a two-dimensional
[01:09:38] array and the elements are organized
[01:09:39] like this. But, the reality is they are
[01:09:42] stored in a single dimension array. How
[01:09:43] they are stored? So, they are stored a
[01:09:46] row by row. Row by row. So, in row major
[01:09:49] mapping, the elements are stored row by
[01:09:52] row. So, I will copy these elements row
[01:09:54] by row. So, first row elements are A00,
[01:09:56] 01, 02, 03. So, I will write them here.
[01:09:59] A00, this is the first value in the
[01:10:02] first row, 01, 02, and 03. So, this is
[01:10:08] row number
[01:10:10] zero. This is index zero. So, I have
[01:10:14] completed the first row that whose index
[01:10:16] is zero. Now, I will write down the
[01:10:18] elements of next row.
[01:10:21] This is row index one.
[01:10:25] Then, the last row
[01:10:27] A20.
[01:10:29] So, this is row index two.
[01:10:32] So, that's how the elements of that
[01:10:35] two-dimensional array are mapped on a
[01:10:38] single dimension array row by row. So,
[01:10:41] this is a row major mapping. Now, we
[01:10:44] need a formula that should be used by a
[01:10:46] compiler for accessing that
[01:10:48] two-dimensional array.
[01:10:50] Let us take one example and frame a
[01:10:52] formula. Suppose, I want to access an
[01:10:55] element at index A of 1 2.
[01:11:01] This is the element.
[01:11:03] A of 1 2 is present here.
[01:11:07] Now, in our program, we will be writing
[01:11:09] like this. Like say A of 1 {comma} 2, I
[01:11:11] want to store the value 10.
[01:11:13] Then, that A of 1 {comma} 2 has to be
[01:11:16] converted in the form of an address by
[01:11:19] the compiler. So, already we know that
[01:11:21] the addresses cannot be known at compile
[01:11:23] time. So, compiler writes the formula
[01:11:26] for obtaining an address. So, let us
[01:11:28] observe what should be the formula. So,
[01:11:30] for getting the address of a
[01:11:33] location
[01:11:34] A of
[01:11:36] 1 2
[01:11:38] This ADD is for address.
[01:11:41] Now, to reach here, that is 1 {comma} 2
[01:11:44] 1 {comma} 2, that is present here. To
[01:11:46] reach here, the starting address is 200.
[01:11:49] Okay? 200.
[01:11:51] Then,
[01:11:52] I should leave all the elements of one
[01:11:55] row, that is row zero. I should leave
[01:11:57] all these elements. So, how many
[01:11:58] elements are there in a row? 1 2 3 4,
[01:12:01] that's equal to number of columns. So, I
[01:12:04] should leave four elements. Okay? Four
[01:12:07] elements I have left.
[01:12:09] Then,
[01:12:11] 200 is this index. If I leave four
[01:12:13] elements, then 1 2 3 4. So, here I was
[01:12:17] in the beginning of the first row. Now,
[01:12:19] here I am in the beginning of the next
[01:12:21] row, that is index one. So, here, so
[01:12:25] from here, how many elements I should
[01:12:26] move further? 1 2 elements. So, I should
[01:12:29] move further for two elements.
[01:12:32] Then, this should be multiplied by
[01:12:37] There are two bytes for each element
[01:12:39] because it's integer. So, this is two.
[01:12:41] So, this is 200 plus
[01:12:45] 6 into 2
[01:12:48] that is 212.
[01:12:50] Yes, I got the address.
[01:12:54] So, I have just worked out by taking the
[01:12:56] addresses and the index, and I got the
[01:12:59] address that is 212.
[01:13:01] Now, let us try for one more element.
[01:13:04] Let us take this element 2,3.
[01:13:07] A of 2,3.
[01:13:09] So, address of
[01:13:12] A of 2
[01:13:15] 3
[01:13:16] So, if you see that A of 2,3 is at
[01:13:18] address 222. So, I should get this one.
[01:13:22] Now, let us follow the same method.
[01:13:24] First, take base address 200, then
[01:13:27] from here, I should skip two rows.
[01:13:31] So, two rows means
[01:13:33] two into four elements in each row.
[01:13:37] See, four elements and four elements.
[01:13:39] So, I should skip two rows. This I will
[01:13:41] be in the beginning of this third row,
[01:13:43] that is row index two.
[01:13:45] So, it means earlier, this was 1 into 4.
[01:13:48] Now, this is 2 into 4 plus
[01:13:51] Once I skip first row, I'm here. Then, I
[01:13:53] skip one more row, I'm here.
[01:13:56] Then, I'm skipping four four elements
[01:13:57] for each row. Then, from here, I should
[01:13:59] move by 1 2 3 elements. So, that is
[01:14:02] three elements. This whole thing should
[01:14:04] be multiplied by two.
[01:14:06] So, how much it gives? This is 4 2 8
[01:14:09] plus 3 11 into 2 22. So, this will be
[01:14:13] 222.
[01:14:15] So, yes, I got the address.
[01:14:18] Now, from these two examples, we are in
[01:14:20] a position to write a formula for it.
[01:14:23] So, address of any index
[01:14:27] A of
[01:14:29] I J
[01:14:31] We take I and J for represent the
[01:14:33] indices. So, this is L not plus L not is
[01:14:37] the starting address, that is the base
[01:14:39] address, L zero location zero, plus
[01:14:42] This is
[01:14:44] I
[01:14:45] into
[01:14:47] number of columns. So, let us call this
[01:14:49] as m cross n.
[01:14:51] So, this is n.
[01:14:54] Plus
[01:14:55] this one is three, third element, so
[01:14:58] that is j.
[01:15:00] And this whole thing multiplied by w.
[01:15:03] So, this is the row major formula used
[01:15:05] by compilers for obtaining the address
[01:15:08] of elements of two-dimensional array.
[01:15:12] Two-dimensional arrays are accessed
[01:15:13] using two indices, but actually they are
[01:15:16] kept in a single dimension array which
[01:15:18] needs just one index or the address. So,
[01:15:21] the address is obtained like this.
[01:15:24] So, this is about row major. Now, one
[01:15:25] more thing.
[01:15:26] See, if a language is allowing indices
[01:15:30] starting from one onwards, then how the
[01:15:32] formula looks like?
[01:15:34] So, I'll just remove this and show you
[01:15:36] here.
[01:15:37] If a language is allowing indices from a
[01:15:41] of 1 to 3 and 1 to 4,
[01:15:47] I will not work out everything. Directly
[01:15:49] I will write on the formula. So, the
[01:15:51] address of any location
[01:15:53] a of ij
[01:15:56] can be obtained by l naught plus
[01:15:59] See, already we have seen it for single
[01:16:01] dimension, this i minus one was used
[01:16:04] because the indices are starting from
[01:16:05] one onwards. So, here it will be i minus
[01:16:08] one
[01:16:10] into n
[01:16:11] plus
[01:16:12] j minus one also.
[01:16:15] Then multiplied by word size, that is
[01:16:18] the data type size.
[01:16:21] So, this is the formula.
[01:16:23] See, in C C++ array indices start from
[01:16:26] zero only. So, C C++ uses this formula.
[01:16:30] C language and C++ language follows row
[01:16:33] major representation and they use this
[01:16:35] formula.
[01:16:36] But, if any language is allowing indices
[01:16:39] from one onwards, then it has to use
[01:16:40] this formula.
[01:16:42] Now, when it comes to two dimension, let
[01:16:44] us count the number of operations done.
[01:16:46] 1 2 3 4 and here 1 2 3 4 5 6. So two
[01:16:53] extra arithmetic operations are
[01:16:55] performed. So this formula will be
[01:16:57] little slower or costly in terms of
[01:17:00] time. If you have many elements and the
[01:17:02] dimensions are very large, then
[01:17:04] accessing all the elements will be very
[01:17:06] time consuming using this formula. So
[01:17:08] that is the reason the languages like C
[01:17:11] C++ they start indices from zero only.
[01:17:14] They allow indices only from zero
[01:17:15] onwards so that the formula is little
[01:17:18] simpler than the other formula. Now
[01:17:20] next, let us frame a formula for column
[01:17:24] major mapping.
[01:17:25] For column major mapping, the elements
[01:17:27] of this two dimensional array are mapped
[01:17:30] on a single dimension array column by
[01:17:32] column. So I will fill these elements
[01:17:35] column by column. These elements I will
[01:17:37] take. So first column are A 0 0, 1 0 and
[01:17:40] 2 0. So first column elements are A 0 0
[01:17:45] 1 0
[01:17:47] 2 0.
[01:17:48] This is column index zero.
[01:17:51] Then
[01:17:53] this is column index one. These
[01:17:55] elements. These elements. So I have
[01:17:57] finished with these elements now. These
[01:17:59] elements. The next column A of 0 2
[01:18:04] This is column two.
[01:18:06] Then A of this is column three.
[01:18:11] So the elements of two dimensional array
[01:18:14] are mapped column by column.
[01:18:17] Now we need a formula for obtaining an
[01:18:20] address of any element. So this address
[01:18:24] I have taken the addresses starting from
[01:18:26] 200 and as integer every integer takes
[01:18:28] two two bytes. So this is 200 and 201,
[01:18:30] 202 and 203, 204 and five so on. Then
[01:18:34] let us call this as starting address as
[01:18:36] L
[01:18:37] that is base address, location zero.
[01:18:40] Now, let us take some example elements
[01:18:42] from there and frame a formula.
[01:18:46] Suppose I want this element 1, 2. See
[01:18:50] where is 1, 2? Here.
[01:18:53] It is in column number two.
[01:18:55] So, address of
[01:18:59] A of 1, 2.
[01:19:02] This is base address, that is 200.
[01:19:06] Plus
[01:19:07] Now, for reaching this element, I should
[01:19:09] skip first column and the second column.
[01:19:11] Two columns I should skip. So, two into
[01:19:14] how many elements are there in each
[01:19:15] column? 1 2 3, 1 2 3. So, columns are
[01:19:18] having three elements, so that is equal
[01:19:20] to the number of rows, M, that is three.
[01:19:23] So, three, then plus.
[01:19:26] Once I have skipped column zero, I am
[01:19:28] here. Then I have skipped column one, I
[01:19:30] am here. Now, from here how much I
[01:19:31] should move? Just one element. One
[01:19:33] element. So, you can see that this is 2,
[01:19:36] 1. So, this is 2, 1.
[01:19:39] And this has to be multiplied with
[01:19:41] number of elements that are W. So, this
[01:19:44] will be
[01:19:45] 3 2 6 + 1 7 into 2 14. So, this is 214.
[01:19:51] And you can see that the index of A of
[01:19:53] 1, 2 is 214. So, that's how we got the
[01:19:57] address of a given element. Now, let us
[01:20:00] take one more element, that is A of 2,
[01:20:03] 2.
[01:20:04] Now, let us take one more element, that
[01:20:06] is A of 1, 3. 1, 3 is here.
[01:20:11] So, how to get the address of that one?
[01:20:13] So, the address of A of 1, 3
[01:20:17] is 200 plus
[01:20:20] Now, I have to skip how many columns? I
[01:20:23] should go in third column. So, skip
[01:20:24] zeroth column, first column and second
[01:20:26] column. So, I have to skip three
[01:20:28] columns. And in each column, how many
[01:20:30] elements are there? These are
[01:20:32] three elements. So, this is 3 into 3
[01:20:35] plus
[01:20:37] Now, once I reach here, how many
[01:20:38] elements I should move forward? Just one
[01:20:40] element. So, this is multiplied by two
[01:20:44] and this is 3 * 3 = 9 + 1 10 * 2, that
[01:20:47] is 20 220. So, 220 this is the address.
[01:20:51] Yes, 220. The element is here.
[01:20:55] This A of 1,3 is here.
[01:20:59] So, now we have two examples, we can
[01:21:01] prepare a formula. So, let us prepare a
[01:21:03] formula for any index
[01:21:06] A of
[01:21:08] I,J.
[01:21:11] This is
[01:21:13] location zero, that is base address L0
[01:21:16] plus
[01:21:18] This is three and one or two and one.
[01:21:21] See,
[01:21:22] two, one three, one are used. So, this
[01:21:24] means first J then I. So, this is J into
[01:21:28] number of elements in a row, that is
[01:21:31] three.
[01:21:33] Number of elements in a column, that is
[01:21:34] equal to number of rows, three. So, this
[01:21:36] is M plus One is taken at the last, so
[01:21:40] this is I, that is row number.
[01:21:44] This is multiplied by W.
[01:21:47] So, this is the formula for column major
[01:21:50] representation or column major mapping.
[01:21:52] I will write down row major mapping
[01:21:54] formula also just below this one, so
[01:21:56] that we can see the difference between
[01:21:58] them. This was row major formula. L0
[01:22:02] plus I into N
[01:22:05] plus J
[01:22:07] multiplied by W.
[01:22:09] So, in row major, I was used first then
[01:22:12] J, but in column major, J is used first
[01:22:15] then I.
[01:22:17] So, I can say that in row major, we go
[01:22:19] from left to right and in column major,
[01:22:22] we go from right to left. So, first J
[01:22:25] then I. So, first J then I. In row
[01:22:28] major, first I then J. So, this is for
[01:22:31] row major and this is for column major.
[01:22:36] Now, let us observe one more thing here.
[01:22:38] See,
[01:22:39] these two formulas, how many operations
[01:22:41] they are having? Plus, plus, multiply,
[01:22:44] multiply, plus, plus, multiply,
[01:22:45] multiply. So, both the formulas are
[01:22:49] having same number of operations. So, in
[01:22:51] terms of time, both are equally
[01:22:54] efficient. So, a compiler may follow row
[01:22:57] major mapping, or it may follow column
[01:22:59] major mapping. So, if you are designing
[01:23:02] your own compiler, then you have option
[01:23:04] to select anyone. Both are equally
[01:23:06] efficient. There is no difference in
[01:23:08] them.
[01:23:09] We can't say one formula is better than
[01:23:11] another in any situation.
[01:23:15] But, C C++ follows row major formula,
[01:23:18] this one.
[01:23:19] They follow row major formula.
[01:23:24] So, that's all about 2D.
[01:23:26] So,
[01:23:28] So, that's all about two-dimensional
[01:23:30] array. We have discussed how row major
[01:23:32] and column major mapping is done. And we
[01:23:34] have seen the formula for index starting
[01:23:37] from zero, and also we have seen the
[01:23:39] formula for indices starting from one
[01:23:41] also for row major. Now, for column
[01:23:43] major, you can devise the formula. So,
[01:23:46] you know well that only I - 1 and J - 1,
[01:23:49] - 1 has to be taken, then we get the
[01:23:50] formula for
[01:23:52] indices starting from one onwards. So,
[01:23:54] that's all in this topic.
[01:23:56] Let us prepare a row major formula and
[01:23:58] column major formula for four-dimension
[01:24:01] array. I'll show you the approach. Then,
[01:24:03] using same approach, we will see row
[01:24:05] major column major formula for
[01:24:07] three-dimensional array also. So, here
[01:24:10] already I have an example of a
[01:24:11] four-dimension array. Array name is A.
[01:24:14] Data type is something.
[01:24:16] And the dimensions are D1, D2, D3, and
[01:24:18] D4.
[01:24:20] Now, for the address of any location
[01:24:22] whose indices are I1, I2, I3, I4, then
[01:24:27] what should be the formula?
[01:24:29] So, let me prepare the formula for row
[01:24:31] major.
[01:24:34] In two-dimensional arrays already I have
[01:24:36] explained that row major is
[01:24:38] done from left to right and column major
[01:24:40] is from right to left. So, we will go
[01:24:43] from left to right. So, let us take
[01:24:45] first is I1, the first index
[01:24:49] into multiplied by leaving first
[01:24:52] dimension multiply rest of all the
[01:24:55] dimensions. So, this is a D2 into D3
[01:24:58] into D4.
[01:25:02] Now, second index, that is I2. I2
[01:25:07] is multiplied by
[01:25:09] after dimension two, all the dimensions
[01:25:11] should be multiplied. So, this is a D3
[01:25:14] into D4, dimension three and dimension
[01:25:16] four.
[01:25:17] The next is index three, I3. So, the
[01:25:21] third index, I3
[01:25:24] after third index. So, after third
[01:25:27] dimension all dimensions should be
[01:25:29] multiplied. So, only D4 is there.
[01:25:31] Then plus
[01:25:32] last
[01:25:34] I4
[01:25:35] fourth index. After fourth dimension
[01:25:38] everything should be multiplied. So,
[01:25:39] nothing is there. So, multiplied by W.
[01:25:44] This is the formula for row major
[01:25:46] mapping of a four-dimension array. So,
[01:25:49] you might have observed what is the
[01:25:51] procedure for preparing the formula.
[01:25:53] So, for every index we take the
[01:25:55] dimensions after those dimensions. For
[01:25:58] I2 we take all the dimensions after D2.
[01:26:02] So, this is the approach we have taken
[01:26:03] it from left to right. Now, I will write
[01:26:06] the formula for column major.
[01:26:09] Column major formula
[01:26:12] address of Now, for column major
[01:26:15] already I said that for row major we go
[01:26:17] from left to right, for column major we
[01:26:20] go from right to left.
[01:26:23] So, let us prepare the formula.
[01:26:26] L naught plus first one is index four.
[01:26:30] So, I four multiplied by
[01:26:34] See, this is dimension four. So, take
[01:26:36] all those dimensions before dimension
[01:26:38] four. So, D1 D2 D3. D1 into D2 into D3.
[01:26:45] Plus
[01:26:46] Now, this one. We are moving from right
[01:26:49] to left. So, next is I three, index
[01:26:53] three. So, go to dimension three and
[01:26:55] take all the dimensions before dimension
[01:26:57] three. So, this is D1 and D2. So,
[01:26:59] multiplied by D1 and D2.
[01:27:04] Plus
[01:27:05] I two.
[01:27:07] I two multiplied by
[01:27:10] Go to dimension D2 and take all the
[01:27:12] dimensions before that. So, that is D1.
[01:27:16] Plus
[01:27:17] This is I one multiplied by W.
[01:27:22] So, that's it. This is the approach for
[01:27:24] preparing the formula. Huh. Using four
[01:27:27] dimension I have shown you how to write
[01:27:29] row major formula and how to write
[01:27:31] column major formula.
[01:27:33] Now, using the same approach we can
[01:27:35] write the formula for any dimensions,
[01:27:38] any number of dimensions. So, even 2D
[01:27:40] was following the same approach, but
[01:27:42] this is very small problem. So, we have
[01:27:45] seen it thoroughly with the diagrams and
[01:27:46] everything, but here I am showing you
[01:27:49] directly the formula.
[01:27:51] Now, based on this formula, by observing
[01:27:53] this one, we can prepare a general form
[01:27:56] of this one for N dimensions.
[01:27:59] So, let us prepare a general formula for
[01:28:02] N dimensions for row major mapping. So,
[01:28:05] I am showing it for row major mapping.
[01:28:07] Let us take
[01:28:08] same formula, I will generalize it for N
[01:28:11] dimensions.
[01:28:12] n naught plus
[01:28:15] See, all these terms are added. See,
[01:28:17] this is one term. Then next this is the
[01:28:20] term. Next this is the term. Next this
[01:28:22] term. So, total four terms are there.
[01:28:24] Four terms are added. So, for addition
[01:28:27] we use sigma.
[01:28:29] And here i1, i2, i3, and i4 is used. So,
[01:28:34] i and its subscript that is i1, i2, i3.
[01:28:38] So, I will call it as p.
[01:28:41] So, p takes the values from 1 to It is
[01:28:44] going up to four. So, instead of four
[01:28:47] just make it n. So, this will be for n
[01:28:50] dimensions.
[01:28:52] So, p takes the values from 1 to n. So,
[01:28:54] for n dimension. So, this will become
[01:28:56] i1, i2, i3, i4, and so on up to n
[01:28:59] dimension.
[01:29:01] Next thing.
[01:29:02] Every index is multiplied by dimensions.
[01:29:05] This i2 is multiplied by d3 and d4. So,
[01:29:08] it is multiplied by product of
[01:29:10] dimensions. So, this is a product of
[01:29:12] dimensions. Product of dimensions. So,
[01:29:14] product of pi we can use.
[01:29:17] of d
[01:29:19] Now, these dimensions are taking
[01:29:21] subscripts from 2 3 4. So, this is just
[01:29:23] 3 4. So, whatever i is, this is 1. So,
[01:29:27] this is 2 3 4. So, this is taking the
[01:29:29] values after p. So, this is d q and q
[01:29:33] takes the values from p plus 1 to n.
[01:29:38] So, it is going from
[01:29:39] 2 to 4. 3 to 4. So, this is 2. So, 3 to
[01:29:43] 4. This is 3. So, 4 to 4.
[01:29:45] So, q takes the values from p plus 1 to
[01:29:48] 4. And this whole thing should be
[01:29:50] multiplied
[01:29:53] with w.
[01:29:56] So, this is the row major formula and
[01:30:00] general form of n dimension. So,
[01:30:03] similarly you can prepare a formula for
[01:30:07] column major mapping for n dimensions.
[01:30:10] So, try this formula by yourself.
[01:30:13] Now, let us do some analysis on this
[01:30:15] one. I'll remove this column major
[01:30:17] formula and we'll do analysis on row
[01:30:20] major formula. Let us see how many
[01:30:22] multiplications are performed for
[01:30:24] getting this result.
[01:30:27] Let us count them. 1 2 3, three
[01:30:29] multiplications here. 1 2, two
[01:30:31] multiplications here and one
[01:30:34] multiplication here. Then that W is
[01:30:37] common always, that is plus one.
[01:30:41] Depending on dimensions, the number of
[01:30:43] multiplications inside the brackets may
[01:30:45] vary.
[01:30:47] Now, for four dimensions, how many
[01:30:49] multiplications are there? This is 3 + 2
[01:30:52] + 1
[01:30:53] for 4D.
[01:30:55] It means for 5D, how many
[01:30:57] multiplications will be there? So, 4 + 3
[01:30:59] + 2 + 1.
[01:31:01] So, for n dimensions, how many
[01:31:03] multiplications will be there? n - 1 + n
[01:31:06] - 2 + goes on to 3 + 2 + 1. So, this is
[01:31:11] nothing but
[01:31:12] n into n - 1 by 2.
[01:31:16] So, n into n - 1 by 2 is
[01:31:20] n square multiplications.
[01:31:23] Oh,
[01:31:24] these are too many multiplications.
[01:31:27] So, the time taken for evaluation of
[01:31:29] this formula is n square multiplication
[01:31:33] operations.
[01:31:34] So, this is too much time consuming.
[01:31:37] So, the row major formula used by the
[01:31:39] compiler take order of n square time.
[01:31:42] So, is there any way to reduce the
[01:31:44] number of multiplications?
[01:31:46] Let us see what we can do for reducing
[01:31:49] the number of multiplications. I will
[01:31:51] take only the portion of formula that is
[01:31:53] inside the brackets and I will write the
[01:31:55] formula from right towards left. This is
[01:31:58] a row major formula. I'm just writing it
[01:32:00] reverse. So, this is I4 plus
[01:32:04] I3 into D4
[01:32:06] plus
[01:32:08] I2 into
[01:32:10] D3
[01:32:11] into D4
[01:32:14] plus
[01:32:15] I1
[01:32:17] into
[01:32:18] D2 into D3 into
[01:32:21] D4.
[01:32:23] So, I have taken only the portion of the
[01:32:24] formula that is inside the brackets and
[01:32:27] I have written from the last term
[01:32:29] onwards so that we can reduce the number
[01:32:32] of multiplications. So, if you observe,
[01:32:35] this is having this is having four
[01:32:37] values multiplied, three values
[01:32:39] multiplied, two values and then there is
[01:32:41] no multiplication. So, I have taken from
[01:32:43] the smaller value onwards. So, first is
[01:32:44] smaller then go on increasing.
[01:32:47] Now, in this if you observe, this I4 is
[01:32:50] as it is plus
[01:32:52] D4 D4 D4 D4 is common among all these
[01:32:56] terms. So, I can write D4 multiplied by
[01:33:00] D4 is common, so I3 plus
[01:33:03] I2 into D4 is taken as common, so D3 is
[01:33:07] there plus I1 D4 is taken as common, so
[01:33:11] this is D2 and
[01:33:13] D3.
[01:33:16] Okay, so I have taken common. So, I got
[01:33:18] one multiplication here. Let us continue
[01:33:20] and see if we can take more common
[01:33:22] values.
[01:33:25] I3 plus
[01:33:28] Now, in these two terms, D3 is common,
[01:33:31] so I will write down D3 multiplied by
[01:33:34] and I2
[01:33:37] plus
[01:33:38] I1 into D2.
[01:33:43] So, I have taken D4 common and D3
[01:33:45] common. Now, only two terms are
[01:33:47] remaining here. Further, I cannot take
[01:33:49] anything common.
[01:33:51] This formula can be rewritten like this
[01:33:54] by taking dimensions as common.
[01:33:57] Now, this is the same thing what is
[01:33:59] there inside the brackets. So, let us
[01:34:01] see how many multiplications are there.
[01:34:03] This is one,
[01:34:05] two multiplications,
[01:34:07] three multiplications. Oh, so for four
[01:34:10] dimensions, just there are three
[01:34:13] multiplications. For 4D, three
[01:34:16] multiplications are there. And for 5D,
[01:34:19] there will be four multiplications. So,
[01:34:20] for ND, there will be n minus one
[01:34:23] multiplication. So, total time for
[01:34:26] performing multiplications will be order
[01:34:29] of n. So, this formula reduces the
[01:34:32] number of multiplication from n squared
[01:34:34] to order of n. So, by taking common, I
[01:34:38] have reduced the number of
[01:34:39] multiplications. So, this is how we can
[01:34:42] reduce the number of multiplication in
[01:34:43] row major as well as in column major
[01:34:45] formula. So, the rule that we have
[01:34:47] applied, that is taking commons to
[01:34:49] reduce the number of multiplication,
[01:34:51] this rule is called as Horner's rule.
[01:34:56] So, we have applied Horner's rule to
[01:34:58] reduce the number of multiplications.
[01:35:01] So, that's all with four dimension to n
[01:35:05] dimensions. Now, let us prepare row
[01:35:07] major and column major formula for three
[01:35:09] dimensional array. We have already seen
[01:35:12] formulas for two dimension, then I have
[01:35:14] shown you a general method by taking
[01:35:16] four dimension. Now, using the same
[01:35:18] general method, we will prepare a row
[01:35:19] major formula and column major formula
[01:35:21] for three dimension array. So, here I
[01:35:24] have an example of three dimension array
[01:35:26] whose dimensions are L M N
[01:35:29] N. Then, let us prepare a row major
[01:35:32] formula for an
[01:35:34] element at index I J K. So, indices are
[01:35:38] I J and K. So, let us prepare a formula.
[01:35:42] L naught plus
[01:35:44] that is the base address. So, I'm not
[01:35:46] drawing a diagram showing you three
[01:35:47] dimensional array.
[01:35:49] As you You the approach for preparing
[01:35:51] the formula, same thing will follow.
[01:35:53] So, let us
[01:35:55] take indices. I
[01:35:58] This is for the first dimension. So,
[01:36:00] multiply remaining other dimension. So,
[01:36:02] I into M into N.
[01:36:06] Plus second dimension that is J
[01:36:11] into So, J is for second dimension. So,
[01:36:14] multiplied by third dimension that is N.
[01:36:18] Plus
[01:36:22] plus K
[01:36:24] And this was the last dimension, so
[01:36:26] multiplied by W. So, K is the last
[01:36:28] index. So, we have prepared a formula by
[01:36:31] moving from left to right.
[01:36:34] Now, similarly, let us prepare a formula
[01:36:36] for
[01:36:38] column major representation. So, for
[01:36:40] column major representation or column
[01:36:42] major mapping
[01:36:50] address of
[01:36:53] A of
[01:36:54] I J
[01:36:57] and K
[01:36:59] This is
[01:37:01] We will
[01:37:03] prepare the formula by moving from right
[01:37:05] to left. So, this is L0 plus first take
[01:37:09] K.
[01:37:10] K multiplied by K is for last dimension,
[01:37:13] so multiply with M and L. So, L M we
[01:37:16] will write. L into M.
[01:37:19] Plus next
[01:37:21] index is J. So, J multiplied by J is for
[01:37:25] second dimension, so take L.
[01:37:28] Then plus
[01:37:30] I
[01:37:32] I
[01:37:33] multiplied by W.
[01:37:36] That's all.
[01:37:37] This is a row major and column major.
[01:37:41] This is a column major formula for
[01:37:44] three dimension array.
[01:37:46] So, I have used the approach that I have
[01:37:48] shown you for n dimension and I have
[01:37:50] directly prepared the formula. Now, one
[01:37:53] of the thing that mostly confuses the
[01:37:54] student, that is see if I have
[01:37:58] two-dimensional array
[01:38:00] of dimension m n, then we say row major
[01:38:05] means first row then column. Column
[01:38:07] major means first column then row. Then
[01:38:09] if suppose we have a three dimensions,
[01:38:13] then what? Row major means what?
[01:38:17] So, already I have explained this one
[01:38:18] that row major means go from left to
[01:38:21] right and column major means go from
[01:38:23] right to left. Don't say first column
[01:38:25] then row because there are three
[01:38:26] dimensions. So, if this is row, this is
[01:38:28] column, then what is this?
[01:38:30] So, instead of giving a name, let us
[01:38:32] avoid giving any name to that one. Let
[01:38:34] us say for row major, left to right, for
[01:38:37] column major, right to left.
[01:38:40] So, that's all about three dimension
[01:38:42] formula.
[01:38:44] Let us look at the solutions for the
[01:38:46] questions in quiz two.
[01:38:48] So, there are four questions in quiz
[01:38:50] two. This is related to arrays. So,
[01:38:52] based on the topic I have given the
[01:38:54] question. So, let us see the solutions.
[01:38:56] So, the first question. So, first
[01:38:58] question says that if there is an array
[01:39:00] of type integers and the dimensions two
[01:39:03] dimensions array, so the dimensions are
[01:39:05] 1 to 10 and 1 to 15.
[01:39:07] And the base address of an array is 100
[01:39:10] and the size of integer is 1 byte.
[01:39:13] Word size is 1 byte. It is given in the
[01:39:15] question. So, I have written it like
[01:39:17] this.
[01:39:18] Then, we have to find out the address of
[01:39:21] any location i and j. What will be the
[01:39:23] formula? Because the formula is known
[01:39:25] formula and in that we already know
[01:39:28] L not, that is location zero and W. So,
[01:39:31] we can use that and still it will be a
[01:39:33] formula. We will not get a single value
[01:39:35] because we don't know i and j. So, what
[01:39:37] is the formula? See, formula is
[01:39:40] L not plus
[01:39:43] I minus 1 into
[01:39:46] N plus
[01:39:48] J minus 1. This. Why I minus 1 and J
[01:39:51] minus 1? Because the indices are
[01:39:53] starting from 1 onwards.
[01:39:55] Like in CC++ or Java, the indices starts
[01:39:58] from 0 onwards, but here it is starting
[01:40:00] from 1. So in old languages, it was
[01:40:02] allowed to start index from 1 also. So
[01:40:06] when when it is 1, so we have to write I
[01:40:08] minus 1 and also J minus 1. If it is 0,
[01:40:11] just we can take I.
[01:40:13] Now, what is N? So the dimensions are M
[01:40:16] cross N. So this is N, right? This is N.
[01:40:19] So now, let us put the values. L naught,
[01:40:22] we know 100.
[01:40:23] Right?
[01:40:25] Then I minus 1. Okay, I minus 1. And
[01:40:28] this is N, that is 15.
[01:40:31] This is 1 to 15, so it is 15 into 15.
[01:40:34] Then plus J minus 1. So J minus 1 will
[01:40:38] be as it is. So I'll keep it open. Now,
[01:40:40] how much this is? This is 100 plus
[01:40:45] 15 I
[01:40:47] minus 15
[01:40:49] plus J minus 1. So this is 15 I
[01:40:53] plus J
[01:40:55] and this one is 100 minus 15 and minus
[01:40:58] 1. So minus 16 if it is done, then it is
[01:41:00] 84.
[01:41:02] So this is the correct answer. 15 I plus
[01:41:05] J plus 84 is the correct answer. So this
[01:41:08] is the first question.
[01:41:10] Now, second question, this one. This is
[01:41:12] little difficult for the student. So
[01:41:14] I'll explain in detail. Watch this one.
[01:41:16] There is a two-dimensional array of
[01:41:18] dimensions 4 and 3, that is four rows
[01:41:21] and three columns. So here I have drawn
[01:41:23] it here. Four rows, 0 1 2 3, four rows
[01:41:26] and columns are 0 1 2.
[01:41:28] And it's already initialized with values
[01:41:30] 1 2 3 4 5 6 7 8 9 10 11 12. So 1 2 3 4 5
[01:41:34] 6, all these numbers are there. And the
[01:41:36] base address is given, that is 2,000.
[01:41:39] And this is integer type, and the size
[01:41:41] of a integer is four. So, this is given
[01:41:44] in the question.
[01:41:45] So, already I have drawn in the diagram
[01:41:47] here, and I have filled the values.
[01:41:50] Now,
[01:41:51] let us go towards the solution. Base
[01:41:53] address is 2,000, and integer is taking
[01:41:56] four bytes. So, first address is 2,000,
[01:41:59] so I wrote zero for that. And next is
[01:42:01] 2,004, 2,008,
[01:42:03] 2,000
[01:42:05] 12. Plus four, right? Plus four is 16,
[01:42:08] plus four is 20. So, I have written the
[01:42:10] addresses of all these locations. So,
[01:42:12] you know very well that in the section
[01:42:14] we have studied that two-dimensional
[01:42:16] array means actually the memory will be
[01:42:17] allocated in single dimension form.
[01:42:20] So, the addresses start from 2,000, so
[01:42:22] this is 2,004 because integer is four,
[01:42:24] and eight, the next is this is 2,012.
[01:42:27] So, likewise, this is 2,036, 40, and 44.
[01:42:32] So, these are the addresses. Now, the
[01:42:34] next important thing we should know for
[01:42:36] getting the solution is how to access
[01:42:38] the elements using pointer arithmetic.
[01:42:42] Suppose I want to get the value eight.
[01:42:44] How to get the value eight? See, eight
[01:42:47] is row number two and column number one.
[01:42:49] So, how to get that one?
[01:42:51] X plus row number two.
[01:42:54] X plus two. Then here, star.
[01:42:58] Okay?
[01:42:59] One star. Then, column number is one,
[01:43:03] plus one. Then this whole thing should
[01:43:05] be inside star.
[01:43:07] This is how we can access
[01:43:09] any location or any data inside any
[01:43:12] location at any given location. So, two
[01:43:14] comma one I am accessing. If you want to
[01:43:17] access one comma two, then write this as
[01:43:19] one and this as two.
[01:43:21] So, important thing in this one is we
[01:43:23] need two stars for accessing data.
[01:43:27] Accessing data.
[01:43:28] If I remove one star, then what I get?
[01:43:32] I get address of the data. Address of
[01:43:35] the data. I don't get the data, I get
[01:43:38] the address of So, what is the address
[01:43:39] of eight? It is 2028. So, this is 2028.
[01:43:44] So, your starting is 2000, so I have not
[01:43:46] written 2028, just 28 I wrote. It's
[01:43:48] 2028.
[01:43:50] Next, in this, if I remove this plus
[01:43:53] one, if I remove this plus one, so it
[01:43:56] means it will not go to this row one,
[01:43:58] then whose address I get? I get the
[01:44:00] beginning address of this second row. X
[01:44:03] plus two means this is second row, X
[01:44:05] plus two. So, first plus is for row and
[01:44:08] second plus was for column, right? So,
[01:44:11] if I say X plus two, I'll get the base
[01:44:12] at beginning address of the second row,
[01:44:14] that is 2024.
[01:44:16] This is what I get.
[01:44:18] And one more thing, if you remove this
[01:44:20] is star also, then also it means the
[01:44:24] beginning address of the second row,
[01:44:26] that is 2024. It means same, whether you
[01:44:29] write star or don't write star, because
[01:44:31] you will not be getting the value. So,
[01:44:33] definitely it's address. For value, you
[01:44:35] should have two stars, remember this.
[01:44:38] Now, we are able to get the solution.
[01:44:40] So, what is asked here? First thing is,
[01:44:42] what is X plus three?
[01:44:44] Percentile U is used there for printing
[01:44:47] the address. So, what is X plus three?
[01:44:49] X plus three means third row, beginning
[01:44:52] address of third row. What is this? 36.
[01:44:55] So, 2036. So, this is 2036.
[01:44:59] Right? Next,
[01:45:00] if I write star, I'll not get the data.
[01:45:03] It is X plus three, third row only, same
[01:45:06] address only I'll get. So, what is the
[01:45:07] address of the third row beginning
[01:45:09] address? That is 2036, 2036 only.
[01:45:12] Same thing.
[01:45:14] Now, next, star X plus two, so beginning
[01:45:17] address of the second row.
[01:45:19] Means this one, 2024.
[01:45:22] Plus three, plus three means one,
[01:45:25] two, three.
[01:45:27] It's again same address.
[01:45:29] This is also 2036. Again, this is also
[01:45:33] 2036.
[01:45:35] X + 2 star means this address, 2024,
[01:45:39] right? Then + 3, 1 2 3, so 2036.
[01:45:44] That's it. So, the answer for this 2036,
[01:45:47] 2036, and 2036. Three times it is given.
[01:45:51] So, this is the answer for this
[01:45:52] question.
[01:45:54] Now, the next question is third
[01:45:56] question.
[01:45:57] Here, the question is there are two
[01:46:00] matrices, A and B.
[01:46:02] Matrices can be represented either using
[01:46:05] row major mapping or column major
[01:46:07] mapping, right? In by a compiler. By a
[01:46:11] compiler, they can be represented in any
[01:46:13] way.
[01:46:14] But, C language and C++ compiler follows
[01:46:17] row major mapping, right? We have
[01:46:18] already discussed this one. Now, coming
[01:46:20] to the question.
[01:46:22] It says that if A and B are being
[01:46:24] multiplied, we have to perform such an
[01:46:26] operation.
[01:46:28] Then, if your compiler you are writing a
[01:46:30] compiler, so in your compiler for such a
[01:46:33] situation, what do you prefer?
[01:46:35] If A and B are multiplied. So, matrix A
[01:46:38] multiplied by B means if you have two
[01:46:40] matrices, right? So, if you know the
[01:46:43] matrix multiplication, rows are
[01:46:45] multiplied with columns, right? Elements
[01:46:47] in a column. Row with column, we get the
[01:46:50] product of two matrices. Row with
[01:46:52] columns, right? We multiply and get each
[01:46:54] element. So, you know the matrix
[01:46:55] multiplication procedure.
[01:46:57] So, you should know the procedure.
[01:46:59] Now, the question is if I'm multiplying
[01:47:01] these two, then which mapping is better?
[01:47:04] So, the options are given that first one
[01:47:06] is row major, second one is column.
[01:47:09] Because rows are multiplied with
[01:47:10] columns, so the first idea that you will
[01:47:13] be getting is first one A should be row
[01:47:15] major and B should be column major. That
[01:47:18] is the wrong answer.
[01:47:20] The right answer is any representation
[01:47:22] can be used. Reason, which
[01:47:24] representation is better? So, if you see
[01:47:27] the row major formula, I have just
[01:47:28] written the formula here. Column major
[01:47:30] formula, how many operations are there?
[01:47:33] Two additions, one multiplication. Two
[01:47:35] additions, one multiplication. Both are
[01:47:37] equally efficient. So, efficiency is
[01:47:39] based on the operation. So, how many
[01:47:41] operations do you have to perform to
[01:47:42] calculate this formula and for this one?
[01:47:44] They are same.
[01:47:46] So, both are equally efficient. Right?
[01:47:49] So, the answer is it's independent or
[01:47:50] both are equally efficient. You can use
[01:47:52] anyone.
[01:47:54] Right? Not specific. First one is row
[01:47:55] major or second one is column. Both can
[01:47:57] be row major. Both can be column major.
[01:47:59] Any combination. Four combinations are
[01:48:01] possible. Anyone can be taken. So, it's
[01:48:03] independent of any representation.
[01:48:05] Right? Now, you may be thinking that if
[01:48:08] n value is small, then the number of
[01:48:09] multiplications will be more. So, why
[01:48:11] less? Then, I will say m value may be
[01:48:14] small also. So, it's not based on value.
[01:48:16] It's based on just number of operations.
[01:48:18] So, both are equally efficient. So, you
[01:48:21] can use any method. Independent of
[01:48:23] representation is the right answer. Now,
[01:48:25] the next question is fourth question.
[01:48:28] So, this is little lengthy. I'll remove
[01:48:30] this. And I will do the work for this
[01:48:32] one.
[01:48:34] The question is there is a
[01:48:36] three-dimensional array. Name of this
[01:48:37] array is X. And there are three
[01:48:40] dimensions, L, M, N. We don't know what
[01:48:43] are the dimensions.
[01:48:44] We don't know the data type also.
[01:48:48] And if the size of an integer is 2 bytes
[01:48:51] and the size of float is 4 bytes. This
[01:48:53] is given in the question.
[01:48:55] And the question says that
[01:48:57] for evaluating the address for a
[01:49:00] three-dimensional array, compiler is
[01:49:02] performing following steps.
[01:49:05] And these are the steps given.
[01:49:07] Then, from these steps, can you find out
[01:49:10] what are the dimensions and what is the
[01:49:12] data type? This is the question.
[01:49:15] So, when the compiler is performing the
[01:49:17] operations,
[01:49:18] it is using row major representation.
[01:49:20] Okay, row major representation is
[01:49:22] followed. And these are the intermediate
[01:49:24] steps.
[01:49:25] So, now we have to find out what are the
[01:49:27] values of these dimensions and the data
[01:49:30] type.
[01:49:31] So, for the solution I have taken it
[01:49:33] here.
[01:49:35] Data type we don't know. Dimensions are
[01:49:37] L M N, right? Three dimensions, 1 2 3.
[01:49:41] Then, what is the row major formula for
[01:49:44] three-dimensional array? Address of X of
[01:49:47] I J K is L naught plus I into MN plus J
[01:49:52] into N plus K.
[01:49:53] And this entire thing multiplied by W.
[01:49:56] This is the formula.
[01:49:58] And in this, one thing to observe is
[01:50:02] we have MN,
[01:50:04] right? MN. I J K is there and W is
[01:50:07] there. So, we don't have L. So, yes, we
[01:50:10] cannot find out L. We cannot find it
[01:50:12] out. But, we can find out MN and W.
[01:50:15] These are the values that are variables
[01:50:17] that are used here, right?
[01:50:20] Then, if I expand this formula, it will
[01:50:22] be like this.
[01:50:24] Now, if you reach till here, then you
[01:50:26] know the answer from here.
[01:50:28] Let us see. This is the formula. This
[01:50:30] complete thing multiplied by W, so I
[01:50:31] multiplied by uh every term with a W.
[01:50:35] So, I have multiplied every term with W.
[01:50:37] Now, see this.
[01:50:39] Compiler will not execute the entire
[01:50:42] formula at once.
[01:50:44] It has to do step by step, like
[01:50:47] multiplication is done, then next is
[01:50:48] done, then next is done, then addition
[01:50:50] is done. So, each operation, it takes
[01:50:53] one machine instruction. So,
[01:50:54] step-by-step it is doing. So, that's
[01:50:56] what it is shown here. Steps are there.
[01:50:59] This entire formula cannot be done. If I
[01:51:01] give you pen and paper and I give you
[01:51:03] values and ask you to solve it, you
[01:51:05] cannot do it at once. For example, if I
[01:51:07] say
[01:51:08] 3 into 42 plus 9 into 73. Just do it.
[01:51:14] So, in a single glance, can you give the
[01:51:16] answer? No, first you'll multiply this,
[01:51:18] then multiply this, then add it. So, the
[01:51:20] same way compiler is also performing
[01:51:23] them a step by step.
[01:51:25] Now, solution. In the formula,
[01:51:27] what is multiplied with K? K into W. K
[01:51:31] into W. So, here in the steps, K into 4
[01:51:35] is there. So, K into 4 is there means
[01:51:38] what is the value of a W? 4. So, W is 4.
[01:51:44] That's it. So, it's so simple. We know
[01:51:46] the value of
[01:51:48] W now. Then, if you see this,
[01:51:51] I started with the smaller one, not this
[01:51:53] one. This is having three things multi-
[01:51:55] four things multiplied. So, I took the
[01:51:57] the simple term. Then, the J N W. So, J
[01:52:00] is multiplied by N into W. So, what is
[01:52:03] the value of the J? 32.
[01:52:06] So, that is 32 means it is actually N
[01:52:08] into W. So,
[01:52:10] N into W is 32. Then, what is N? N is
[01:52:16] 32 by 4, that is 8. So, now we know N is
[01:52:21] 8.
[01:52:24] Then, the last thing, I is multiplied by
[01:52:27] M into N into W. So, here it is 1024.
[01:52:33] So, this is M into N into W. So, M into
[01:52:37] N into W, this is 1024. 1024.
[01:52:43] N into W already we know that was 32.
[01:52:45] So, this is M is equals to 1024 by 32.
[01:52:50] So, if you divide this, this is 32. That
[01:52:53] is 32 into 32 is 1024. So, we know the
[01:52:56] value of M also, that is 32.
[01:53:00] So, now we know the values. W is 4. Four
[01:53:03] means this is data type. W is data type
[01:53:05] float. So, this is float. Data type is
[01:53:08] float.
[01:53:10] Right? And X, as I said, the first
[01:53:12] dimension we cannot know, right? Then,
[01:53:16] what is M? M is 32. M is 32. And N is
[01:53:20] eight. That's it. So, whatever it may
[01:53:23] be. So, if you check the options, among
[01:53:26] the options, only one option is having
[01:53:28] float here and 32 and eight here.
[01:53:31] So, whatever it is, 12 is written here,
[01:53:34] 12 or anything. Whatever the value may
[01:53:36] be, it will be true because this one and
[01:53:38] this one and this one are same. So,
[01:53:41] there is only one option that is true.
[01:53:44] So, that's all. These are the solution
[01:53:45] for
[01:53:47] questions in quiz two.
