C_54 Two Dimensional(2D) Arrays in C | Initialization of 2D Arrays

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in this series of learning programming in c we are discussing 2d arrays in c in the previous video we have discussed need of 2d array means y2d array and declaration of 2dl now in this video i will talk about initialization of 2d array two types of initialization at runtime and at compile time both you will see with the help of some examples right before that just want to ask one question when it comes to interview preparation for software development roles at top product companies do you know what topics are crucial to be prepared for let me tell you data first of all data structure and algorithm because they want problem solver second is system design and back-end development because they want design oriented thinkers so the good news for you is what an academy is conducting a free event called programmers carnival this weekend which is relevant for everyone who wants to prepare for product company interviews on saturday focus will be on data structure and algorithm and there will be a session on sorting and searching for interview preparation introduction to basic dsa game theory and greedy algorithms and sunday focus will be on development and there will be sessions on introduction to system design micro service architecture and introduction to back-end and as usual the classes would be taken up by the best educators at an again so i don't think there could be a better way to add values to your interview preparation or your programming journey at free of cost so make this weekend a super productive one and register for this event the link for the registration i'll put in the description box of this video you can go and check out so now back to the topic initialization at runtime and at compile time first we will discuss at compile time how to initialize a 2d array at compile time right syntax is almost similar to initialization of a 1d array right a little bit different so now see at compile time means at the time of declaration only you will give the values so how to declare a 2d array data type the name of the array size first of all row size suppose i am taking 2 and then column size 3 columns and 2 rows i want now hair only in these braces give the values right suppose i am giving here values what 0 0 0 1 1 1 right so see general syntax if you write then what what would be the syntax data type name of array in first bracket row size second bracket column size equal to in these braces you can you will write list of values values or you can say values you want to give that is the general syntax i hope no need to write that sentence that syntax here so now how many elements total elements will be there in the studio array how to calculate row size into column size that that is three into ah two six six elements total element would be six here right how many bytes would be allocated 6 into for 1 4 bytes integer will take 4 bytes so 24 bytes would be allocated here now i have given value something like this now how to visualize this 2d array how we see that data would be stored in memory see here like this so this is it two rows it's row one and row sorry row zero and one index would be started from zero column would be zero one two from zero to two three columns are there in every row so you can say it's a two d array which is array of one d arrays this is what one one one d array this is one one d array right that's it so now how this data would be initialized here first of all in first row data would be initialized data would be a written like first of all this one 0 in this first row then again 0 again 0 once we have filled all the columns then 1 then 1 then 1 this is how data is to be initialized right so like this you can give second alternative is is what better to give something like this because we know first one is this is first row so what you can do these three you can put again in this braces right this is you can say first row and for second row again you can put one more braces and one comma one comma 1 and that's it within these braces this is one row and this is second row this is i guess more understandable so you can say this is row 0 and row 1 index would be 0 and 1 right so directly now we know that by by looking at this point we know this is one row zero zero zero would be initialized here and this is second row so this is also correct you can write something like this or you can write something like this in a and 2 3 in a matrix form in this 0 0 0 comma 1 1 1 and semicolon this is also correct in the next line you can write this for the row 1 in next line you can write for row 2 and here you can close this this this braces so this is also correct you can initialize it in matrix home this also would be initialized this also would be in memory would be like how we see that would be written something like this in 2d array this is first row this is second row right now another method is how you can initialize here what you can do here you can you know skip this one if you don't give the size the row size then also if you give value something like this in braces like this is for first row this is for second row right you want two rows but you don't initialize here you don't give the size here that is fine it will not give any error because you are initializing here so zero zero zero that would be stored here one one one that would be stored in row two ultimately by default what two rows would be there and obviously column size you have declared here so i guess you got that the initialization is done row by row right row by row from here first row then second row only right so now when you can when it is not compulsory to give the size of this row size when you have given you have initialized all the values explicitly in 2d array then it's not compulsory to give row size means here see what you want you know the size of row would be 2 so 2 rows and three columns and you have initialized you have given all the six values here all the six values here in that case no need to give this row size if you write something like this and here also if you don't write this that is also fine it will work because you have initialized all the six values right but if you write something like this see i'm writing here into a i'm not giving any size 3 and i'm just writing here 0 ok first of all let me you know give you another example if you write 2 and 3 and 1 0 only in that case what will happen all the values would be initialized with 0 right or before that one more thing if i write something like this 0 0 and 0 and 1 or you can say 0 0 1 1 only 4 values i have given and size is 2 and 3 so now how that would be initialized initialization would be done row by row so first value zero second zero next one also here now first row is complete now one is there and remaining would be initialized with by default zero this is how the initialization initialization is to be done but if you want that 0 0 would be in first row 1 1 would be in second row then you have to specify explicitly that this is row 1 row 0 i mean first row this is second row so what you can do you can put just curly braces here and here and then in curly braces one and one now whatever you have written within these curly braces either one value or two value that would be considered as row one means here this row zero row zero throw and this would be considered as one throw so now initialization would be done something like this zero zero would be here and by default there is no nothing so one more zero by default and this is row second so one one here and by default here would be zero right so this is how we can do different different types of initialization now what what i want to tell you one more thing see and i have told you if 0 is there then all would be 0 first is 0 remaining would be 0 right but if you will not initialize size here then what will happen now it will not give any error see it will not give any error but you know here initialization is what because you have given only one zero half the size is to be calculated so when it will initialize like we know three columns would be there so zero zero zero one zero by default to zero now we don't know second row is there or not right so that would be only one row there would be no second row so it's like 1d array only this does this and if you print if you write down the logic to print a 2d array like from that that we will discuss here and if you will you want to print this second roll so then it will give any garbage value because you haven't initialized this thing right so better to if you note do not give the size here and if you initialize something like this this is assignment for you see if i write here like this 1 1 1 and that's it now how data is to be initialized here right in this form 2d array it would be 2d array or 1d array right and if you write second is what if you write here 1 1 1 and 2. then these are the two uh example i mean two assignments for for you you have to tell me in comment box right how data is to be initialized in these two cases right now uh next point about initialization at compile time is what if you write here something like this size is 2 and if i write here like 0 0 0 you have put this this one in three values into into these curly braces so that would be considered as this row first row so 0 0 would be assigned here now next three rows would be assigned with what i mean and next row the next row that one one through would be assigned with zero zero zero remaining would be zero zero zero right so you can try this this type of declaration all the declaration that i have told you on your laptop and you can you know see if you are getting any error or not now second is what at runtime how you will initialize how you can initialize a one day rate runtime using for loop and scanf same here we will use for loop and scan a function but here two for loop but hey because we are having here one like suppose i am taking same hint a two and three right so this is row 0 index is 0 1 0 1 2 right now how to initialize this thing so first one you have to take here two loops nested loop would be required and this thing i have discussed why nested loop when we were discussing printing pattern programs in c so there we have discussed i have uploaded one video like what is need of nested loops and what are nested loops you can first of all watch that video then come to this video i'll put the link of that video in the side button you can check it out so now first loop would be to print what rows right so rows are 1 and 2 2 rows only so i would be from 0 to 1 because index is starting from 0 to 1 and i plus plus now within this loop the second loop would be j and that is responsible to print these values so within every row we have we are having three values so that this loop would be would run for three three times zero less than three and j plus plus so this is for to print row like two rows are there so zero to one zero to ah you know zero to two sorry right and here this is for printing these columns so zero to three sizes three and within this we are going to take input so printf percentage d address of name of error is a and here you will give 2 subscript because this is what 0 0 right this block is what row is a 0 column is 1 0 2 1 0 1 1 1 2 so i and j you will have to give two values row value also and column value also then only then only it is you know possible to access these blocks or these cells and that's it this is what you can initialize at runtime right so at first i value is zero condition true enter here then j value is zero condition true enter here scanf percentage the address of a of i value zero j value zero address of a of zero zero zero means this one so whatever you will give that would be assigned to this block like i am giving one now first of all j plus plus because you are in this inner loop now j plus plus j becomes one condition true enter here again now a of i i is 0 still but j is 1 so now i 0 1 this cell you can access so now whatever you will print you will enter that value would be stored here because we are getting address of this cell so suppose i am entering one again so one would be stored here again we are still into in this inner loop so j plus plus that is two again condition true enter address of a of i is still 0 but j is 2 0 2 0 2 this cell so whatever you will enter that would be stored here that is suppose i am entering 2 right now j plus plus j becomes 3 now condition 2 3 less than 3 no exit from the inner loop but still we are in outer loop that is i plus plus now i becomes 1 condition true again enter here again j would be initialized with 0 and same j would be repeated 3 times for this row right and once i value becomes then two condition not to exit from the loop and this is how we will initialize at runtime this is how this is working with nested loops right so now i hope you are clear with the run time and compile time initialization of 2d array so in the next video we will discuss what uh memory representation of 2d array as well as how to access elements of 2d array and then that in that video only i'll show you all the things like on my laptop practically we'll take one example and practically i'll see i'll show you the declaration part and initialization part as well as how to print an array and how to take this this how to take values or how to initialize our array at runtime right so i'll see in the next video till then bye take care
Info
Channel: Jenny's Lectures CS IT
Views: 149,539
Rating: undefined out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures, two dimensional arrays in c, arrays in c programming, memory representation of two dimensional arrays in c, what is array, c programming tutorials for beginners, data types in c, gate cs lectures, ugc net computer science preparation material
Id: FMjWTzPSZKA
Channel Id: undefined
Length: 17min 26sec (1046 seconds)
Published: Fri Jul 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.