Hello ji , this is Love Bubbar and welcome to Channel code help. Today we are going to start with lecture 9 and we will talk about arrays. we will be starting from the basics(what is an array?, why do we need array?, how do we use array? what is the functioning?, what all operations do we need? We will also solve multiple questions like what is 1D array, 2D array, multi dimensional array? how can we make arrays work with functions? Also we have learnt pass by value, pass by reference, we will be using all these concepts also. Also you will get so much to learn in the video So lets start the video. First of all I will give you all a situation, about which we will try to understand and then we will get to know what actually is an array my dear friends, I'm giving you 3 variables they are 'a' 'b' 'c' you will have to tell me from the following 3 variables which variable has the largest value simple enough I'm telling you that variable 'a' is 2, 'b' is 3, and the 'c' is a zero and now you tell me which of these has the largest value and the largest is 3 if i ask you to code then you will tell me that i will do 3 variables,'a' 'b' 'c', then compare the three and i will get to know which one is the largest. and i will say that's obviously correct for suppose if i'm giving you 10 values instead of these 3 then what is your step? your answer will be, instead of 3 i will do 10 variables and then i will get to know which one is the largest okay. well, it is optimised. lets head forward. now instead of 10 i will give you hundred values now what's your answer to this? which one is the largest well you are a hardworker, you will do all the variables you will complete the code in 1 hour, LOL now i will give you 10000 values? what will you do now I gave you 10000 integers, what next? now it is getting really harder to tell what's next, isn't it? Here intelligent people might say that as soon as i take input i will find out whether it is maximum or not but i'm telling you that you should only know the values, that what should you tell in the answer and what you give me in the output in the out put you give me, all the elements (that is the values), below this write the maximum that is all the elements so you will have to take care of all the elements as well as the maximum value. you cannot delete it and should store the data try do smart work not hardwork because it is not possible for us to do 10000 variables. here we use array so what is an array, let's understand what is an array this video is sponsored by relevel. If you wish to enter the software field and want to have a good job then Relevel is the platform where you find a lot of companies like Cred, 1mg, Meesho, UpGrad and so many others where you can work as a developer on monthly leader board we have kanishk singh who recently cracked a job in Cred with 40 LPA if we are able to maintain our place in the leader board then you will be able to earn a lot of money so to take part in this firstly you will have to register yourself for the test you can chose so many profiles under the test like buisness development test, analytics profile and many more profiles are available. you will have to give the test you will have to attend the interviews and then you will get the offers and all the process just takes 15 days and you will be hired you will have to book the free slot accordingly for the test thank me later for this information here i'm providing you the coupon code in the description box. enjoy your job thank you we will use the simple language to not complicate things this is a type of list in which you can include the same items for example, imagine you having a bag in which your mom can add only potatoes only potatoes and nothing else and your dad has a valet in which he can only have 2000 rupees note. Only 2000 note can be added and nothing else. In the same way, think there is a container named array in which similar kind of information is stored . from this what we understood is array is a data structure in which only similar kind of things can be added And what do you mean by the same kind of things, we are talking about data type, it can be integer, character, it might be custom made array made by class and object. It might be a predefined data type or custom made data type. Like we can say we made an object for a class and want to make it in array form even that will work so we need do understand that we have so many integers, characters, pools soo many items in the similar manner then we can store them in array so i said i have 10000 integers and i will make an array of 10000 integers or i will say i will make the array of this type for suppose, i said i have 10000 characters or 100000 characters so i will make array of those characters or for suppose i said i made custom data type and i call it car lets imagine i have 1000 characters of this and so i will make array of this of which type? car type. now something is understood in which ever type there are simlar products we call it as array That's it now when we wrote array.. what did you understand?[ we are relating it with similar type of items Avo: we are holding the similar type of catch either int or char but all are of same data type not contain different like char, int,string single type which you want to make string different ,char different, int different cant mix all 3 of them only sambar or only idli or only dosa cant mix all of them lets find out next catch in array the values a stored in contiguous locations all things are stored at contiguous locations means int a then we have a block in our memory which contain some values garbage value we gave it name a something like that it have some address in memory asume that its 100 make a array in which i want to store 5 integers like this block diagram 2 , 3 ,4 5 like that we store 5 integer in here 3,5,9,2,11 address 100 we know int is 4 bytes so 100 ,104,108,112 I am assuming that its starting from 100 now we understanded that it will store continuously in the memory we found one more catch one more thing all the values of your array can access from Index if i want the 3rd element of my array i can access that using the index what is index we will study in future we understood what is array how to store little bit idea about it what is index little bit about it that array is nothing bhaiya told me is a list which contain same type of elements thats called array why we need array? ok so i told you that you can store multiple same time elements in one variable when i ask you 10000 values ,what you told me that brother i can make 10000 variables you also know that this is stupid ans but you told me so if i store these 10000 values in one variable how would you feel so why we need arrays ? when we have the power to store multiple values in one variable then what is the loss to use array easy to access we will enjoy from here your data structure journey begins why videos are coming late 2 days pass why you are not uploading videos my editor was on a leave for 1 week so i have to edit my own videos so its coming late and it takes a lot of time one video take 6 hous to edit and then upload now my editor is back so videos are coming regularly till now we understood what is array? is a list , stores at contougus locations i can access them from index we can store multiple values in a single variable a big profit for me that why i'm using array if you google that why we need arrays you will get the same answer i also read this from there XD how to implement that we will see when we do ok now start implementing we studied int a; that i declared but how i declare array in this case a block made in a memory in which garbage value which we called A but now if i say i have tu declare an array then see how to declare make a int type array and name as 'dost' and how many dost [10] (int dost[10]) this is the way to make an array a array was made named as dost locates the address of the 1st element its a name but at a same time its locates the address of the 1st element make total 10 memory blocks which type int type so here i only declared so i dont know the values (garbage value) we understood declaration assume that you have 5 size of array i said your 1st address is 100we know int is of 4 byte so next location 104 ,108,112,116 name of array int v[5] so we are showing it from v so v is locating at the 1st location when i say v[0] lets talk about indexing in arrays indexing starts from 0 which means index of 1st location is 0 next will b 1 and so on when we write v[0] its locates 1st location assume 2 is in 1st location so v[0] is 2 which means cout<<v[0]; is 2 when i say v[1] we will find the address like v is 100 and 1 x 4so 100+4 =104 and in 104 block we have 6 so when you try to print v[1] so answer will come 6 like this is 3rd index if i try to access v[3] so v is 100 +3x4=112 so if i print cout<<v[3] it will print 8 so we understood index and how to access it how to find which location I am talking about base address and on the bases of index you will find the total location and done in arrays 1st location starts from 0 index and after that 1 2 3 simple very easy we understood that how to declare array and how to access the array assume an array dost int dost[10] if i have to find its 5th location so i write dost+[4] or i written 4 instead of 5 because indexing starts from 0, let me show for your help 1st location ,second location ,third location fourth location so in case of array index if your array size is n so 0---(n-1) so thats why we are going 0-9 in this array you can access the array through index you understood declaration and how to access initialization in variable we write int a=5; which means make a block and name as a and store 5 in it very easy this we called we initialized a so how we initialize array i made an array int number[3] now we have to initialize it we have to put values in starting int number[3]={5,7,11} so there are 3 boxes 1st is 5 2nd is 7 3rd is 11 and index 0 1 2 respectively and name of array is number so that how work done and today you learn how to initialize an array there are many ways like i want all my array int array[100000] so I am not gonna write all the values so let us assume i have to initialize it from 0 if we dont write 0 in this case you will get 100000 values and contains some garbage values and no one will know the actual value some thing random comes if i want to do it from one no you cant do it from 1 its your homework to find how you will initialize the whole array from any value time to impliment lets play some games 1st we declare an array int number[15] run run complete no errors cout<<"everything is fine" now run every thing is fine no errors cout<<endl<<"everything is fine"<<endl; cout<<endl<<"everything is fine "<<endl << endl; if i want to print it now lets make a function void printarray() cout<< "value at 0 index" << number[0] << endl; we saw that on 0th index we have 0 so on 1st index is also 0 on 15th location its 1 its random we declared an array and we printed his random value you can only find the value of array if your index is lower than the size of your array error comes you go to a nike shop the shoe cost is 20000 but you have only 10000 so you cant get them so in same if you want to access the 20th value you have to add the 20th value to your array in our case we are trying to find 20th index value so if i want to find 20th index we have to make 21th sixe of array example if you want to access 5th index i have to make atleast 6 size of array int array [15] make sure all index you want to access is under the size of the array range of index 0-----(n-1) n= size of array we understood how to declare how to access now we inislise an array int second[3]={5,7,11}; lets run everything is fine trying with second index run no error second[3]={5,7,11} so 2nd index is 11 all correct this is how you initialize an array int third[15]={2,7}; no error everything is fine how to print there are 15 locations to access we need only name of array and index if i start a loop 0 to n-1 and put i in this, will it work? for{int i =0; i<n; i++) { cout << third[i]<<" "; } now lets run it we put only 2 and 7 and the rest comes 0 so if we try to initialize the array n the size of array is really big and we enterd few elements so the rest will be 0 we catch that if you entered few number so rest will b 0 one more thing we learn that int forth[10] ={0}; lets run all comes 0 i can initialize all from 0 lets try from 1 int fifth [10] = {1}; lets try in this case only the 1st value is coming 1 and the rest will remain 0 same 1st location 1 and rest 0 if we want that all become 1 that not gonna happen not possible with below line done aii one thing is not looking nice we dont need to write the print statement everytime just create a fn and the recall it for print 1st let me copy this i made a fn up there name print array in this fn 1st i have to pass my array int arr[] printing the array printing done or if i call the function and run the code, its working printing the array It is a very clean code recently we studied arrays with functions and how to pass arrays in functions a function main and another function print array if you want to do this, you have to take your array into in the parameter, no need to give the size lets try let us assume that the fn is only for the size 15 array all working properly you can put the numbers, but dont do it. Its not appropriate you should know about length of your array and size of it a operator named sizeof can be used to determine the size assume we have to find the size of fifth like fifth is of 10 elements so it has 10 blocks, 1 block is of 4 size, total size = 40, so if i divide it with 4, i get 10 locations so the length of fifth is 10 now print so you can find the size from this method but it has a mistake lets see up and assume that you made an array which is of 15 size but you only put 2 elements in it for you the length of the array is 2 but when you try to find the size of it size of 3rd is 15 but we only needed 2 size because we are only intrested in these 2 elements in this case where I have larger size array but i put less number of elements so i cant get those elements from this method that why when i make a function i send the size with it dont get confused between size and length thats why i take this size variable to apply loop you can find actual length and size off the array but if i want that in a big array, i want to find small part of its size, then its not possible array in char commenting all other print statement everything is fine putting values everything is fine lets print in 3rd index "r" printing getting error the function printarray is only for int again printing everything is fine same you can do with any double, float, bool so till now you understood that why we need array ,how to declare it ,how to initialize it, how to store in memory meaning of int , square bracket size ,how to access the array ,index of a array what is garbage values now we can continue lets do a question you have an array, tell me in output which is the maximum element and which is the minimum element example- i have an array -{4,12,8,10} give me the output that max=12 and min=4 simple create a new file we have to make an array. For that first we will take the input of the size of the array and then declare the array int num[size]; we created an array. Size will be entered by you, it means the size of the array is an variable size is an variable in this case, we should not use a variable to define an array's size if you put size =100000, its still good but taking variable is not a good practice i'll teach you when you will take size as variable If you know the constraints, then you can easily declare the array of that size so here we will take 100 values taking loop we know how to print but you didn't teach us how to take input its simple just replace cin by cout taking input int getmax(int num[],int n) INT_MIN, you know int has a range so the min value of int stores in int_min and the max value is in int_max in default case min value is int_min Writing code When you will come out of this loop you will have your max value ready same we can make a function for min value if your num is small from min then copy it to min taking output same for minimum as well now run everything is fine lets try one more take 10 elements max =22 min =-19 code is working properly you studied about how to take input in array, how to find max and min values we have some pre defined functions for max and min values you have a variable maxi which is storing the maximum element lets use this here comes the output so you can also use the default function to find max and min values same doing with min function fn of min lets run. It is working fine now you know how to find maximum, minimum numbers now you know little bit about functions. Lets talk about scope create an array. Then we will call update function printing lets see what happened here printing lets run this error incoming now working inside the function 1 2 3, going back to function 1 2 3 if now I update this array like change the 1st element of array from 0 to 120 and print lets run it error now ok inside the function 120 2 3 going back to the function 120 2 3 but what we saw in variable case that if we try to update a variable in a function, it don't work main function is having a variable int n=5 sending it to a function n++ you told us that they have different n so how it updated in array main value lets see we have a main function and a second function update we updated the value 120 and when we try to print its value in the main function it is also 120 how? its very simple in our update function, update(int arr[],int n) our function call=update(arr,3) in int arr[3], arr shows 2 things, 1st its name, second the base address so you gave your starting address to this function you just gave the address of your first block not the memory blocks main gives address of first block of array to update which is stored in int arr() so in this arr you do any kind of processing like adding something removing something you afre doing these things on actual array in case of array you just give the address and the changes held on that address only and the orignal value will also update when you pass array in function always remember never update . update if its required because what you update its updated to your actual array not in the copy one make sure you know that if you update from here the actual array will update home work- you have an array print sum of all elements in an array n=8 is power of 2 or not one more approch if i no of sct bit -1 then my answer is yes otherwise no linear search lets try creat new name as linear search.cvv creat an array you have to told me weather 1 is present in it or not 1st make a function bool search passing array through it we have to tell that we have this element it it taking input calling function search taking conditions assume that we made a array size 5 ={1,2,7,9,11} search 7 in this array we go to the 1st not found then 2nd not found 3rd found done now assume my key value =11 same one by one Now the next question which we are going to do is - Reverse an array Let's say we are given with an array having elements and we are asked to reverse it After reversing, our array will become equal to Now, how will we do this? When I was in college, there was a subject "Intro to Programming" in our first semester and there we have to reverse the string That time, I was not unable to solve this question as there was no one who explained it on youtube or in any course.. Even if you are afraid of solving this question, then I must say that you should not Let's start solving it - To reverse the array, we just have to swap the elements from the both ends Suppose, we are having array with elements [1, 2, 3, 4, 5, 6] then we just need to swap the elements from the both ends i.e. 1 and 6, 2 and 5, 3 and 4 will get swap with each other It was a simple problem where we just have to visualize and observe to get the correct logic of the problem As you can see that there is a replacement of ending positions only to get our required output. So we know that if it is on the first position on left side, then we have to put it in the end on the right side and vice versa Let me show once again Take this example We will just replace these ending elements Now do the same thing with 2nd and 4th position So this is our answer So we are just swapping the elements and our array is getting reversed But how we will write code for this 1st observation - Array could be of odd length In this case, the last element gets swapped with itself 2nd case - Array could be of even length, as we saw in this example our whole game is just dependent on these swappings Let's take an array and see what to do Now I want to replace this with this So I declared 2 variables to keep the track of starting and ending indexes I will swap the values at these 2 indexes after this, we will increment the 'start' and decrement the 'end' Then again I will swap the values at these 2 indexes Then againwe will increment the 'start' and decrement the 'end' I will swap the values at these 2 indexes Now our start and end, both are here. Now on incrementing start, and decrementing end, they both will cross each other` This is our terminating condition Let's dry run it for odd case First I will swap the values at these 2 indexes we will increment the 'start' and decrement the 'end' Then again I will swap the values at these 2 indexes Now the swap and end are are same index, we will swap this element with itself Now our start will come to 3, and end will come at 1 Now we have reached to our terminating condition, we will stop here Let's take one more example to understand properly This is the array Here is our start and end I will swap the values at these 2 indexes then we will increment the 'start' and decrement the 'end' Then again I will swap the values at these 2 indexes now we will increment the 'start' and decrement the 'end' I will swap the values at these 2 indexes we will increment the 'start' and decrement the 'end' Now the start is greater than end, this is our terminating condition. we have to stop here Now let's write the code for this one Making two arrays we will call the reverse function and then print the array We have already written print function, but let us write it again writing PrintArray function Now we will write the Reverse function start = 0, end = n-1 we know that we have to stop when start is greater than end, so we will run a loop as long as start is less than end we have to swap the values at the 'start' index and 'end' index we will increment the 'start' and decrement the 'end' Is this code enough? let's run and see It is giving correct answer You can see, how easy it was We are just swapping and update start and end so we have reversed an array These are some of the questions which we will do in the next lecture 1. swap alternate elements 2. Find unique elements in array 3. Find duplicate element in array(very important question0 4. Find the intersection of 2 arrays 5. pair sum 6. Triplet sum All these questions are very important and can't be skipped 7. Sort 0s and 1s All the questions are famous ones which are asked in various companies So you will enjoy the next lecture So tell me in comments how was the today's lecture. Are you getting bored? Enjoy this learning process. If you are finding it boring, leave it. Do it only if you are enjoying it If you consider Amazon, Microsoft, flipkart , as good companies, then i can help you in cracking them So this was for today's video, will see you in the next one Write in comments, how is this course? It provides me motivation + it increases the reach of the lecture So this was for today's video. Will see you in the next one. Till then, bye