C-49 Arrays in C - Part 4 | Array Program 1 | C Programming Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the series of learning programming in c we are discussing arrays in c we have discussed all the basics about arrays like what is array declaration of array initialization of array memory representation of array elements as well as how to access array elements so i have just written down all the points in brief about arrays here you can take a screenshot or you can it's like a notes for you these contents you can say these are characters characteristics of arrays or you can say some here we have some advantages of array as well as drawbacks of using arrays right like i i think no need to explain all the points here because these are very simple i have explained clearly all the points from here only you can find out the advantages and drawbacks like advantages is what if you want to store more than one data item then you can use arrays right it's simple to use arrays but drawback is what it contains only it can store only same type of data item like if a teaser is array then all the data items should be of integer type if character arrays of character type then all the data items can be of character type all the elements are stored in contiguous memory allocation intake should be started from zero one drawback is also there like what if sizes declared you have declared the size of the array then you cannot change it so it is having fixed size problem accessing of array of element is faster using index of array how we excess just write down the name of the array and here in bracket index like index always start from zero so first element if you want to excess then a of zero so it is faster random access is possible directly you can access any element of the array in constant time so this is what advantage is offering here it can store data in multi-dimensional form in 2d array and multi-dimensional area through the area also right so this is advantage but inserting and deleting of data items in an array is tough it's costly right how that we will discuss with the help of program this is you can say it's a drawback of array here the one point is what no bound checking in arrays in c now what does this mean this is what it's a drawback suppose you have declared as area of size 10 so index would be from 0 to 9 but if you want to access a of 10 then it should give error right because there is no index of 10 but it will give garbage value so no bound checking bound boundary is what 0 to 9 but it is not checking the boundary in c it will not give any error it will give what any garbage pin we will see practical also of this point no bound checking in c so i guess this is clear now i will discuss one or two programs on arrays right but before they just want to tell you one thing if you have just started your programming journey or if you are looking for dsa data structure and algorithm or system design or full stack development courses then on an academy you can get access to all the courses as well as free classes on all these topics here you will get access to many free classes live classes every day where many topics are covered which are very important for interview preparation for software development roles in the top product based companies and as a subscriber you will get access to all the batches and all the free classes which are taken up by the best educators from the top product based companies or you can say are among the best competitive programmers in the country plus they will also conduct a dedicated doubt solving session after every three classes in a course and also a separate doubt support you will get on discord where teaching assistant will help you in clearing your doubts and most importantly here you will get the courses in a structured format so this structured learning path with a course plan designed specifically to enhance your skills and to help you create your interviews so all the relevant batch details as well as the link for the free classes i'll put in the description box of this video you can go and check out use my code jkl10 to access free classes as well as to get 10 percent discount on your paid subscription so you must check it out so now let us discuss some problems on aries first of all i'll discuss the logic the problem here on the whiteboard then i'll show you on my laptop right first we will discuss a simple problem like how to read data in an array at runtime although we have discussed it but next thing is how to print all the elements so fairly first of all how to print simply print then we will discuss how to print in reverse order and then we will see some complex problem so see so i have already discussed like how to initialize array at runtime i guess you can already write down the header files and hold i'm not writing the complete program i'm just going to start from the main function right and here what i am declaring array simple hey of size five i am not initializing this at compile time you can also initialize it at com compile time i'll take input from the user right so now for that what one for loop you will use i is equal to 0 and i less than number of elements are 5 and i plus plus before using the size this is what of type integer so we will take what a variable i here will declare a variable i here and how to take input using scanf how to read data using scanner function we know percentage d address of what name of the array is a and on ith location we want to store means i would be from zero to five so all the five location in the all the five location we will store using this thing right now see uh before printing just i'm writing what when this would be compiled then an array how many bytes should be allocated if you take two bytes then ten bytes should be allocated the size of integer if you take four bytes in some compiler it takes four bytes then twenty bytes will be allocated right so now suppose address is hundred i'm taking four bytes hundred and four then eight 12 and for this 16 right and in text would be from 0 1 2 3 and 4 and if you we haven't initialized it then it is containing some garbage as well if you print now then some garbage value would be printed right and one variable i suppose the this memory address for this is 2000 right and this also having some garbage value right now right now when compiler will come here 4i is equal to 0 i would be initialized with the 0 condition true enter scanf percentage d where the data would be stored address of a of i i value is 0 first of all so address of a 5 is what 100 right this address base address is 100 and where it is going to be stored this a is what it will act as a internal pointer it is going to store 100 here and the address of this a also some address like you can uh take thousand any address address would be hexadecimal form but just to make you understand i am taking simple these addresses right and this is what you can say pointer to the first variable of this array right address of a of i a of i zero a of zero address is hundred at this address first data would be stored whatever you will enter so that would be stored suppose i have entered 15 now i plus plus now i becomes what one condition true again enter address of a of now i is one a of one address is what 104 right so the next data would be stored here suppose i have entered here 16 right so i hope you can continue this process now i plus plus now i becomes 2 again condition true a of 2 address of a of 2 this index now data would be stored here and suppose i have entered minus 1 0 and 15 again like this we have entered once i value becomes what 5 means i 5 less than 5 no it will exit from this for loop and will come here now i am going to print the setting how you will print we will take another 4 loop right you can take any loop while do i again i would be 0 to i less than i less than 5 if you write i less than equal to then you should write 4 right from 0 to 4 and i plus plus now to print all the values we use what printf function for reading we use scan a function data is of type integer so percentage d and a of i simply you have to write this thing right now again one for loop now i would be again initialized with what zero condition true enter printf percentage d which type of value integer value a of i a o 0 right now a of 0 address how address would be calculated how you can fetch in the previous video i have told you the formula one base address plus what this index index means i we are taking i into size of data type data type is integer so size of n so now here base addresses what base address is stored in a base addresses what uh that 100 plus here i value is first 0 into size of integer is 4 means address would be calculated hundred 100 so a of i means 100 here from here the value would be fetched and the fifth the data 15 would be printed on your screen i hope you got it now i plus plus 1 again condition 2 enter here now a of 1 this is how we access the element right now how this address a of 1 address would be calculated base address is this one into plus 1 into 4 that is 1 0 4 that is 1 0 4 from here data would be fetched accessed that is 16. so now 16 would be printed but if you write here slash n then the data would be printed in next line otherwise data would be printed in same line so you will not get you know it will be very clumsy type of thing so better to use slash and new line 16 now i plus plus i become so two i guess now you can repeat the process and this is how all the data would be first right and once i value becomes what 3 then 4 once it becomes 5 condition not real exit from the loop and that's it end of the program right so this is how you can print simply the elements of array now i'll show you the output also my screen but if you want to print in a reverse order from here 15 0 -1 16 15 then how you will print first of all think the logic what you want in that case directly i want to print first of all the statement then this then just send this then this so now at first what kind of logic come to your mind scanf this for loop would be same because it is for reading purpose you have to change this for loop only this is for printing purpose right so now rather than starting the index from zero first of all you want to go to which index the last one right so directly start the index from what i is equal to here 4 if you start from 4 then it should be next 3 then 2 then 1 then 0 so here should be i minus minus simple thing condition is what you will move this side till i becomes 0 if i becomes minus 1 you have to stop so the condition should be what i greater than equal to 0. and simply print a 5 that's it now see the working of this thing first of all here this i value would be 4 in this variable 4 would be stored is this condition true 4 greater than equal to 0 yes condition 2 enter into this loop printf percentage d a of i a of i is four directly a four you have to access so now how base status will be calculated sorry the address f4 would be calculated base addresses hundred plus index is four into size of int is 4 means 1 0 sorry 1 1 6 directly you can access this 1 1 6 and what value would be printed 15 i minus minus sorry i becomes three now right so condition true again enter here a phi a of three again it should be calculated i becomes three address would be one one two excess this thing zero would be printed minus minus i hope you got my point now once i becomes what then 2 then 1 then 0 would be printed the 1 once i becomes minus 1 condition is not true exit from the loop this is how you can print it reverse order so now let me show you the output of these two programs on my screen so now let me create one more file for you i'm writing what reader okay don't see first of all header file we will write then main function and then we declare array error name is i'm taking only a as well as one variable loop variable i right and one for loop what to enter first of all to read the array right less than five and i plus plus so for reading purpose we will use what scanf and percentage d i hope you know the syntax address of a of i right and then for printing purpose again we will use one more for loop and i is equal to 0 till i less than 5 and i plus plus right and for printf we use which function printf so i'll write array element add i'm going to print index value also at index like zeroth index one one index second index like this percentage d is and one more percentage d for actual printing the value so first percentage d will be will be replaced with i value and second how to access a of i the actual value right and that's it so i'm just writing here one more print there it will ask like starting enter the array elements right and now let me just just run this program and it should ask you enter the area elements first of all so see it is asking array elements i'm entering 1 minus 9 0 four five and see okay uh i'm going to decorate this a little bit slash in for the new line right and now you will get it better like minus one four three five two oh sorry sorry sorry and now run this program and here i am going to write minus 1 3 4 5 6 and see okay we i have to save it first of all and i haven't enabled that auto save option so now again i'm going to run this program and it will ask enter arrangements minus 1 0 5 6 7 and see an element an index 0 0 to 4 it should be so minus 1 0 5 6 7 it is going to print right so it is in forward and if you want to print in reverse order then just a little bit you have to change here in this also i am adding one more for loop so i should be started from size minus one last index i want to first of all excess i should be 4 i greater than equal to 0 and i minus minus sorry minus minus right and here simply print printf array element at index same line percentage d is you can copy also and one more percentage d because two values we are going to print first of all i value and then a of i value right i hope we will get the answer right now i am going to run this program and it is going to ask enter elements of array 9 five six seven and eight see it is first of all it is printing forward nine five six seven right and eight last one and then in reverse order again i am going to write what slash n here then you will get it better so again let me just run this one two three four five see first of all from zero to four 1 2 3 4 5 and then 5 4 3 2 1 right so i hope you go to this one so now here you can change something like this if you don't declare the size here i i have told you like if you will do something like this you don't declare the size here then it should give error right and obviously if you run this program then it will give error see it is going to give some error here you have to give some output so you can try out those different different things if you are getting the you know error or not right and you can initialize here then you can initialize at runtime and everything you can do here i have just shown you the program fine and here you can also write down something like this 3 plus 2 that is also fine right it is not going to give you any error okay i have to save it first of all now let me just run this and see it is asking a number of elements so you can write down any expression also which is positive uh integer right so you can try out all the things and you can tell me in comment box which are getting uh the things i uphold you in the program like declaration and initialization videos those are correct or not right so you have to try out all the variants here so now in the next video we'll see a more complicated problem like um we'll take marks of maybe five or ten students we will what calculate average of all the marks first of all we'll sum all the marks then we'll calculate average using n so now i'll see in the next video till then bye
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 43,453
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, introduction to c programming, c programming tutorials for beginners, arrays in c, data type in c, loops in c, gate cs lectures, ugc net computer science preparation material
Id: ZxqOvg05O6w
Channel Id: undefined
Length: 20min 44sec (1244 seconds)
Published: Sun Jul 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.