C Array - Part 3 | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is in verse in this session we'll discuss how to insert an element into array right how to insert a single element and how to insert all the elements of one array into another array so these two discussions we are declaring one array int ARR so what is the size see here it is a static memory allocation only a fixed memory allocation right if you want to increase or decrease the size of array we should go for a dynamic memory allocation concept that we will see soon but here it is array so first declaration is a 50 so here we are using 50 right is it necessary to store all the 50 elements into array no not required we just reserve only 50 memory locations so once again we are asking the end-user how many elements you want to work with once again we are asking the size but maximum number of elements so we can take into this array is called a 50 so here now to read the size I am declaring one more variable en so we are asking printf enter sizeof array enter sizeof array but remember the maximum number of elements that that is nothing but the maximum size we can mention that is a 50 because the reserved space is a 50 only suppose end user input is a 5 so we are working with only 5 elements so remaining 45 memory locations so we cannot do anything waste of memory as of now the problem we can resolve in a dynamic memory allocation concept that we'll see later okay so we are reading scanf % is d into n we are reading into n and next we are asking enter those many elements into array how many elements depends on the size the end user inputs enter suppose n value is a 5 5 elements enter 5 elements here we are writing en in place of percentage D the N value will go and print so enter 5 elements suppose if end user input is a 7 enter 7 elements like that so how to read n elements into array already we discussed processing array elements is possible only with the help of iterators nothing but loops we are repeating loop I value starts with 0 so declare the I value also I is less than n not 50 very very important if you write 50 here 50 times it will repeat and 50 elements you should input that is not possible only n elements less than n I plus place we are reading scanf every time one element we are reading into address of ARR of I so reading is over and next we are asking enter element to be inserted here printf enter element to insert new element right then they will enter the new element to insert that in it is specified a memory location nothing but it is specified array location the scanf percent is d that we are collecting into ki that we are collecting into ki so key variable also i am declaring key element so here it is we already inserted that what element we want to insert now where we have to insert the location also you should specify so next one we are asking printf enter location to insert so that we are collecting scanf percentage be in to address of location so we are collecting into a variable LOC so first we are reading elements into array and next we are reading enter the new element to insert and next enter a location to insert so what we have to insert where we have to insert into which array we are inserting all the three so reading completed now a concentrate on logic how to write the logic suppose this is array n value consider six so index is a 0 to 5 0 to 5 and here it is array is pointing to this location base address of course we know that so now just consider the key element is a a new element that we want to insert is 100 and location is at to consider the element a new element that we want to insert is 100 and location is a 2 sir what are the elements of array any elements you can give you can input for example elements are 10 20 30 40 50 60 now the new element 100 I want to insert into this location to sir into the location 2 already right 1 element is there that is a 30 so directly we cannot insert because if we insert the value directly rights we lost the value 30 so we should shift all the elements so here you should shift all the elements by one location by one location we should shift all these elements 60 we should move to here from fifth location to sixth location 50 we should move to here 42 here 32 here and 31 copy will be present up to here only right so that is nothing but 5 means n minus 1 from n minus 1 to 2nd is nothing but a location what are the elements we should shift right all the elements from n minus 1 index number 2 location index number number of elements will be present so we are using 1 for loop I value starts with the N minus 1 I is greater than or equals 2 because a decreasing order 5 4 3 2 greater than or equals to location I - - I - - so here first I value starts with a n minus 1 n minus 1 is a 5 5 is greater than or equals to what is the location - location is at - condition true the control move inside fifth location element we should move to 6th location 5 means what I so I slow keishon element ARR of I value will be stored in to 6th location so nothing but Y ARR of I plus 1 ARR of I plus 1 so we are shifting right fifth location element into I plus 1 means 4 5 plus 1 6 so it will move in the next iteration I - - I value become 4s condition true fourth location element will move to 5th location next I value decrease it will become 3 third location element will move to 4th location so next I value to a second location element will move to third location next I will you become 1 1 is greater than or equals to 2 condition false so then for loop will terminate so shifting completed next the element key element the key element key means what is 100 we are inserting into this location so that 30 will be replaced with 100 how can we store LOC location is nothing but at 2 it is a second location so that key element we are storing into ARR of elbow c-loc is a 2 right into ARR of 2 into the second location we are storing 100 so that 100 will go on store into this location so this is this is element insertion into array 2 things we have to perform using 1 for loop first we need to shift all the elements from last location that is nothing but from n minus 1 to a location right that is input location once shifting has been completed using a for loop so directly we can insert the value key value into ARR of location this is inserting an element into array okay so now we'll discuss how to insert all the elements of one array into another array you see how to insert all the elements of one array into another array right see so now first we are taking one array so remember now we have to read two arrays this is first array just consider the size of first array we are reading into variable m and the size is a six consider index starts with a zero one two three four five index and elements 10 20 30 40 50 60 and here it is a arrayname is a a so that it is pointing to this block of course by holding the address so first array 1 is ready now I want to read second array elements this is a second array this consider second array size is a three only you can take any size but of course first you have to fix the size either 100 200 300 any size you can take right here it is we are taking only 3 so that index is a 0 2 less than 3 that is 2 and here it is array variable is a B name of array is B right what are the elements now we are asking right insert the elements into array so the input for example 100 200 300 100 200 and 300 now so we need to ask where we have to insert the elements of one array so we need to insert into another array from which location right the location we need to ask here it is printf enter location to insert location to insert we are just reading that location location suppose a location is a to consider from the second location I want to insert how to insert means in the previous example we are inserting only one element so all the elements we shifting by one location only but now we have to insert n number of elements not three do not use a hard coding try to write the logics using variables only so that that will work anywhere okay so here it is we need to insert n elements into array a so every element so must be shifted by n number of locations n number of location see now the array size will grow like this array size 6 7 8 locations and remaining because n value is a 3 so every element is shifted by three locations sixty-fifth location element will come to five plus three nothing but phi plus n 8 so 60 will come to here 50 40 all the three elements and 30 will move to here and remaining up to location only from M minus 1 to M value 6 6 minus 1 5 2 location from M minus 1 2 location all the elements we should shift rights by n locations n value is a 3 and remaining same 50 40 30 so how to shift the elements very simple so take 1 loop i value starts with M minus 1 I is greater than or equals to location I - - same previous logic only now here it is a of I a of I because I value starts with M minus 1 M value is a 5 right min right M value is a 6 6 minus 1 is a 5 so I value is a 5 so fifth location element will be stored into I plus how many locations you have to shift n locations you have to shift them so I plus en I plus L so first one five is greater than or equals to two so a of five will be stored into a of five plus 3/8 location shifted next I - - same story I value become four fourth location element 4 plus 3 7 next I value 3 3 plus 3 6 next I value 2 2 plus 3 5 next whenever I value 1 1 is greater than or equals to 2 condition false so shifting completed now insertions are how to insert we are not inserting a single element again we are inserting an array suppose here it is 3 is there okay suppose 300 is there 300 times 300 statements we can't write so use the for loop once again here one more for loop how many elements I value starts with 0 to n number of elements we should insert n value is a 3 so n elements 0 1 2 n elements we should insert so first I value 0 0 is less than 3 condition true condition true how to insert B of 0 nothing but B of I I value starts with a 0 B of 0 location element nothing but 100 so we are storing into this one this is nothing but a location location plus I value because I value 0 right location is a 2 2 plus 0 is it - only next location plus 1 location plus 1 I means what I value 1 location value 2 2 plus 1 is a 3 third location next one here it is a location plus I is nothing but for because I value is a to location value Z - 2 + 2 4 so here so we are writing into a of location plus I B of 0 value will be stored into a of location is a 2 2 plus 0 is a 2 only so that element 100 will be inserted here next I value increases I value 1 1 plus location location value 2 1 + 2 3 so 200 will be stored into this location next 300 will be stored into this location whenever I value 3 3 is less than 3 condition false this is insertion so we need to take two for loops to insert 1 array all the elements of 1 array into another array right what is the use of a first for loop means to shift all the elements of a first array and what is the use of a second for loop means right insertion of all the elements of second array into first array okay so this is inserting elements into array either single element or a set of elements hope you will enjoy this video right for more videos please subscribe to nourish id channel thank you all you
Info
Channel: Naresh i Technologies
Views: 506,092
Rating: 4.8922491 out of 5
Keywords: C Language, Srinivas, Naresh IT, Hands on C Language Training, C Language Demo, Arrays in C, Online C Language Training, C Language Tutorial Videos, C Language Overview, C Language Interview Questions
Id: tCYlG065CxM
Channel Id: undefined
Length: 19min 34sec (1174 seconds)
Published: Mon Sep 19 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.