Full transcript of “Flow Chart and Pseudocode”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.
0:15 in the last lecture we had concluded with a term called algorithm and we said that an algorithm means a sequence of steps that are followed to solve a problem to be followed to solve a problem now the question is that how do we express an algorithm how do we express
1:05 the sequence of steps how do we express that there can be different ways of expressing it right so usually ultimately we will have to write a program for this ultimately we will have to write a program for this so obviously program is the final form of expression of the sequence of steps that we want to reach to but even before that i mean a program is for communicating the sequence of steps to a computer but even for our human exchange we may like to express what are the steps to be followed there are two distinct ah ways by which an algorithm can be expressed one is flowchart another is pseudo code
2:03 now a flowchart as the name implies is a diagrammatic representation of the sequence of steps its a diagram all right and pseudo code on the other hand is an english like english or whatever in human language english like not exactly english we can take a lot of liberty we will see how to when i express ah the sequence of steps english like ah language to express
2:49 the sequence of steps ok now we will show both the flowchart and pseudocode with respect to some of the problems that we had discussed earlier ok so let us start with this finding the average of three numbers this is the simplest possible way simple simplest possible problem that we can solve so we will start with ah
3:35 the first we will start with the start node we are trying to draw flowchart so once we draw this start then next will be sum assign zero now what all of us know that in order to find the average of any number set of numbers we have to first add those numbers so the addition result is stated as sum so here we are when there is no number that has been we have ah already taken into account we initialize the sum to be zero so right now sum is zero right next what we do is we read the first number we read the first number read number
4:25 one now what does this read mean read means who is reading whom do you want to read who ever who ever will be finding the average he or she will have to read the number if i ask a human being to do that the human being will have to know the number so if i show that number on a piece of paper writing five he will read that number five and will say ok so five is the first number then i show the second number seven ok seven is a second number in that way that is the significance that is the meaning of this read now in our case ultimately ultimately the computer will read it and ah earlier um we
5:15 had talked about ah um um the input often we had discussed about this this is also known as an input mechanism so it is reading the number now here i would like to ah state a few things that is um say a flowchart has got some basic fundamental elements this one you have already seen that is a start symbol ok now this figure is known as oblong
6:03 this figure is a rectangle now this oblong is actually used for input and output power i mean specification wherever i need to do some input or the system provides me some output let me explain so input i have already explained that when i am writing some number for you to read or for the anybody will do the computation to read that is the input and you know all of you that we have got some input devices the most standard input device is the keyboard using which we can put in the data the output is after the computation
6:48 is done the system will or system will give me the output or if it be a human ah being who is doing the computation he or she will be telling me the output that is the result so that is known as output so while drawing a flowchart sometimes we will write read for this input and maybe print or write print or right for output ok now this diagram is essentially the computation box so whenever we do some ah computation that we show in this sort of a box for example we can say x is p multiplied by q something like this or ok the other very important block
7:45 is the decision blocks block which looks like this a diamond now here what we do we just take decisions depending on some conditions depending on some conditions i will either take the left path or the right path ok so for example i can say ah whether the age of a boy is greater than fourteen if the age of the boy is not greater than fourteen he is not allowed to go to a film ok so if it be no then he cannot go to to see a particular film and if it be fourteen or greater that means it is greater than fourteen if it is yes then he can go to a film so we
8:36 come to this particular point and make a decision looking at this out of scenario so this is known as the decision box now these are the basic four elements of any flowchart all right so now we will again come back to ah our flow chart so you can see here that sum is zero and the number that has been read is the first number right now suppose that number that was read ah the number that was read is five suppose i the number that i read is five so some number so next what
9:39 i do is i add that particular number with the sum so now if the sum was initially zero so i add five with sum so if becomes the total sum becomes five right sum becomes five so next what should i do after i computed the sum then i read the second number because i have to read three numbers and find the average i read the second number and then i add the second number with the sum the first number was five so the value of sum is now
10:27 five ok value of some is now five i have read the second number suppose that was eight then the sum becomes eight ah plus five that is thirteen now i come and read the third number suppose it is three all right so i read the third number and then i compute the sum so i have computed three the sum to be thirteen plus three sixteen next what do i do i have to compute the average so what i do i find the a v g is by a v g i am depicting the average average is sum divided by three ok so it was what was it thirteen plus three sixteen divided by three
11:17 so it will be five point something so that result whatever i get that i will be printing so thats a flowchart these are the steps that i have to do ok now the same thing the same thing i can express in the form of a pseudocode or english like language let us see how it looks like here you just see what i have done i have initialize i what i wrote in the first step is this one is equivalent to this all right initialize the sum to zero and then i read the first number sum is added sum equals sum plus number one exactly what i did here
12:02 read the second number then i sum equals sum plus number two and then i read the third number and then sum is sum plus number three so these are the steps you see exactly if you just dont look at the left part of the whole thing if you just look at this right part you can also understand what are the steps through which i must go ok and so ultimately i compute the average which is i just write so its english like compute av a v g equal so its not exactly english its english like anybody who knows english will be able to understand this so this is known as the pseudocode
12:49 so this is for the very simple problem that we are doing now suppose lets think off for a second that suppose instead of three numbers i ask you to find the average of ten numbers how would the flow chart look like obviously this same thing that i have done here read number three read number four read number five read number six in that way it will go on and on and on ok had it been hundred numbers there will be even larger ok now can we do something better in order to i dont have so much paper to waste so can i write it can i express it in a much better way so let us go to the next problem where
13:39 we want to find the average of n numbers av average of n numbers i want to find out so the first thing will be again start that is from where i am starting next is read the value of n so what is this value of n now if i say this value of n when i read what is this n this n is telling me how many numbers i will be considering for finding the average is it hundred is it thousand is it ten whatever so i am that is n so i am reading the value
14:28 of n ok now i am initializing the sum to zero again just as before i initialize the sum to zero and then i read now you see here i have written read number i have not written read number one earlier since i had to only do ah number one two and three here there i wrote down number one number two number three here i am just writing read number ok because i do not know how many numbers i will be reading the reason will be very clear soon
15:16 so next what do i do i read the number the sum is sum plus number so i take the sum was zero i take the sum and at that number which number the number that i just now read say i have just now read ah number five ok i have read just now i am sorry i just read number five so i read number five and i have added that number with sum so sum is now five ok next i decrement this is a new thing that is coming
16:04 up here since i know here at this point the the user or the whoever supplied the data told me that you have to look for hundred numbers say n is hundred i have learnt that so now here i have read one number and i have taken care of that number because i have added that with sum and so therefore i now decrement that value of n what does this signify this signifies that i now since initially it was hundred i have to read ninety nine more numbers ninety nine are yet to go right now i have to check have i read all the numbers what is the value of n the value of a n is
16:52 ninety nine that means n is not zero that means i have not been able to read all the numbers if i have since in a decision box as i have told you just a couple of minutes earlier that i can have two options i can either have yes or no answer if n is equal to zero what does that signify if n is equal to zero that signifies that i have read all the numbers i have already read all the numbers then i compute then i can compute the average as sum whatever sum i got divided by n otherwise if n is not equal to zero then lets look at
17:38 here if n is not equal to zero no then i will go back here again look at this part i will go back here again and read number i read that second number read the and add it to sum i will go over here decrement so i have read to number so now how many are yet go ninety eight n is zero no n is not zero ok n is not zero so i go up again i read another number add that particular number to the sum decrement n how many to go ninety seven is it zero no not yet zero so i go on in this way i will go on in this way ultimately after i have read the hundredth
18:28 number this n will become zero so at that point i will come to this average and compute the average as sum divided by n and then i will print the average and stop so see is just by this thing i am sorry just by this thing which is nothing but this is called the loop this is called a loop and using this looping repetition i could reduce the length of the flowchart the repetition of the flowchart in a very elegant way i hope it is clear so the same thing can be also reflected in the flowchart now compared to the with the earlier flowchart earlier um statement here when i read it only for three numbers i read initialize
19:19 sum to zero read number one read number two and at every point i was adding it to sum here what i do is i write it in a little bit flexible way because when i write in a pseudocode i am not very constrained i i can take the flexibility as as long as i can express myself its good enough i just wrote get the value of n earlier probably i wrote read the value of n means the same thing so get the value of n initialize sum to zero and till now here i am talking of this looping till n is as long as till means as long as ah n is greater than zero ah it will better
20:09 to say as long as n is greater than zero read a number at the number read the number that you have read to sum you read a number add the number read to sum ok and one thing i missed out here and you decrement n you decrement n here you have to make n equal to n minus one in your note you please take it down that here it will be n n minus one and so this till n as long as n is greater than zero i will be going on doing this after that i will compute average as average divided by sum average is sum divided by n i am sorry and then i will print the average so i can express it either in the form of a flowchart or in
21:00 the form of a pseudocode ok now let us take a ah ah i mean its not very visible here finding the maximum of three numbers that was another problem that we had discussed last time what i am trying to do is i am trying to find the maximum of three numbers so how can i go about doing the flow chart so i start just three numbers not a large set of numbers i first read the number one now tell me one thing when you are first read one number what is the maximum number obviously since you have read only one number that itself is a maximum only one so it is
21:53 a single ton so it is the maximum so therefore i say that max is nothing but the first number the number that i have read max is number one all right now i read the second number ok i read the second number now which one is the maximum if the second number is greater than the first number then the second number will be the max now when i read the first number when i read the first number here i said that to be the max so now after reading the second number i can compare the second number with
22:38 respect to the max if the second number is greater than the max if the second number is greater than the max then what will happen then number two will be the max ok if it is yes the number to becomes the max and if no that means what number two is not greater than the max then i skip this part i skip this part and go out by this way skipping this part that means at this point who who is the max obviously at this point i had number one and number one is still the max ok i hope its clear
23:25 next i read the third number again i do the same thing after reading the third number is the third number greater than what number two should i compare no i should compare with the max because till now between number one and number two whatever is the maximum i have already remember that in max so i read the number three and then again compare is number three greater than max i can have yes or no so if it be yes then number three becomes the max because number three is greater than the maximum number till now ok and then i will proceed and if not if number three is not greater than max then i will
24:16 then obviously the whatever was the max till now that is the number one that will remain the max ok think of number one to be five number two to be three and number three to be one what will happen three so max is three ah sorry this was five so i read five ok next i read three is five greater than three no so i kept five as the maximum all right i read number three that was one i compared is it greater than the maximum which was five no then i print that five which is the maximum but if suppose number three was seven so it
25:05 is greater than five so then i can go here and update this max to be number three or the seven and then i come to the print max part so this is a flowchart of finding the maximum of three numbers and consequently you can see the pseudo code of this start read first number then i assign max to be the first number then i read the second number assigned now if number read is greater than max then i will make the max to be number two ok if the number read if the if ah its badly written read number it is wrong it if number two if number two is
25:57 greater than max then make max number two all right then read number three if number three is greater than max then min make max number three otherwise you are continuing so if number two is not greater than max then i am reading number three i am not doing this part ok this is another way of explaining the pseudocode so the same thing now we come to ah another variety of this finding the maximum for n positive int n positive integers here i have taken a little liberty the finding the maximum of n positive integers earlier i was doing it for all numbers but just as a change so lets see i read n again n means
26:45 the number of numbers that i want to read now i set max to zero why do i set max to zero because i am trying to find the max of n positive integers so any positive integers will be greater than or equal to greater than zero so i am putting that max to the minimum value possible and then i am reading a number i read a number and if that number is greater than max if that number is greater than max which is by default zero i have kept it as the bottom was possibility right in all unless that number is zero it will always be greater so ah i will set that number to be the max next again you remember what i did for finding
27:35 the average i reduce the value of n suppose i had to do find the max of ten numbers so n becomes now nine and then i check all right hum i have the max sorry this is a mistake here this arrow this arrow should come from this point this arrow should come from this point so i am just drawing it here ah um i am drawing it here it should come like this um so i have
28:28 read a particular number and then if that number if that particular number is ah greater than max i am sorry i am sorry i read a number and if that number is greater than max number is greater than max then i will if it is true then i will make that number
29:15 to be the max right max will be that particular number and then i check n i have already read n so is n zero then i i have read one number and then i reduce n just as i did in the earlier case and then i check any n greater than zero if yes then i have to go back i have to go back and again read a number right read number and this will go on as long as
30:17 n is greater than zero if n is greater than is not greater than zero no then i will come and print the max right so there was a little error in that diagram which ah you will be able so you can see that this line should have come from here here is n zero yes then print max otherwise this should have come from this point all right this should have come from this point this is wrong ok so in this way in this way we can again find the max for n numbers its a flowchart so again i can have the pseudocode for this so a pseudocode for this is shown
31:10 here read the value of n here initialize max to zero till and till n is zero read the number if number is greater than max max is equal to number decrement n n is n minus one and then it will be not one here it will be again print the max so in that way you have seen examples of some intermediate representation of ah the sequence of steps or the algorithms will encounter a number of algorithms in the course of these lectures and um the next point next step we will see what is to be done in order that
31:58 this sequence of steps can be converted can be ah transferred to a computer language so that and can be ah transferred to a computer so the computer can solve the problem that we will see in the next part