Transcript for:
Understanding Flowcharts and Pseudocode

Hello ji, How are you all? This is Love Babbar and welcome to the channel CodeHelp We are starting the DSA course with today's lecture We are going to discuss many things here Today we will learn about flow charts, pseudocode and programming languages And why do we use them If you have already done these things, then you can wait for the upcoming videos But if you are new to programming, then take your copy and pen and let's get started Getting started Now lets talk about the intent of the course If you get any question in the online test or in interview if the question is from DSA, then you should be able to easily solve it You should be able to provide optimal solution to that problem And to do so, we need to work on your problem solving skills we need to improve it Problem solving skill or we can say thought process to solve a problem How will we approach a problem First we will understand the problem I will explain the problem first Then we will discuss, what is given to us 3rd - we will try to think about the approach to solve the problem Can we solve this question by dividing it into different sections Basically we will try to find an approach to solve it After this, we will write our program This will be our flow whenever we will solve any problem First question, then given values, then approach, and at last we will code it Got it? Fine How will I write it, let's see that This is my problem and this is the required solution First I will write a rough solution for it - We call it pseudocode or flowchart We will just get a rough solution in our mind We call it flowchart or pseudocode We will study about them too Then we have to write a code for it How to do that? Will tell that soon So we will code in some high level language But computer won't understand the code which we have written Computer only knows 0-1 It don't know anything apart from 0-1 So we have written a code, which computer won't understand So first we have to convert this program into a machine understandable format If I will do so, then my program is executable If you have made program to add a+b and you want to print the answer, then first you have to convert it into machine understandable language First question, then given values, then approach and at last we will code it Then we need to convert this code into machine understandable language, and how we do that? Will answer soon We call this program, source code too Got it? Bhaiya what is this flow chart? Now what is this flowchart It is basically diagramatic representation of an approach First you wrote an approach to solve a question then the diagramatic approach of that approach is known as flow chart Which will constitute various components, connected with each other Bhaiya what type of components? Will tell So now you know what is a flowchart Let's show you an example First, I will start with the various components First we should know that from where a program is starting For eg, we know that in any book, we start reading from top left So we should know, where a program starts So here in coding too, we have a component called terminator, to show the start of the program It is used to show the start and end start of the program Got it?. Will show you a flowchart also, but first let's understand all the components We have an another block, in the shape of a parallelogram It is used for input and output When you have to take any input from the user or print any output, that time we use this block Next block is a simple rectangle, which shows a process We you can call it calculation too If you didn't get it? Let me make it clear Terminator is start/end In input, you can read 'a' and in output, you can have something like this, print "hello" Now what is this process, i = i+1, this is a process Done? got it? Now let's move on the decision making Or diamond shaped block here you mention a condition For eg - is i equals to 0? You always get only 2 outputs of this, yes or no For eg - I can say that if answer is 'Yes', then end the program, and if answer is NO, then print 'i !=0' Got it? So this was the decision making block Now let's move on to the next one These are my arrows Why we use them? These are used to show the flow, and to connect different different components Here we have already used this arrow, we have shown the flow of the program with the help of this arrow here Here we have connected this block with this with the help of arrow, so this is how it connects components We have a circle too, known as connector We will see it later when we will study functions Let's summarize Terminator is used to show the start/end We have input output block to show the input taken or output given We use rectangular block to show the processing We have this rhomboid shaped block to show the decision making We use arrow for connecting components or to show the flow And we have this another connecter which we will see later Now you have got the basic idea of the components of the flow chart, now let's see a flowchart here Ok, let's make a flowchart for the sum of 2 numbers I will give you 2 numbers - a and b, and you have to print the sum of them From where our program will start? So for that, we will make a start block I made this block and wrote 'start' So my program is starting from here And what we have to do after starting? I have to take input first Input means - a and b So I wrote, read a,b When I wrote, read a, b, it means that in memory two distinct blocks are assigned the name as a and b and then we have given the value to that block as 5 and 10 now we have to find their sum, how we will do that We have to add that, so what is this addition? It is a process So to show this process, I made a rectangular block and wrote, sum = a+b Done? Now we have our sum with us Now I also want to print it and you already know that we use parallelogram to show the input/output I wrote, print 'sum' Now I have printed here = 15 Now we also have to end this program somewhere So end it, I have used this terminator again, to end it so this is a flowchart Let's understand once again We started a program to add 2 numbers, then we took the input of that numbers then we did their sum, printed the sum and then we ended the program Got it? right? We are going to see many flowcharts ahead Bhaiya, first tell me about the pseudocode, then we will see the other flowcharts Okay fine, we will see the pseudocode first Pseudo means rough, code means code So pseudocode as a whole is a generic way of representing logic Every language has different syntax or way of writing the code, but pseudocode is the generic way of representing our logic If you are given with the pseudocode, then you can code it in any language you want Syntax might be different in different languages, but the flow is always same So pseudocode is a generic way It is very easy to understand, just like simple mathematical statements written in English suppose I have to write the pseudocode for the above question then how will I do that I will say - Read 2 numbers, a and b next line, sum = a + b 3rd line - print sum. This is a pseudocode Keep on thing in mind, no pseudocode is wrong You can write it like this too. Read a, then read b then sum = 0 sum = a+b and then, print sum Bhaiya is this wrong? No Will I get my answer with this Yes sure. It will give the same output as the above pseudocode So pseudocode is a generic way of presenting logic So you can write it something like this Now you have basic idea of flowchart and pseudocode Now let's do 1 question more, then you will understand these concepts clearly Next question is We want to make a flowchart for calculating simple interest This is the formula for simple interest You must have studied it in your school We started our program from this start block I don't know p, r, t, so I need to take them as inputs first So I asked user to give me the values for p, r, t So now I have P, R and T So I made this rectangular block for the calculation part Here I wrote this formula After finding this answer in the above calculation, I have to print the output too So here I will print the output with this block print SI and then we will end this program So this was the simple flowchart for this question. Simple, right? I started the program, read the input p,r,t I took these values to put in the formula, then I calculated the answer You are enjoying it, right? Bhaiya, give 1 more example. Okay let's see 1 more example Okay, the question is, we have to find the average of 3 numbers find this Suppose we have 3 numbers a, b, c we will calculate the average like this First we understood the question, then we checked what is given in the input Now we will think about the approach Approach is simple in this case because we simply have to put values in this formula Done question Now we will make Flowchart. First let's make the start block Now in next block we will take the input I wrote read a, b, c I know that we calculate average like this So I wrote this formula inside this rectangular block as a part of my calculation A block named average will be created in memory and avg will be story in it and then I will simply print the output using this input/output block and then made the end block So see, how simple it is So we have created another flowchart here Let's do 1 more question Now let's increase the level. Tell me whether a is less than b or not Print yes, if a is less than b, else print B You will say that it is very easy You know that here a and b is your input which user will provide and in output I simply have to print either "yes" or "no" You got this much First you started with a start block Then you took the input. Read a,b Now you have to check whether a is less than b or not How we check conditions ? I told you earlier that we have one block, named decision making block which helps in checking conditions A<B, this is a condition which we have to check in this case So I will write is A<B inside this decision making block you know that it gives 2 outputs First one is "YES" 2nd is "NO" It means that when A is less than B, then flow will be on this side, and we will print "YES" that time So we made one more block here to print the output and if this condition is false, then we have to print "NO" You are getting it, right? Bhaiya how to end this program in this case We will make this end block here and attached the flow from both output block to this end block This is our flowchart Got it, right? Let's look at it again We started the program, took a, b as input, then we checked whether a is less than b If a is 2 and b is 5, then this condition is true, so flow will go on right side It printed YES, and program ended here suppose a is 8, then this condition will be false, and it will print NO and our program ended here You are enjoying it right? So we have understood this flowchart also, right? Bhaiya, write pseudocode for this flowchart let's do it First we read a, b then we checked a condition, if a<b print "YES" else print "no" and its done Simple You just have to write in english what you are thinking and it is called pseudocode Someone else's pseudocode might be different than this, But both are correct if they are giving same answer for every input Now let's move on to the next question Now let's check whether a number is ODD or EVEN Let's n=3. We know that it is ODD, but how to do this programmatically If any number has 2 as its factor, then it is called even number, right? if 2 is not its factor, then it is a ODD number Here we have one Modulus(%) operator. It gives us remainder How? If you will do 5%3, you will get 2 as your remainder If you will do 6 modulus 2, you will get 0 as your remainder So in short, if n%2 is 0, then that number is even, if n%2==1, then it is a ODD number For eg = 6%2 is 0, so it is a even number and 7%2 is 1, so it is an ODD number This is our logic, this is how we have to solve it N is our input here We have to check, whether it is ODD or EVEN Let' create the flowchart Let's start with the start block then we will take the input Read N, now we have N Now we have to check a condition that whether N is ODD or EVEN, so we will make one block for this condition Here is the decision making block, Is N%2 = 0? We will get 2 outputs, YES or NO If output is YES, then it is an EVEN number, so I printed, EVEN Else, I will print ODD Then I made this end block and ended this program something like this This is the flowchart for EVEN, ODD Got it right? Pseudocode is very simple for this First read, then check if N%2 is 0, print "YES", else print "NO" Pseudocode is similar to the one we wrote above Now let's move on to the next question, where you have to tell whether a number is positive, negative or zero You know that input, output now You started with this START block , then you took the input, Read N Now you have to check whether it is Positive, negative or zero So I wrote, is n>0 If it is true, then I will print "+ve" here If this condition is not true, then it may be 0 or -ve so let's check again, is N<0? If it is true, then print "-ve" Else we will print "0" Bhaiya, why are you putting this inverting commas again and again, you will come to know soon So printed all the 3 outputs But we have to end it too So we made a end state, and attached all these flows with it Got it, right? Now let's move on to the next question You have to check that whether the given triangle is valid or not A triangle is valid if A+B > C and B+C >A and C+A > B If all these conditions are getting followed, then a rectangle is valid, else not This question is your homework If you have understood the above question, then you can do this one too You have to do this question, okay? Next we will talk about Loops Let's say, I gave you a number N, and I asked you all numbers from 1 to N Suppose I gave you 5 as input, then you have to print, 1,2,3,4,5 Got it right? How we will do that, let's see First we will make a simple logic, then we will move ahead You know how we start a program, using stat block Then we took a input, read N Now we have to print something and then increment too You have to Print, and then increase the printed number too You can see here, we printed and then increased the numbe by 1 But we don't know how to do it How we will increase again and again Let's rub it You know that we have to start from 1 and print till 5 So if I give you a number which starts from 1 and stops at 5, will it work? Your are like, yes, this is what we needed So I took a number =1 This is not a input, it is a process, so I wrote it inside rectangular block and Now I wrote, check whether it is less than N If YES? then print this Number And after printing, increase this number by 1 Let's understand again, n is 5, then we took num = 1 Then we checked whether num is less than N, If YES, then we printed the number and incremented it by 1 Now number = 2 So we to print 2,3,4,5 also, we have to repeat these steps again and again When we have to do something again and again, we use loops that time You will again go here from here Now let' see what happened here 1 is less than 5, you print the number, and then incremented it to 2, and then again moved here and checked, is 2>5 It is true, so printed 2 and incremented number to 3, again moved above checked condition and printed 3 and incremented to 4 Then checked 4<5, printed 4 and incremented 4 to 5 5 less than 5? No, but we have to print 5, so we will write num less than equal to 5 Now it will print 5, increment to 6 This time 6 is not less than 5, It means we have to stop now So simply wrote, end block here You started from here You took a input Then you started from 1 and moved till N You checked is my number less than N, if yes, print the number and increment it and then we follow these conditions again and again, till the time this condition holds true Once thsi condition becomes false, you program will end Bhaiya, I didn't understand it so clearly No worries, you will get it soon Let's do 1 more question Now let's print all even numbers from 1 to N It means if N is given as 5, you have to print 2 ,4 How will we do that? It is very simple We will start from 2 always, because we know it is the first even number So I took a number which is 2 in the beginning and then I will keep on incrementing it by 2 Then check whether it is less than N, if yes print it, and increment it again by 2 and repeat these steps till N You have to print all the even numbers till N if n is 9, then we have to print 2,4,6,8 Got this, right? Now let's create the flowchart once First we will start, and take input Now I can increase my pace because you have started understanding things After taking input, we initialized a number with 2 because we are going to start with 2 as it is the first even number We checked, is 2 less than N Is 2 less than N If No, then simply end the program here If it is YES, then we will print the number and then we will increment it by 2, means, number = number+2 Then we will check the same condition again after incrementing And when we check the same condition again and again, we call it looping You will enjoy these loops a lot, Now let's dry run our example once again This is our flowchart, Now let's run it for N =5 So, now this 5 gets stored in a memory block and let's name it as N The block in which 2 gets stored, I can call it as number Is number < N? Yes or no? Yes!! So print number The final output that we get is 2 number = number + 2, so now our number becomes equal to 4 Now you check is number < N? i.e. 4 < 5 Yes! it is, moving downward, print that number which is 4 Again, when you add 2 into it, your number becomes equal to 6 Moving upward, you check is 6 < 5? The condition fails here, and our program ends here simply This is how we have to do these questions You have to print all the odd numbers from 1 to N as a part of your homework The catch is, 1 and N are inclusive here In the above example N was not included, but here in this question it is inclusive, so solve this question as your homework The solution is easy, you just have to use less than equal to instead of less than Try this once Let's move on to the next question Find sum from 1 to N inclusive N If N is 5 then sum = 1+2+3+4+5 so the answer should be 15 Let's think how we will do it We know that we have to start with num=1 and increment it till N I know N = 5, Num =1, and sum =0 in the beginning Then I added number in the sum and incremented the number by 1 and then again did the same thing over and over again In the beginning, number is 1, sum is 0, Then we added number to sum and incremented it, so our sum is 1 now and number is 2 and we kep on doing so, till number<=5 Now let's try to solve it Start -> Read input -> Initialized a number with 1 Then we checked the condition we wrote is number<=N (because N is inclusive) I have used less than equal to instead of just les than symbol because N is also inclusive here If we will get NO, then the program will end If we get YES, then we know that number is smaller than N, so we have to add it to our sum and make number = number+1 and we will keep on repeating this There is a mistake here. We haven't printed sum here. We have to print the sum before ending the program So this was the entire flow of summing all the numbers from 1 to N Start -> read input -> initialize number = 1 -> checked Is number<=N Sorry, we forgot to initialize the sum here, sum = 0 So we added number to sum and then incremented it Got it right? let's dry run it again for n=3 Desired output is 6 n = 3, sum =0, number =1 Number <=3, sum=1, number = 2 2 <=3? True, sum = 3, number = 3 This way you will get your answer as 6 Bhiaya, Homework Okay, homework question is, Find factorial of N 5 factorial is 5 X 4X3X2X1 = 120 You have to solve this question Now let's move on to the next question This is going to be our last flow chart. You will get your homework after this You have to check whether the given number is a prime number or not Prime numbers are those numbers which have only 2 factors, 1 and that number itself, for eg 2,3,5,7 are prime numbers but 8 is not If you have to check whether a number is prime or not, then you need to be sure that this number is not getting divided by any number from 1 to N It should not give remainder 0 with any number from 1 to N If rem is 0, then it is not a prime number For any number to be a prime number, we have to make sure that for every number from 2 to n-1, modules should not be equal to 0 For eg - if N is 5, 5%2 is 1, 5%3 is 2, 5%4, is 1, so no number is giving remainder as 0, so it is a prime number So divide N by every number from 2 to n-1, and it should not give remainder as 0 Now let's create the flowchart for this Start -> Read N I told you that we have to divide it with every number from 2 to N-1, This time I initialized num=2, because we have start dividing from 2 We know that largest num is n-1, so our condition will be num< N If condition is true, then divide it by N and check its remainder, if it is giving 0, then it is not a prime number, so print NO But if it is not equal to 0, then we have to move on to the next number How we will move to the next number? Yes, we have to increment it, so num = num+1 Now I have to again check, num < N Sorry, num should be less than N, not 2 If num is greater than equal to N, then it means we have to stop If you have reached till here after checking many remainders, it means you haven't got any remainder as 0 till N, so this number is a prime number Then we made a end state and connected both print statements with it We started , read input, initialized num with 2 because 1 divides every number, so for prime numbers we have to start checking from 2 If N is 5, only 1 and 5 will divide it, so it is a prime number, Incase of n=6, 1 and 6 will divide 6, but this time, 2 and 3 are also dividing 6, so it is not a prime number Now let's dry run our flowchart for N=5 So now you know why we started from N=2, First you checked from N=2, and if it didn't divide, you kept on incrementing it till it is less than N If on its way, it gives 0 as remainder, then it is not a prime number, but if nothing this happens till N, then it is a prime number So this was all about the flowcharts I will provide this whole sheet in the description, you can use this as your notes You will get your homework questions too You understood flowchart, how to add conditions, loops, input/output, processing, start/end, initializing a number We also learnt how to write pseudocode Now let's talk about programming languages We will answer only 2 questions, What is it? and why we use it Then we will end this lecture Take 2 mins break, then we will continue Now let's start with programming languages. I will just give an overview today I am not going into its depth What is a programming language? If I want to have food, I will say my mother to give me food. Similarly, If I want to ask computer to do a task, I need to use some sort of programming language to instruct it So I need to write a program or code using a programming language Every language have some semantics or rules which we need to follow Just like we use grammar, sentence framing etc in English, we also have some sort of rules in programming languages Now once I have written a code, let's say in C++, will computer be able to run it? NO Why? Because computer won't understand what we have written If I will talk in Hindi to a french person, will he able to understand me? NO First I need to convert my HIndi to French, then he will understand it So there should be some intermediatory body which will convert our code intro machine understandable code Computer only knows 01 language or binary language So I want that some interpreter should convert my source code into binary code So compiler is that interpreter which we use to complete this task Got it? So compiler converts our source code into the binary language Once we are ready with our binary language file, or exe file (executable file), then our computer will be able to run it Programming language is something which helps me in assigning task to computers But computer can't directly understand our code So we need a compiler to convert that code into machine understandable code, so that it can be executed So this was, what is a programming language and why do we need it So we learnt about flowcharts in this lecture, pseudocode, took an overview of programming languages As a homework, you have to write the pseudocode for all the questions we have solved today And also tell me in comments, how was this first lecture, Did you enjoy or you got bore? Also tell me, are you waiting for the next lecture? Also gives answer to the homework questions, along with your gmail id, so that I can send something to top 3 people Next lecture will be out tomorrow, most probably Where we will be writing our first C++ program - Namaste Duniya (Hello world) So this was for the today's video. Will see you in the next one