Transcript for:
Intro to Data Structures and C++ Basics

hey everyone welcome back to the channel I hope you guys are doing extremely well just in case you're for the first time over here welcome to the channel and if you don't know me I go by the name striver in the programming Community if you want to uh check me out you can definitely check me out on LinkedIn I'll be leaving my LinkedIn Twitter Instagram telegram all the social media Handles in the description below so without wasting any time let's get started so uh take you forward is a full-fledged interview preparation website that I run on my end so if you're someone who has three months to like six months of time left for interviews I will definitely ask you how to check this Strivers SD sheet trust me this is one of the most followed resources in the country as of now what does this resource gives you it gives you 119 uh 191 well curated problems such that all your DS algo Concepts will be covered but only prerequisite is you should be well versed with the basics of DS algo in order to start with now if you're someone who's a beginner you can definitely check out our Strivers A to Z DSA course or sheet now over here what I've done is I've created 455 steps every step has been curated properly and you will get all the problem links everything imagine you open dynamic programming you want video you get videos for everything so all the hard topics have been covered you'll find videos for all the hot topics now we will be starting off to cover all the basic topics so this is the video where we will be starting to cover from the step 1.1 in case you have doubt about The credibility you can go to our LinkedIn page and you can check out all the success stories or you can follow me on LinkedIn and you can see how many people do tag me and also you can check out our Instagram story highlights so if you go to Instagram there is a highlight as success stories over there you can check out all our success stories like there are thousands and thousands of success stories of the striversity SD sheet and now we are starting off with Strivers A to Z DSA core sheet which is for beginners or intermediate level people today we will be covering the step 1.1 that is things to know in C plus plus Java python or any language now in order to start off with data structures and algorithms you have to pick up any one language it can be C plus plus a Java python or JavaScript and you have to code in that language make sure you master one language now in the previous video I've already talked about C plus plus versus Java so you can definitely check out that video so in this video I'll be telling you about C plus plus and I'll be covering all these basic stuffs related to C plus plus and in the next video we will be covering the Java 1. I think you should definitely check out coding ninjas because web development okay data Stitch algorithm foreign County there's a link in the description make sure you check them out I'll be using vs code in order to code I'll be writing my C plus plus code over here I'll be giving the input over here and whatever the code generates as an output will be produced over here so before starting this C plus plus video I love to mention two points first is please make sure you watch the video till the end you might be knowing C plus plus but there are a lot of things that I'll be covering in this video which you might know or you might like find it very interesting so make sure you watch the video till the end the second point is again very important when you're learning something on for the first time do not dig deep too much into uh certain things because what happens is when we start off we start to think that okay every certain thing will learn in depth and eventually we are stuck in the beginning process and we do not move ahead so what I'll recommend you is first learn the basics and do not dig much and eventually when you'll uh go from beginner to intermediate to medium and then to high but you will on this flow you will be learning everything in depth but during your start do not dig deep so much why why why do not do a lot of why it is good to do but there are certain things where you should not do a lot of wise because that will be not required in the longer run so the first thing in order to write a C plus plus code is the skeleton of C plus plus code that's a skeleton of C plus plus code stays constant across all the programs that you'll be writing in C plus plus something like hash include so we write hash include now imagine the first program that we will be writing is to print our name so the skeleton States okay you have to print you have to private print something which is you have to give an output or let's say you have to take an input so in order to take an input or take an output we have to use this particular library now as I said do not dig deep you don't have to know what is inside this Library yes if you want to know it is okay but you don't have to remember this so if you use this Library this allows you to take input and produce an output and in order to use this library in C plus plus we can use the hash include so hash include i o stream imagine if you want to use something like a mathematical function which exists in the C plus plus library then you have to use something like a math dot h I'll be talking about radical functions afterwards in case you want to use something like a string then you use hash include string so whatever Library you want to use you have to Hash include it you don't need to know what is inside the Library as I said do not dig much deep and then you say int Main and then you say return 0. so this is the generic skeleton of the code I'll remove this couple of hash includes because as of now we are just going to print so this is the generic skeleton of a C plus plus code after this I said I'm going to print my name so I'll be like see out so when I write C out it means you have to that allows you to give an output okay now in order to use C out you have to write STD and double colon so C out is a function which exists under the STD okay so you have to say STD can you give me c out and then two lower than signs and then double quotes if you're printing a string and you can say hey striver and then you can give this so what I'll do is I'll go and run this the moment I run this you see in the output screen we have a striver so in case you want to again print this so go across and copy paste the same line and again run this so what happens is case driver is printed two times but they are printed on the same line in case you want to print them in different lines you can go ahead and give a backslash n and you can just so the moment you compile this what happens is the backslash n goes across and prints it in the new line so very simple can can we use something else yes instead of a backslash n we can take STD and L and we can again compile so if we compile endl is similar to backslash n handle or backslash n takes you to the next line but usually people use backslash n because it is faster okay but you will find people using both so it is your choice if you want to print something on the same line you can again print something like hey Raj you can again print and again you can give something like endle there is no limitations on the thing that you want to print on the same line you can print as many things over here on the C out and that will be printed according to the functions that you give like over here given handle so that is why haste driver hiraj and his driver are printed on new lines so this is how you give now something must be very annoying which is this STD now for any functions for most of the functions you have to write STD double colon double Corner this is very annoying right this is where something like using namespace comes in so we say hey can you use the namespace STD so basically what this means is if you're not giving anything if you're not stating anything it eventually says since you have written using namespace STD again you don't have to dig deep the moment you say this the program always uses STD for all the functions yes always uses STD for all the functions now if you compile it prints everything now it looks a easier task isn't it so this is how you can easily write okay now let's quickly have an input imagine you want to take an input so imagine I want to take an integer as an input in DX so this is how you take an input it's a scene of X scene means input into this variable input into this variable now imagine in the input I go across and I say 10 and I'll be like let's print this x guy cell billi value of x and then I can go ahead and write X so if I go and print this you will see value of x is printed as 10 what happened you printed this particular string and then you said space and then you printed X so this x was taken from here got it imagine if you want to take two variables as input so if you want to take two variables as input you can give it like this and over here you can give them in the same line the value of x and then you can go ahead and say and Y and you can probably write this as bat this one and this so what happens is value of X and Y now do you need to give them in the same line you can give them in different lines as well and it will still run in the similar fashion so this is how you can take any inputs so what happens is the first thing gets into the first scene the next thing gets into the next CN so depending on how you are writing C in line by line line by line everything gets into variables that you're initializing it with now you might be thinking what is this in now there are certain things as data types which I'll be talking about but before moving into data types now as I said there are a bunch of libraries imagine you are taking strain integer let's say a lot of other data types all a lot of other libraries will you be writing hash include hash include hash include hash include every time no it's going to take you a lot of time this is where C plus plus C is a I've got something better for you I say can I write bits STD C plus plus dot h now what it does is it includes all the libraries everything whatever libraries are there in C plus plus everything is included under this bits slash stdcplus plus dot h I might think but striver in order to include so many libraries we will be taking a lot of time yes we do take a lot of time but that is very negligible compared to the time that your program takes to run so that is why it is okay to include bits slash stdc plus plus dotage yeah it takes a bit more time but that is okay again if you want to check out what are the libraries inside this you can definitely Google this and you'll get all the list of libraries that are inside this particular directory so this is what is the common skeleton of the code that we will be using to write any C plus plus program on the planet so you have discussed the first thing that is the skeleton of the code and the user input and output to a certain extent we will be digging deep into this as we move forward what is the next type that is the data types now we're talking about input and output now if we are taking a number in you have to store it somewhere in your program that is the place where you store that can be a data type okay now imagine you are taking a number like 10 12 minus 5. what will you use to store it so there is a data type known as int integer now this is how you can write comments in C plus plus if you give double backslash you can write any comment so integer what does comment means those lines of uh code will not be executed so this line will not be executed when the program is executed so int is the data type in order to assign anything to integer data type you have to write the data type and then a variable name variable name can be anything it can be Raj it can be X it cannot be a number it cannot be a number okay so it can be Raj one that is okay but it has to be something which has a character so something like let's say I say x equal to 10. so this is how you assign something into an integer data type but integer has certain restrictions it can only store numbers from this range to this range Beyond this it cannot stop Beyond this what will help you something like a long data type will help you if you try to use long it has a wider range same concept you can use the same concept to store uh variables in long if you want to take an input this is how you take an input if you write C enough X the first number 10 will go into X okay now what if you want to store bigger numbers than this this is where something like long long will come in so if you are saying long long it will give you a wider range again the same way you can store it like this or maybe you can go ahead and store a very large number as well so usually we will be using these three things to store any type of integers between a certain ranges okay now what are the next data types that we will be using we were talking about integers what if we have something like let's say 5.6 something like this so it is having something after the decimal point this is where something like we will be specifically using two data types float and double again they have ranges depending on the Range that you use you'll be using float or double the ranges are visible on the screen so we can use something like float again same way float x equal to 5.6 again you can always store float y equal to 5 as well it doesn't mean that like if I'm saying that it has to store decimal it doesn't means you can store an integer if you go and say C out of Y like value of y and then you say okay let's print this one let's see what it prints okay so if you see the value of okay it does give you an error okay by the way let's erase this because we have defined X twice okay let's now compile this you see it is getting printed similarly if you want to Define this as double it's it's still okay if we Define this double and integer can also be stored in a double okay so in data types you have learned how to store numbers and numbers with decimal points now what if you have a string or a character how do you store that so for that what we will be using is something like a string and get line so you have to learn these couple of things okay now imagine I write a string so this is an entire string and the same way I'll say string s and I'll say CN of s I'll say C out of s okay ideally what should happen is this entire Haze driver should go inside this string and this should be printed but let's see what will happen if I run this you will see just a being picked up and if you want to pick up striver what you have to do is you have to Define two strings if you want to Define two strings in a same line comma will be the thing or you can go ahead and say string as and over here S1 and then you can say S2 and over here S1 and then you can give a space and then S2 so what will happen now if you do first S1 is picked up as a then S2 is picked up as travel then both of them are printed with a space right so if you're saying string then what happens is it only picks up anything before the space and after the space it's a new string so you have to again give a c in now in case you want to take everything together in a string how do you do that in order to do that you have to probably Define a string and then if we use something like get line inside the get line you have to write CN what is get line again do not need to dig deep it's an internal function you have to write CN and then you have to give this variable over here and then you go ahead and say C out of Str so what will happen is this get line will take the entire line and put it into the Str now if you print you will see this Haze driver is printed or maybe hey Striker I am cool and if you print this you'll see the entire line is getting printed but if you go to the next line hey Raj will the entire line be picked up let's run this no the next line is not picked up only this particular line is picked up so the moment you click enter the next line will not be picked up so it just picks up till the line break so string and get line is used to take any of the string so you've talked about string and get line now it's time to talk about character now there are 256 characters in English dictionary so character can store all of them yes character can store all of them so imagine if you have something like an H so you can just go ahead and declare cash and you can take c n of CH okay and then you can say C out of CH so what will happen is this H will go inside this and it will be printed over here if you want to declare a character you can go ahead and declare this as G so single quotes is what will be used to declare a character you can also declare it as string but usually string will be within double quotes that is also okay but string ends up taking uh much more space that is why if it's a single character we tend to use something like a cat so okay do we have okay so these are the data types that I have used till now to solve all the problems in DS algo did I require anything else no in case I've required for a single problem like one problem here or there I have done a Google Search and I've used that but I can assure you that if you know this that is more than enough now you must be having a lot of questions sure if we have something like a 10 and that 10 can be stored in all the three or all the five why can't we use simply long long or maybe double now the thing is everything takes up a certain space in your computer now you can't be like you can you can't use long long to store 10 that will not make sense because it will end up eating up a lot more memory space so integer takes a certain memory long does take a certain memory so everything will take a certain memory so depending on that we decide what to use now integer has a range now those numbers you can't uh remember so what I usually do is I keep a rounding of number like I know integer will be from minus 10 to the power 9 to 10 to the power 9 like rounding off long will be minus 10 to the power 12 to 10 to the power 12. long long will be minus 10 to the power 18 to 10 to the power 18. so whenever there is something as 10 to the power 13 I switch to long long you don't have to remember exact exact numbers because you will not remember that much when you go on the longer Run Okay so that is how I usually do it and you can also follow the same plus so you have completed data types now the data types that I've covered that are more than enough again very crucial point do not do not dig deep because eventually when you keep on coding you'll learn everything okay first third thing if else statements let's go so till now we know this skeleton of the code we know the data types now let's write some program so the first program is write a program that takes an input of a age and prints if you are an adult or not so the skeleton is something which I've written and this is the program so let's break down the problem it states write a program maybe I've written the skeleton that takes an input of age now you know one thing age will be what an integer 18 or maybe double imagine the age we are talking in terms of integer and we know you're called an adult when you're greater than 80 correct so imagine I enter something like 19. in that case you have to print adult imagine I enter something like 17 you have to say not an adult we print 18 uh adult right if I print 10 not an adult so I need an age so maybe I can go ahead and say okay int age so you need the H from the user so CN hey give me the H so maybe if I give the age over here or something like this this should be picked up into this age now this is where something like if else statement comes in what is it saying print if you are adult or not can I say if you are greater than equal to 18 then I can say you are an adult you are an adult yes or if you are lesser than 18 E1 not an adult can I see this this is something which you are speaking in very layman terms if you want to convert this into programming language this is where something like if else comes in there is something as if this is the block and there is something like else this is the block so inside this you perform skating okay if something happens perform these lines of code if that thing doesn't happens go and perform these lines okay so I'm saying if the age is greater than equal to 18 then go and perform this and if it is not go and perform this cells very simple and if I'm saying if the age is greater than 18 what am I doing I am saying print you are an adult so the print line is very simple you are an adult simple like you can just capitalize this and if you are not then go and go ahead and print this you are not an adult so basically first line says 20 will go on to each then it will say 20 greater than equal to 18 yes it will go inside this and print this so let's run this and see what happens so I'm taking 20 yes you are an adult if I take 15 what will happen it will see you are not another simple now this is the basic if else statement now you can modify this as well now imagine you are saying for an example you are saying 18 yes 18 no I can also write this as as this this is okay this is saying age is greater than equal to 80 if not go to the else but only get into the else if the condition satisfied over here it will but eventually you will learn is it mandatory to write an else statement always you can omit the else statement as well so if you omit the else statement what will happen is the 15 will be taken as the input and the if will not be executed and will not find an else so nothing to be executed completed so it is not mandatory to have an else statement if you are having if it is not mandatory to have a else statement so coming back to the else if what happens if you're giving 50 the if is false it states no not greater than equal to 18. so goes to the else but L says wait before going into me you have to pass my condition in my condition states are you lesser than 18 and it says yes some lesser than 18 so I will go if this was less than 10 see what will happen you're giving 15 I am taking 15 balls goes to else and else condition say is less than 10. when I run it doesn't runs wire false again false no execution but the moment you give something as nine see what will happen when you give it 9 it prints y goes here Falls goes to Wells and he says 9 lesser than that yes goes ahead and prints this this is how something like a if and an else if block Works got it so let's now move on to a complex problem so that you can understand the conditional statements in depth so this is how you can write the multi-line comments since I had an entire problem statement I put them under multi-line comment so backslash asterisk is how you can do it a single line it is very simple doubles backslash but front slash your backslash whatever you called it call it so this is how you can write the multi-line comment so the problem is very simple school has following rules for grading system below 25 grade f 25 to 44 grade e 45 to 40 49 ready and so on so your task is to ask user to enter marks and print the corresponding grade okay so what is the first reading get the marks so imagine I have to take the marks so this is how I'll take the marks this is something which we know what is the next line below 25 very very importantly below 25 so I'm saying if the marks are below 25 can I say the grade is for sure that is something which I definitely know if the marks are below 25 I know the grid is f if the Mark is between this can I write if the marks now over here there are two conditions should be from 25 and should be till 44 so can I say should be greater than 25 and and if you have multiple conditions if you want to satisfy multiple conditions you say and and marks lesser than equal to 44. this is how you satisfy multiple first condition and and second condition and then you say C out e similarly you can go ahead and write all the if conditions so what I've done is I've written all the condition you see all the conditions are let's say uh the marks is 85 and see which condition is getting executed so if you take the marks as 85 this is executed if you take something like 24 let's see what is executed you see everything is running fine but is this the correct way to write no but why what happens is the marks is going and checking this set imagine the marks are something like 86 this if is false again goes and checks this false again goes and checks this false again goes and checks it false again goes and checks this false a game goes and checks this false so at the end this is executed and a is printed over here okay now imagine if I give 24. what will happen F will be printed right so this is executed true principle but then go ahead this is executed false this is executed false executed false executed false executed False only this was true but all the ifs got executed hence you ended up taking a lot of time this is where yes this is where something like an else if comes in so if you write else if see what happens else if you can write else if you can write else if you can write else if so if you write else if what will happen is if 24 is true over here none of these lines will be executed because it states else it will never go there and it will not be executed so you'll be saving a lot of time previously in every F was executed so this is why else if will be written now another thing you're having 24. imagine if you have something like uh let's say 30. so we are saying if not lesser than 25 so this is false this is definitely false do you need to check again or greater than equal to 25 because we are sure that it is not lesser than 25 years sure because this is false you are sure it will definitely go here so you don't need to write this anymore you don't need to write this anymore because if it is lesser than 25 and if it is less than equal to 44 that means it is between 25 to 44. it means that and if this is also false it means it is not less than equal to 44 you can also omit this T you can also omit this same way because 49 is checked 59 is checked so you can omit checking greater than 60 and eventually you can do this so you saw how easily I trimmed it down this is how you will be trimming it down so for now deal you see 30 is printing e if you give something like 40 that will be printing D every line is executed properly because if all these are false which means the number is never lesser than 49 and if it is true it means 50 to 59 super simple what if this is how you trim down multiple conditional statements and multiple if statements so this is the last question for conditional statements and over here we will be learning something about nested if else so the problem is pretty simple uh take the H from the user and then decide accordingly so if the age is smaller than 18 you can print this and all the conditions are given to you so something we know uh for sure is we need age right so we can take the scene of H now we know something if H is uh smaller than 18 then we gotta print something like not eligible per job okay this is what we will be printing correct then it states else if the age is greater than equal to 18 you will be printing eligible for job if it is greater than equal to 18 you will be printing eligible for job okay but if it is somewhere between this which means anywhere between this it's eligible for job but anywhere between 55 and 57 eligible for the job but retirement soon that is what you have to print got it so what I will go ahead and do is I'll say Okay else if age is greater than 18 and and ages lesser than equal to 54 and you can say C out eligible for the job so I'll just copy paste this and print okay again do you need to write greater than 18 no because if this is false it is definitely greater than equal to 18 so let's remove simple or else if if this true or false that means it's not less than equal to 54. so you just need to check if it is lesser than 57 if it is lesser than 57 you go ahead and print C out off the statement very simple you go ahead and print this statement super simple and if it is not less than equal to 57 it's greater and if it is greater it will always go to else it will always go to else and if it is going always to else you can always say C out retirement time very simple this is how you can easily write it so if else if else if and the last can be written you can write the conditional of greater than 57 or you can just leave it as it is so imagine I give you something like 55 let's see if it is working fine 55c is eligible for job but retirement so now if I want to convert this into an SNF just to explain you an extra diff something to uh figure out so over here it states 18 to 54 is eligible for job even 55 to 57 is eligible for the job but retirement is soon that's the extra addition that I have to tell the user just retirement as soon as what I have to tell the user so can I write something like this hey wait can I write 57 and can I omit so if I omit this what will happen for everything less than equal to 57 and greater than 18. I'll always eligible for job inside this can I see if if the ages greater than 55 because I know if it is coming inside it's greater than 80 there is something I surely know it's greater than 18 and less than 57 if it is going inside these blocks and it will definitely print eligible because I know for everyone from 18 to 57 they are eligible but if the age by chance exceeds 55 and I know it will always be lesser than 57. then only I go ahead and say hey if it is exceeding then can you go ahead and say the time and soon but retirement soon that is the extra thing that you need to print right so this is how you can also write it so this is what you call as nested if so what happened was you just took out the F from here and you put it over here does it save time no I just wanted to tell you that inside if inside else if you can also write conditional inside this if you can go ahead and write another conditional so it can go nested these are called nested FL statements inside f1f so nestatives got it so you can write conditions according to your choice and eventually when you go across time and you solve tougher problems you'll see that you will be writing multiple such conditions okay so we have completed the if else statements now what's the next thing we will be completing the switch statement we are talking about switch statement so we will be taking this problem in order to understand the switch it is similar to if else we generally don't use switch that much but it is Handy to know the switch statements as well so what is the problem stating take the day number and print the corresponding day for one if they're giving the D number as one you have to print Monday for two you have to print Tuesday and so on for seven you have to print Sunday so they'll be giving you a number between one and seven so the syntax of switches super simple so you take the let's say the number the day number same day so whatever variable you're taking on that you have to write the switch so it's kind of a switch so you say switch d okay and then you open the braces so on the basis of the day you the switch will be triggered okay so you know the switch will be between one two seven because they're saying the input will always be between one to seven so you write case one okay so case and then if this is an integer you write the integer if this was a character then you could have write a written a character like this whatever is your switch you write the same switch over here so case one is the switch and then you go ahead to write Keys two is the switch and then you go ahead to right case three is a switch and so on so what I've done is so what I've done is I've written down all the cases because those are the cases now I know one thing for case one I have to print what I have to print C out of nothing but Monday so go ahead and print Monday similarly Tuesday Wednesday please print out everything so this is how you'll write down all the cases in order to understand switch better let me give you an example imagine you enter five so what will happen is it will take the input as file switch say is fine it will directly go to case File and print Friday but see what Prince Friday Saturday so it went to the case five and it executed all the corresponding keys but it started from case five imagine you give two it will start from case 2 and it will execute everything so how do you prevent this something for sure is when you trigger two it reaches you it doesn't execute this so once it reaches here there is something as break statement in C plus plus what break does is it breaks out of all the conditional statements that you have written so over here the cases are conditional States it will break out the switch is a conditional statement so it will break out see what will happen if you give a break everywhere so make sure you give a break everywhere after every condition [Music] if we give a break after every condition and after all the cases are executed and if I just give a CO to check see what will happen and I'll also give a c out to check after all the cases have been executed okay check inside switch so ideally what should happen is 2 means Tuesday should be executed break means all the cases and as I said all the condition this will also be broken out and it will reach the line number 36 see so if I give it it reaches the line number 36 check and not over here very important to understand this okay now there is something as a default imagine I give you something like 90 then what happens so if I execute if I give 19 none of the cases gets executed because there is no case as 90. so you you can probably give something as default Keys imagine none of the cases is executed then it falls back on the default so if you give something like as 19 it falls back on the default and you can say as invalid so this is the last executed step they don't need to give a break because after this it automatically breaks out so imagine you give you an if you give 19 what will happen is we'll go ahead and say invalid and then go over here if you don't give a break here and if you give something like C out invalid check can you guess what will happen because you're not giving a break see what happens invalid and then the last line of the switch is also executed but the moment you write break it breaks out it doesn't execute this so whenever you write break it will take you out from all the conditional Loops or all the conditional traces that it has above it got it by the way we will be using break a lot of times in the upcoming problems so your concept about break will get more and more clear as we will be solving problems in the upcoming lectures so this was about the switch statement it is kind of similar to if else that is why people tend to use if else because that is more readable but yeah it is Handy to know switch so just uh just in order to know just know it so we are done with the switch statement now we will be knowing the basics of arrays and strings now arrays will be covered in depth over here we'll be covering 41 problems and strings will be covered uh on the upcoming lectures but as of now we just need to know the basics of arrays and strings so let's uh start with arrays and strings so when do we use arrays is a very big question when you have a similar data type being stored multiple times for an example over here I'm giving you poor data so you can see that those are these are integer data types right three four five seven these are integers so one of the ways you can store them is by the way template yeah one of the ways you can store them as a b c d and you can say scene of a a will take three then you can say b b will take four C will take five and then B so apparently all these four integers one by one will go into these four variables but imagine if you have 50 50 data you can't have 50 variable names because to name 50 variables it's gonna take a lot of effort that is where something like an array is used so what I say is I know the entire data is of similar type which is integer why can't I Define an array so what I do is I say IND array of let's for an example say five numbers are there and the array of five so what this does is I'll give you a diagrammatic representation this creates an array of size 5. now this size 5 array looks something like boxes every box can store an integer the zeroth index can store an integer the first index can store an integer the second index can store an integer and all the index can store an integer what is an index this first guy is referenced by zeroth index the second guy is referenced by burst index one less so if the size is five the last index will be fourth this is how an array will look and depending on the data type that you have given every box or every location of that array of the index is gonna store an integer if you would have defined it as it's a long long it would have stored long long in that index it would have defined it as character it would have stored character in that index so over here our use cases defining it as an integer so I will Define it as an integer now I'll say C enough array of zero CN of array of 1. scene of oh my bad I'm taking the other one array of 2 then I'm saying array of 3 then I say array of four so what happens is the zeroth index takes three and everything is stored now imagine I want to print seven I wanna print seven so what you can say is C out of array of where is Seven Stone seven will be stored at the zeroth in zeroth index first index second index third index second is a three yes and if you print this let's see if seven was stored you saw so what happened was these five numbers went in and were stored in these places consecutively so when you access the third index it means the fourth element got access and you got the fourth element can you modify these yes you can if you want to do operations on them you can imagine I say array of three can you go ahead and add yourself ten so if it adds 10 to itself 7 should become 70 let's see if it becomes it does so you can modify so it is exactly as a data type you like whatever you wish to do you can do it if you want to do something like hey array of three replace yourself with 16. change your value to 16 it can as well you can do whatever you wish to with this particular array you can treat it similarly to an integer data type got it so this is how you can store similar data like together and the container used is array there are multiple other containers as well I have talked about that in the C plus plus STL video you can definitely check it out as well so we got to know what is an array and the most important thing is everything has to be of a similar data type you can't be saying 7.7 if you say 7.7 what will happen is since you declared this to be an integer data type this 7.7 will trim itself to an integer and hence it will be stored as seven got it if there is a double data type maybe you can store double and if you say double then 7.7 is stored whatever data type you give that has to be constant for all the elements that is the key Point okay so that was about the array now where is the array stored we know it is zero the next first index but the entire thing is taken and stored in a memory location yes the entire thing is taken and stored in a memory location so this guys zeroth index is stored at a particular memory and that memory at can be of any address that address is very randomized I cannot say that it is stored in let's say India like there is nothing like at this memory address a desktop throughout the CPU it can be stored anywhere now the second the first index now that one I can definitely say will be stored right after the zeroth right after the zeroth guys memory address write the consecutive one then the first the second guy will be so right after that so the zeroth guy we're not sure but we are sure about the next guys because they will always be in consecutive so remember array elements are stored in consecutive memory address but I am not sure where the first one is it can be at any randomized location very very important point to remember but will it be required in DSL go I don't think so but it is a point which is important and you should keep in your mind so till now we did learn about the 1D array now we will be learning about the 2D array what is a 2d array again I'll be giving you very basic idea about uh 2D array over here eventually when we move across the playlist We will be learning about 2D array in depth if this was declared this was something like Five Guys or five boxes indexing from zero one two three four correct now the moment I say I need five boxes but I need them three times I'll add three what it means is I'll have three five boxes if I show you diagrammatically this is what I'll have three one two three five boxes one two three four three five boxes as what I'll have this is what it means this is the row number we generally call this as row and this is called and if I talk about the indexing over here again the indexing starts from zero and this is how the indexing goes you see Zero 1 2 and this is 0. so this guy this guy is known as zero zero this guy is known as zero one this guy is known as zero two this guy is known as zero three so this is how the naming or the row and column goes if I say one comma 3 so here is one and here is 3 76 the value will be 76 if I say give me the value act 1 comma 3 the value will be 76. again if you want to assign like it'll be like a 1 3 equal to you can say 78 and then you can go ahead and say C out of a one three we will be seeing the use of 2D Matrix a lot we will be solving a bunch of problems don't worry about that so this will be array and this will be array so if you go ahead and print this you'll be seeing that we actually getting 78 at this location and for the locations that you have not defined they'll be assigning themselves some garbage value it can be any value like if try to Define try to print print one two you'll see it is having a garbage value like if you try to print it again it will have different different values every time you print it has some Garb wedge value that comes in from the system because you did not initialize the only guy that you initialized one was the first row and the third column in terms of zero Beast indexing very important in arrays we have zero based indexing okay where is 2D array used it is used to solve a lot of Matrix problems a lot of graph problems which you'll be dealing with in the future okay as of now just know this is what we know as 2D array this is the row this is the column and it goes as row and call that's it that is what you have to keep in your head as of now so in the STL video I've talked about a lot of functions related to arrays vectors list and a lot of other things so you can definitely check the check that video after this one so we have known about arrays in Basics now let's talk about string so imagine this is the string that I am having string s equal to strip and now I want to access I so the string also stores every character in terms of indexes so s will be at the zeroth index so if you try to print something like C out of s of 0 what will happen C s will be printed if you say s of one see what happens T will be printed if you use S of 2 you know what will be print R so every character is stored at an index and the first index is the zeroth index and what will be the last index whatever is the length so can I say the length is this is how you find the length length equal to s dot size other is another function as length which you can use so if I say length minus 1 because I know last index because everything works in zero base indexing the last index will be n minus I know it if I print this what will be printed driver R because this is what happens at the last index that's how you can easily access the string if you want to change any value imagine you want to say hey I'm not interested at this please make it Z stripes and I do it it gets changed to Z so you can also change it remember these are characters characters at every index and characters when gets added makes up a string so all of these are characters you can't do something like this and expect this to work if you do this it will not work and it will give you errors you see errors so since it is a character you have to assign it with a character so string is storing characters within itself so if you know about this and the length function eventually we will be solving a lot of string problems and there you can easily get on with strings so we are done with arrays and strings now it's time to do for loops and while loops and as of now I'll be talking about Basics eventually we'll be solving a bunch of patterns a bunch of patterns and once you end up solving all these patterns using for Loops trust me your logical thinking your concept of all Loops will be super strong and then we'll move on to solving maths and everything using the for loops and while loops and eventually in the entire DSA all loops if you don't know for Loops you can't do anything so please stay focused so what is for Loops why is it used imagine I say you hey print your name I'll be like uh see out okay what's my name my name is Fredo and I'll print it printed five tips done five times print 2500 times 500 [Music] that's a lot this is where fall look comes for Loop says same line of code why do you want to write it again and again I'll do it so what for Loop says is okay same line of code right write it once and then enclose it inside me inside me enclosure and the follow-up says okay my syntax is very simple say for what is the variable that you wanna go across how many times you want to run put up a variable for that I'll link I'll run it for five days or 500 takes what is the initial plus I equal to 1 variable declaration I equal to 1 variable declaration this only gets executed for the first iteration only gets executed for the first iteration then condition at which I'll stop the for Loop will stop the looping will stop so I'll be like the condition is 10. I'm saying till I don't reach 10 I'll not stop and at every step I have to do something if I don't do I will forever stay one and if I stays forever 1 this condition will always be true and if it is always true that line of code will run run run run run at 11 this is where something like increment or decrement your wish comes and I say this is the line that I use I want to execute and I go ahead and write this line now what happens is see what will happen I'll tell you how it happens oh 10 times exactly 10 times Drive got printed what happened was first initialization I equal to one first time is one I lesser than 10 yes goes Prince printed comes back to this line I gets two next gosia 2 less than equal to 10 yes goes here Prince goes again here I equal to 3 after yeah goes here 3 less than equal to 10 yes Prince then goes here I equal to equal to 4. imagine this is 5 for an example so that I can explain I equal to 4 Prince goes again here I becomes five Prince goes here I becomes five checks prints goes again here I become six don't I become six six less than equal to five no no stop so it'll get executed five times but if you try to print the value of I imagine you want to print the value of I something very important to notice if you print it like this it will not be able to find I why because the scope of the I the I was declared inside this so the school wherever you declare right the scope is limited to that so the other way you can do is you can say I and you can write something like this say degrade the I here and then you have initialized it here so if you print I over here see what happens so I print I the last value of I is 6 where it got truncated you can run the for Loop in the reverse order as well something like hey maybe start from five maybe go on till and maybe decrease it so if you do this and if you print the value of I you can see the value of I oh sorry my bad you want to print the value of I maybe go like this oh sorry maybe like this and yeah see the value of 5 5 for the first and four then three then two then one and see you got it so you can write the follow as you wish to is it mandatory that you reduce by two no I want to run this for five times how I run it for five times is my choice I can be like one I can be like I need for five times I will go till 25 I will do I into or maybe I plus 5 my choice I can go like one six eleven sixteen Twenty One my choice that's my choice no one is here to stay how I run doesn't matter how many times I run is what matters this condition depends on you what you need to make sure is if you want it for five if you want it for ten if you want it for 20 make sure the condition is written in such a way that it runs for that many number of times that is the core concept of power Loops do we have nested for Loops similar to F yes we do have and the rested for Loops is something which I'll be teaching you while printing patterns as of now this is what you need to know can we write if conditions inside the for Loop yes you can go ahead and write if any line of code can be written inside any for or any while loop so this is how you write a for Loop now I want to convert this exact similar for Loop I'll just comment it up I want to convert this exact similar for Loop into a while loop so the while loop does the same thing the task is same whenever I attach something like Loop the task remains same executing some lines of code for some number of times we do the same thing and here the only difference is the syntax changes while executes like this whatever is the condition whatever is the condition will go here and whatever is the increment goes at the end goes at the last line whatever is the increment goes at the last line okay and the initialization goes over here first time initialization happens while says one is less than equal to 5 fine go ahead and do c out of favor and if you want to print ice value you can also check it out so if I go ahead and do handle you will see the value see perfect so what happens check print increase check print increase first check goes inside increases comes back checks prints increases very simple this is how a while loop executes so this is about while loop now while As One More Concept and that concept is do while loop okay now what does do while mean so can I say if I keep I equal to 2 and I keep something like this will it execute the answer to that is no because I is 2 the condition is false never executes goes to this ends the program so do while says you don't know the condition because when you are writing code in industry or maybe you're solving a problem you a lot of times the condition might be user dependent or you might have such use cases so you always want one thing to happen that is you want your line of code to be executed minimum or one type minimum for one time that is one that is when you say okay you want it to be executed for minimum of one time I will write it something like do and I'll put this condition for oh I have at the end and then I'll be doing the same thing I'll be doing the same thing so what will happen is even if the while even if the Indi is defined as 2 and the condition doesn't execute because it doesn't allows me and then you end it with a semicolon even if the condition doesn't allows B do says do it for the first go ahead do it for the first then increment then check then do so if you say I equal to two see what Driver 2 gets printed and then if you see the value of I at the end of the while loop let's print what will happen the value of I was 2 you printed scriber I increase to 3 and 3 was less than equal to 1 and it got X edit but do will make sure the first time it gets executed irrespective of what condition you are given so this is the basic like you can Define it here you can Define it before this that is your choice but this is the basic of for and while Loops so I can say uh this is the basic of foreign while Loops eventually when we solve these amazing set of patterns the concept of the four and while Loops will get more and more into your head and you'll be very strong in uh logical thinking and eventually you can apply for Loops in maths or recursion and all the other problems okay what's the next thing functions pass by reference and pass by value functions is very very important because we'll be using functions throughout yes throughout to solve any problems we will be writing functions so please make sure you understand functions in a proper depth so let's start with functions what are functions functions are a set of Port which performs something for you now if I want someone to do something for me like for an example if I want to order food I can say I can deliver food from a food delivery app and that will bring me in food so you can say functions are similar you want it to do something you will give it or maybe something and you will do that for you that is the role of a function wire functions used functions are used to modularize code now a lot of times you'll be having hundreds and hundreds of lines of code and it's it becomes very clumsy so in order to modularize because every portion of the code will be doing something so that something will be put into a function okay functions are used to increase readability now imagine you are writing code and you have like 1000 lines of code you know you know what every line is doing but if someone from outside is reading your code you might not understand so what you do is every step you put it into a function name the function in such a way that it is understandable what it is doing got it and functions are used to same I used to use the same code multiple times so if you are performing same like if you are using the same set of code again and again it's not good to write the same code again and again you can put them into functions and use the function okay so I'll be uh showing you all these things so there are basically uh so there are a lot of kind of functions but usually we do use like four kind of functions one is the void function the other one is the return function the other one is where we carry a lot of parameters the other one is where we do not carry any parameter okay so non-parameterized functions so let's talk about the first one void function the void function is something which does not returns anything very important it doesn't returns anything so imagine imagine I'm saying print your name you know that is easy you'll come across and say hey see how history printed so you know one thing if I'm saying hey is fragile that is something which I know but if someone is coming from outside and is reading this like what does c out mean I don't know I don't know what CR means this is where readability comes in if I do not know C plus plus I will not be knowing what C Out means that is where we say okay let me write a function so what function I'll write us I'll say okay why that is the keyword it means it will do something but it will not give me some it will not give me some and then I'll write the function name I have to print the names so I'll just write printing and in that I'll put this so it did not return me anything it's a void it will never return you anything and this is the function name and this is a non parameter it will not carry any parameter whenever you enclose it within parenthesis and there is nothing inside it it doesn't carries a parameter so it does this now over here if you just run this this will not be executing this function because this is outside the main you know the skeleton of the code is executing anything that is inside the int Main and the function is outside what do you do is is a printer name and you execute this now what will happen is in domain will come print the name will happen and then it will go across and execute the line of code see Ace driver executed the imagine I see take a input so you'll be like okay this is the lineup maybe I'll take ID name and as a scene name and now I want to print this name I want to say hey this name and this name might be Raj so if I say just print name what will happen see it will still print his driver I want you to print a Raj this is where something like a parameterized void function comes in I know I have to take this name maybe I'll pass this name so I'm saying print name take this name print name will say okay please give me this what a by the way string what is the data type of this name this is string I'm passing the name but it gives you Red why because this guy is not taking any parameters but I'm giving it name I'll say okay what are you giving me you're giving me a string I'll take the string I'll take the string when I've taken the string now I'll say hey can I see your name see what will happen this function took this name and printed now this moment when you say Raj if you say struggle it will say a struggle if you say Amun it will say hey Aman simple this function what is the function of this function takes a name and prints a name very simple it can take any name so you can maybe use it again C string name two and you can maybe take name two and you can say print name see reusability of codes I'm using the code again and again and I can say Okay Amun and then Raj and then maybe over here let's give an angel so that every time it says hey you like see what happens now had you written this line you'd have written it again and again and again this is where reusability of code comes in now this is not under like you can maybe put it under for Loop that is okay but if there there will be cases where you cannot use for Loops but you have to use shuttle lines of code again again at different different places that is where something like a function comes so I can call this as a void function and as well as a parameterized structure so you have understood void you have understood parameters you have understood non-parameters okay now the next question that we will do is take two numbers and print its sum so what I'll do is I'll take two numbers the two numbers are on the input so let's do this ID number number two let's take the two numbers number and number two now I'll say Co of num1 Plus num2 or maybe you can do it something like IND num3 equal to num1 Plus num2 I can say let's print num three perfect model up 5 plus x 11 will be printed imagine this addition operation you have to do it multiple times imagine you're writing a program for calculator a lot of times addition operator will be done can you always just keep on writing plus plus plus plus no that's when you have to modularize the code to make it readable so one thing you can do is blank you can say okay what is this doing I'm giving two numbers and I'm saying sum it will always have two numbers to take so if you're trying to write a return function it will always be like integer sum again the name of the function can be anything it always takes two number so there can be two parameters there can be any number of parameters multiple data types you can have something like car c as well that is okay number two this is number three equal to num1 Plus num2 and you can say return num3 and over here what you can say is I've taken the inputs and I can say the result will be I'll say the sum guy to take num1 and to take num2 so the sum guy is called so what happens let's understand the line of executions so what happens let's understand the execution so the means is okay num1 number take it as input five and six five goes to num1 6 goes to number two and then I call sum this is where the program stops the program stops at the line 21. program stops at line 21 and now it goes to line 13 at 13 it is taking num1 which is five it is taking num2 which is six so five and six are passed over here and it states five plus six and then num3 takes five plus six which is 11 and it says return return the value so it returns eleven and this function now this function gives you 11 because this function was for this function gave you 11 that 11 goes and gets stored into rest and then the line 21 which was stopped will be executed and then this line 22 will be performed what is the value of rest now it is 11 so it gets printed so whenever you're calling a function first the function will be executed whatever value the function gives if it is a return that will be stored here then the remaining lines will be executed on a function called the flow will go to the function the flow will go to the function got it this is what you call as a return function this will return something you could have done it as a void function void parameters and you could have done C out now that is also okay in that case you don't have to do this and you don't have to do this now different ways of writing code a lot of people write code in different ways you can try it as you wish my main idea is to teach you what are functions what different kind of functions you can have now remember one thing if you are writing as this and this this can be anything this can be an integer can be a double can be a long long can be a string can be an array can be a vector can be a list can be anything can be anything and you will see eventually in the DSA playlist We will be writing functions so I will be returning vectors arrays 2D arrays there'll be a lot of things returned from here and we will learn throughout the course so please make sure you watch all the videos of this playlist because I'm going to go in super depth since we are talking about functions let's take these couple of numbers again and there are a lot of internal functions remember at the start I said there is something as math dot h so there is an inbuilt function which says give me minimum and if you write Main and you give the num1 num2 and then you uh print minimum see whatever the five is the minimum similarly there is Max as well there are a lot of inbuilt functions as well these functions are similarly written so if you have to write a Max function how will you write it'll be very simple it will be like num1 num2 and I know if I'm looking for max if number additional statement greater than num2 I can say that my Max is num one so right or else I can say is my Max is num2 now there is something you cannot name the function uh with the same name so make it Max Max okay so because of inbuilt Max function exists so you have to give the function such a name that the inbuilt there is no such inbuilt functions there cannot be repetition of function names okay so max now will it work it should because data type returns returns returns let's see it does return okay now for an example let's omit this slide for example just for an instance let's omit the slide this is an integer this has a return state will it work no it did it did throw some random value why I did this up can you please tell me why did this happen the reason this happened was let's understand one thing over here what did I say I said number one greater than equal to number two so five was greater than equal to 6 no so there was no if since this if did not execute Itself by the way since there is one line you can always write it without presence since this if was not executed because I was stating is 5 greater than equal to 6 and that is false so the if never got executed and since the if did not get executed the return line was not executed and since the return line was not executed this function was looking for a written line it was looking for a return line and he did not find one thereby did give you like garbage Stuffs the program did not compile properly it's very important that if you are writing an end data type stating that it will always have a return you make sure you write a return you make sure you write a return has very very important every function which with a return data type must have a return one right make sure you do that so we did learn about functions but in the bracket it is written pass by reference and value let's understand what that means so let's just take a number just in order to understand and do something and we write a function do something and pass the number okay and we go over here and write void you know this is a function so right do something and to take this number and maybe print it and go ahead change this number let's say add 5 to it again printed and maybe again uh add a 5 to it and again print it so what will be the expected value I'm sending 10 so 10 will be printed and plus 5 15 then 20 so ideally it should be 10 15 20 it is but what did I say about the execution how does the execution works so it is like the first line is executed then it calls it so it goes here this is executed this is executed this is then this then this and then the line 21 then the 22 then ends at 23. so ideally if I printed here what should happen ideally according to your number was 10 it went here 10 15 20 finished 21 at 21 it should be again 20 because the last value was 20 so it should be 20 right that's true we run this 10 15 20 20 till here at the moment it came to the line 21 the value was 10 again why this is what we call as pass by value what happens is when you call functions and you pass number it goes by value a copy goes then the original number did not go the original number did not go this original number must be somewhere must be somewhere in the memory that original memory did not go what it created was it said you are 10 let me create a copy send that copy I'm not sending original I'm not sending origin this guy took the original took the origin he said you're 10 copy copy take the copy and give it to the function and the function took the copy did everything with the copy did everything with the copy so I changed he modified the copy so hence he modified the copy the original value still State the same this is what we call as pass by value okay this does work with everything like let's assign string string s equal to Raj and imagine you pass the string s and let's omit this and let's say string s again and over here assume you say s 0 equal to instead of Raj is it touch T and I print the string s and away also I print the string s sidely over here if I'm changing S 0 to T Raj will become touch right and then when it comes back s is again printed so what should happen yes first it got changed but since I did not pass the original I passed the copy since I passed the copy what happened was only the copy got changed and the original still stayed the same this is what we call as pass by value now there's something else passed by reference what is this pass by reference I don't want to pass a cup I want my function to make changes on the original one that's when you just attach and to it if you attach an and to it it takes the address it takes the address so whatever you are sending over here it takes the original and sends it so this string whenever you say s it actually is dealing with the original let's now compile and see so if your compile and see you see it changed here and the same prevailed over here this is what you call as pass by reference understood if you are passing like this and sign will say that okay get it from the address get it from the address get the original value okay others pass by reference can be written in a lot of other ways but I personally feel the other ways are very very confusing the easiest way is just attach ah and sign and that will make sure that it is passed by reference the original value is pass so if you want to pass the integer similarly you can do integer num equal to 10 and you can pass a num here and let's print the num here and again take int again just pause the num here and you can go ahead and say let's do the same thing C out of again you can say C out of num and L again you can say num plus equal to 5 and again you can say out num and L okay so let's see so if I run this what should happen see value got updated in the function three times and then still the same value prevailed very simple now uh thing that you have to keep in the mind imagine you are declaring the array so do you remember declaring the array till now you were declaring the array like five and imagine there are like five values 10 7 12 13. till now all these values you were kind of taking like C and array of 0 t hen had a of one c n array of two c n array of 3 CN array of four this is how you took and eventually you were printing it array of 0 array of one and going on array of to 2 and so on you've just printed it like this right so if you run this you saw all the values were getting printed like you can put up and in order to get a Clarity T over here also you can protect and so if you just put an NL and run so yeah the first three values are printed but is this a good way because there might be 500 numbers what did you learn looping array it's combine yes how many what are you doing 0 1 2 3 4. can I write something like this zero I I know I have to go till 4 okay I have to do this and if I know this can I see in array of a can I do this [Music] can I do this what will happen what's the first value of I zero so zero array of 0 will get so 5 will get then what's the value of I 1 to 10 will get in Array of one right and then what's the value of I so if you try to print the value of I if you remember for loops foreign the value of I is zero so array of 0 is taken next the value of I is 1 array of 1st x value of I is 2 so add F2 is taken and similarly it takes everything perfect if you are printed quite simple same way run a for Loop you can print everything array 0 array1 why do you want to write everything again and again just write C out give like this and maybe put up space and just see everything got printed so I combined array loops and everything now a question comes pass by ref I said include and do everything but this doesn't work for arrays arrays are generically passed by reference so if you write this this is I how you pass an array so imagine this is the array I took as an input and I said do something and I said can you please take this array and maybe the size of the array and the size of the array is five so you can go ahead and maybe declare it in a better way this [Music] is the better way and maybe you can Loop till n minus 1 or you can Loop till n that is your choice you can do whatever you wish to you can Loop till n minus 1 because last index is n minus 1 or you can write less than both the conditions are meaning the exact same thing so over here I'll Take Everything now what I'll do is I will go across to every value of or maybe to just understand area of 0 I will go ahead and add plus equal to maybe 100 and over here I'll print array of uh value inside function okay and I'll say array of 0 please get printed okay and I'll say value inside into me after the function okay value inside into main let's see if that got added do you see this stays 105. I didn't pass it with reference but array always goes with ref so please keep this in mind if you're passing an array it will always always go with reference but apart from that Vector Maps list everything you have to pass that and sign to make sure that it has passed by reference got this so whatever you do over here with the arrays that will be uh definitely modified throughout so with this we learn functions passed by reference and value time complexity is the next one which I'll be covering in the next video because time complexity in itself is a very very vast topic and I want you to understand this in proper depth with a lot of examples why because this is going to be the most important topics if you go for interviews they'll be like do it in the least time complexity so we'll be doing that in the next one and if you want to know uh more about C plus plus you can definitely watch out the C plus plus STL video so if you have seen the video till here I think you should definitely hit that like button and if you've understood everything uh please do consider subscribing to us because I was going through our Channel statistics nearly 50 of the users who watch these kind of videos do not hit that subscribe button I know these are educational videos but uh if if you just click that subscribe button that does motivate me to make such kind of videos and yes this is going to be a full-fledged playlist on data structures and algorithms so do stay tuned follow me on Twitter Instagram LinkedIn so that you stay updated about this particular playlist and make sure you watch all the videos in this playlist in case you haven't checked out uh other resources you can definitely check out by diving deep into the playlist section of this particular Channel and you can also go through our take you forward website with this I'll be wrapping up this video let's read in some of the video till then spoken don't ever forget your golden I