Transcript for:
Guide to Algorithm Design and Problem Solving

Hello everyone, so welcome back to this channel. So in this video, we are going to look into the second part of the IGCSE computer science syllabus, which is the practical part, in which we will learn about algorithms, programming databases, and logic games. But in this video alone, we are just going to focus on chapter 7, algorithm design and problem solving, which hopefully will serve as a really good foundation for you when you learn about programming.

So let's look into the first subtopic of this chapter, which is the program development lifecycle. So it is basically a process that developers follow when creating a new program, such as for all the apps that you look into app stores, they might not be using exactly the same cycle. But the essence of it is that they all go through this similar process.

So the first step is always the analysis part. and followed by how they design it and after all the planning has been done the programmers will start coding the applications for different functions and after all the coding stuff that have been done they'll proceed to the testing and eventually they will launch it so here i have some applications here such as the powerpoint app that i'm using to present my teaching the zoom application ai from adobe and also the game that you play such as fortnite they are all part of software all right so in this syllabus you'll be required to do a unseen scenario question and your paper to exam so this is just one example of what you have to do at the end of this syllabus so they will throw you a question and you have to code it out using either pseudocode or any programming language that you teacher has taught you let's not move too far so let's look at each of going to each of these stage and see what they actually mean before we start looking into the more practical stuff such as the flowchart and the pseudocode because by knowing this process you'll have a good big picture on how software is being developed and after that you will have a better understanding when learning the individual components of designing a software all right So the first step of the PDLC is analysis. First and foremost, before you start coding anything, designing anything, you will need to understand what is the requirement specification, which means otherwise they talk about what does this program aims to achieve.

All right, so problem needs to be clearly defined and set out so anyone working on the solution understand what is needed. So for instance, For all the applications that I show here, Facebook, Google, Zoom, before the founder starts to code all of this software out, they need to have a brief idea on what this app is going to achieve. For instance, the Facebook application, I believe that Mark Zuckerberg had the idea in the beginning that this app is going to be able to connect different people around the world.

So he would need to include features such as the chatting function, the posting function, the ability to like a post by your friends. So but before he quotes all these things out, you need to understand what this app is about and what problem it is going to solve. And there are another example is the Google app search engine.

What do they plan to achieve? They plan to achieve that everyone when they use the app, they can ask any questions and get the answers. And the third example is the Zoom application.

It's pretty obvious everyone uses it nowadays, is that they want to create a video conferencing app. So having the requirements set out is extremely important. I would say it's more important than coding the program because if you don't know what problem you are trying to solve, then you will be pretty clueless when you are coding the program out.

So there are two steps of analysis. Basically the two steps that programmers carry out, they don't always call these as decomposition but they do it maybe without knowing the name or they know it. So these are the two steps of analysis that help programmers to understand better what they want to build.

So the first step is decomposition. Decomposition breaks down a complex problem into smaller parts which can be divided into even smaller parts that can be solved easily. So one real life example of decomposition is that, let's say you want to organize a hangout with your friends, then Instead of just saying, okay, we want to do a hangout, you will need to plan a few things, right? A few tasks, such as you need a confirmation of who is going. And after the fact has been confirmed, you need to find a place to hang out and arrange in transports.

So these are all little problems that connected to your objective to organize a hangout. So similarly in programming. an app you will need to figure out what are the smaller part of the program so for instance when you're using the facebook app there are multiple part of the program such as the marketplace which allows buyers and sellers to do transaction that's also the personal peak feed where you can see some more personal stuff from your friends and family and these are just the different components an app might constitute so the second part of analysis is the abstraction phase So let's just read out the definition. It keeps the element required for the solution to the problem and discard any unnecessary details and information that is not required.

So an example, again, using the same example of extraction is that before you, when you organize a hangout, the first most important task to confirm is that you need to know who is going to the hangout before you actually find a place because you want to know who will be going before you decide where to go because the place that you decide might not be for everyone okay so before knowing if anyone is going the details of where to hang out can be temporarily abstracted you just have a brief idea of where to go all right i know this idea sounds pretty abstract so i provide another example here so for instance when i teach this lesson i need to do some planning in the beginning of this video i just said that we're gonna do program development lifecycle without giving you any many details on what this cycle is all about and as the video proceeds i start to explain that there are four steps of the analysis process first steps of the padlc process sorry and analysis design coding and iterative testing and as i explain each section to unveil more details that i haven't talked about such as what is the requirement specification means what is abstraction, what is decomposition. So this is one way I abstract this lesson. So instead of throwing you all these details in the beginning, I abstracted them so that I can explain my subject in a better way. And this is how it works, abstraction. We keep the details that are unnecessary temporarily and review it side by side.

So for abstraction in web development, you, for instance, when you are developing an app, you don't want to care too much about the details first, you just want to have the skeleton, the big picture. So this is how abstraction is useful in software development. So the second part of PDLC is the design part. In this stage, developers of the program will continue the decomposition phase from analysis and begin to draw up visual of the end products. So this is where the programmer start doing some work before coding it.

There are three methods that we learn to design our program in this syllabus, in which I will talk about them in the later part of this video. First of all is structure diagram. They basically help us to visualize how the big, how the angles of the app is like, what are some of the parts of the app, and followed by flowchart and pseudocode. So we're going to talk about that in chapter 7.2. And after the design part has been done, the programmers will proceed to coding by using the stuff that we designed previously like they will be coding using the pseudocode that i written and even also the flowchart and would so this is the part where the programmers work on the design module such as the input output and processes and write code to make each part of the program functional i know this sounds pretty abstract for please be patient with me i'll go through each step one by one so eventually you can code your own program so for coding um there are many parts in in a program there are a lot of parts that needed to be code coded it's not that you write just 1000 codes of line 10 then all these things will come up because let's look at this example called from from the shoppe website which is a very famous e-commerce website in asia so there But even in this small snapshot, there are multiple functions, priorities, application.

So first of all, the programmers need to write a program that update the sole count automatically. so whenever a user purchase a product here the program will need to update this from 4 to 5 automatically so that's one thing the programmer need to program and followed by the login and sign up page if you were to click this button you'll be brought to another page and that's another software to be written and even the notification center so the search bar how to help the user to Select the most relevant product using the searches and an established app like Amazon, Facebook, any app that you think they are established enough. They usually have millions and millions of lines of code to just do the different part, small part of this program.

All right. But however, we will only do coding in chapter eight. So feel free to comment in the bottom.

If you. want to learn about it, so I will make some videos to explain. Alright, in chapter 8. So after all the coding have been done, the programmers will proceed to testing their programs. So the test strategy is that the system will be divided into smaller sections to be tested for input, output and validation rule. So test data is used for all those tests to be carried out.

We'll be talking more about testing in chapter 7.6. Just remember that at this phase of the video, I'm just trying to give you the big picture of what this chapter is about. And there are two types of testing, unit testing, in which we test each part of the code individually.

So for instance, in the example that we see, there are many parts of the program, the programmers will test out one by one. And as for integration testing, all codes are combined to form a program, and then the program is tested as a whole. So an example is that... We test that when an item is stored, the count is indeed increased by 1 instead of indeed increased by 2. And this is how the programming programmers test out their programs. Alright, so before I end this small sub-chapter, just a note here is that if you look at the full name of PDLC is Program Development Life Cycle.

This means that this cycle is not a linear process. So from one end to the other end. Instead, it's a cycle, which means that the programmers might start off from the analysis phase and move on to design, test, coding, and testing.

So let me just redraw it to clarify. First step, second, third, fourth. And after the testing is done, the app is launched.

It doesn't mean that this is the end of the job, right? We know that. all the apps they have software updates nowadays which means that after the testing phase the programmers will need to go back to the analysis phase again to figure out what are some of the new features that can be added and then they will repeat the process design the new feature code out the new feature and test new feature and go back to analysis again and this is how all the apps ensures that they can keep evolving keep improving um as an app all right so that's the end of programming development life cycle so in the next part of our videos we're going to talk about how system is divided into subsystem if you remember in the analysis phase of the pdrc process we need to decompose our problem a software problem into smaller one because a software can contain millions of line we don't just want we want a better way to code all this smaller part of the program out So let's look at the definition of computer system. They are made up of software, data, hardware, communication, and people. And each computer system, every computer system, every app can be divided up into a set of subsystems.

And these subsystems can be divided into smaller subsystems and so on. Let me give you an example from one of the apps that I believe teenagers use often is the Instagram app. and how Instagram is actually a computer system. So first of all, it is made up of software, data, hardware, communication, and people.

So the software would be the code that is written in different programming language to code the app, and data would be the user preference. Just a fun fact here is that all the social media apps nowadays, they keep track of what you like, what you have commented, and how long do you watch something to determine what you like so that they can recommend content that you are most likely to watch. So that's the data part of the Instagram app.

So the hardware will be a server to store all the data. and communication and people would be the instagram's employees so that's um first part and second part each computer system can be divided into a set of subsystems if you use the instagram app you'll notice that the app has different functionalities so they have the post functionality they also have the story functionality which is um copy from snapchat and also the view functionality which is an implemented recently it's also copied from the app tick tock all right so if you look at this app they can actually be divided into multiple subsystems and each subsystem can then be further divided into subsystems and so on so um in the instagram story function you can choose to share your stories to either your normal followers or if you um prefer to have some privacy you can also share only to your close so that's another subsystem involved in this application so there are other examples of a computer system like the alarm clock app weather app games so to aspiring programmer here before you want to build something it's better that you all figure out what to build first because if you look at every app there there are they usually consist of many different functions and if you don't try to lay out all this function before you build when you code this program out you you are going to be pretty clueless so these are the components of a computer system which i've already talking talk about so if you were to create a system a software today so you need to think about the following problem how do you divide your system into subsystem so that it works so that the workload is manageable and the second question is that how do i start developing a subsystem using an appropriate programming language. So the tools that we will learn that will help you to do that is for the first question we use a structure diagram and for the second question we use the flowchart or the pseudocode in which we'll start learning it from now on. So let's look at structure diagram.

Structure diagram they show top-down design in the diagrammatic form so if you look here they basically show how a solution can be subdivided into a subsystem which each level giving a more detailed breakdown so just imagine this problem here is a huge problem let's say i want to have a social media app and that's a pretty big idea right but to make the task more manageable you want to split it down decompose it into smaller part so let's say um this is the story this is the post this is the real let's use the instagram example and this is how app developers they break down a big problem into smaller ones the in other words we call this a top down design in which we show a big problem and then a general one without giving too much details and as we dive deeper and deeper we review more problem or we discover more details that we want to add to our program so that's let me show you an example of how to draw a structural diagram because you need to learn how to draw it so let's say we have we want to create an alarm clock specification i mean how hard can an alarm clock be right oh you might need to think twice after looking at the answer so for the alarm clock this is the way we break it down we split the functionality of the app into three parts the inputs the processes and the outputs so for the input of an alarm clock would be how you're going to set the alarm how you set the timer how you press the stop clock right and the processes would be how an alarm clock checks whether the current time meet the set alarm time and the output would be how the alarm would rank and how it would send notifications and so on so this is the answer uh not not the exact answer it can be broken down further but just an example here for input you will have to set time turn alarm clock on and off all right and the process is that you want to check whether the time has reached and after the time is confirmed you will need to output do the appropriate output such as sounding the alarm and or turn off the alarm so let's do one more question to help you have a better idea on how to do the structural diagram so we have a question here Okay, let me look at this. So we have a question here. A security system on a door allows a user to type a code into a key.

to enter the room and if any incorrect code is input and every message is shown and a short alarm sound is played so we can break down this problem into three parts okay so i will use another app to do this okay so first of all what we want is a security system a security system and we are going to break it down into three parts the input the process and also the output so according to the project requirement the input of it is that we need a keypad to allow the user to enter to enter the key the password a keypad and this keypad should allow the user to enter um enter code enter code So the process would be to check whether the code entered by the user is the one set by the owner. To check the code, to check the code whether they are correct. And the output would be to sound and alarm. So can you see in this just small program, there are multiple parts of the system.

And by using the input process output method, we can... better decompose the problem into some manageable one. Alright, so what if, let's say if I were to add an additional requirement, is the security system allow facial recognition? Then I would, I can draw another part here which says another form of input is facial recognition.

So by drawing this diagram, we can visualize what are some of the things that we need to build here. Alright, so if you want a security system, you first need to install a keypad and then if you want it to be facial recognizable, you need some other app and the process you know what to code and eventually what to output. So by drawing a structure diagram, you will be able to better manage your tasks.

You can assign this task to one person, another to another person, third person and fourth person. So basically helps you to split a big problem better into smaller ones. So let's go back to this question.

We have already driven it, drawn the structural diagram. So in short, structural diagram is a way for you to decompose a problem. In other words, to break down a big problem into smaller ones.

And the way we did this is to classify into the input, process, and output branch, and you'll be able to do that. So let's look into another way that we can design our program so which is to use a flowchart but before we learn how to use the flowchart we need to understand what an algorithm is because the flowchart is basically just a tool for us to model the algorithm all right the algorithm is a set of instructions for solving a problem or accomplishing a task and the way i like to think about it is it is a series of steps that needs to be done in order to achieve a task. So for instance, driving a car in real life, before you drive your car, you first need to walk to the parking space, you need to open the door, turn on the engine, and put on your seatbelt before you can say let's go.

So that's the algorithm of driving a car. And as in a software perspective, it would look something, it would look like this. So for every second, The alarm clock app will check if the time matches the one set by the user Alright, and after that it will check whether the day matches So don't wake me up at 6 o'clock on in a weekend, right?

And if both scenario matches only then the alarm will ring so can you see the algorithm is a step-by-step process? And this is exactly how the computer work. They work step by step but we as a programmer we need to define what is the correct algorithms for them so flowcharts and pseudocode which we will learn and later are effective in communicating how the algorithm that makes up a system or a subsystem work because it shows the algorithm in a diagrammatic manner right so just look at this flowchart here all right can you kind of guess what it does so we have start and it will get time and they'll check whether the time is equal to the alarm time If yes, they will sound the alarm.

If not, they will just wait for 30 seconds. So that's how we explain our app better by using a flowchart. So before we learn how to draw one ourselves, I just want to show you some of the components of a flowchart so that we can use them more effectively when solving a problem.

First of all, we have the terminator symbol, which is used at the beginning and the end of each flowchart. they are a rounded rounded rectangle rounded rectangle so the second one is the process they represent an instruction that needs to be done you can also treat it as a step in the algorithm do remember that the shape here is extremely important so if you look at the terminal symbol it's a rounded rectangle but if you look at the process symbol it is a rectangle it's it's not So make sure you use the correct shape for the correct component. So the third one is the input and output, in which we state that this step, we request the user to give us some input and it is represented by a parallelogram. All right, look at the shape here and followed by the subroutine. Subroutine is a complicated concept.

They basically represent a smaller procedure. running within the large program. So when you look at this get time subroutine here, under this, it could be a bunch of code running it.

That means it could be another flowchart there to do stuff. But at this time, we just abstract it. We don't want to know how get time work in Elan. We just want to know that this is a get time step. So this is abstraction in action, abstraction in action subroutine.

You'll definitely get to learn the benefit of subroutine when we learn about programming because it shows you how we can abstract stuff better. So just keep the idea in mind for now. And last but not least, it's a flowline denoted by an arrow to show the direction of the flow.

So let's try to solve one problem with a flowchart, right? So one note here is that when we are learning about the flowchart or even the pseudocode, It is extremely important for you to practice more because memorizing all these components wouldn't help you a lot if you don't know how to use them. So just try to do it whenever I explain an example, try to do it yourself first by pausing the video so that you can cross-check with my answers after I have done it.

So let's solve this problem. A computer program allows a user to input two numbers. and it multiplies the numbers together and if the answer is larger than 100 it outputs the message greater than 100 if it doesn't then it does nothing and the program loops until the user does not want to input any more numbers draw a flow chart that represents an algorithm for the computer program so just like how we do the structured program when before we start drawing anything let's first split the problem into the three part which is inputs outputs and also process the input here is that we we want two numbers from the users we want two numbers from the users and the output is that well we want to get send a message saying whether the numbers there's some of the numbers no the product of the number is greater than 100 and the process would be you need to multiply the two numbers so i have decomposed it here to just save your time and that's how the input output and process work so let's try to solve this problem i'll open another app to show you how um so first of all always remember when we're drawing a flow chart just start off with the terminal symbol which denote the start and end all right so we're going to start it look at how i draw the rounded rectangle it's not a rectangle so what does the program want First and foremost, they want the user to input two values, right?

So at this time, we need to use the input symbol, which is a parallelogram. Okay, a parallelogram. So look at how I draw this. You will need to write something inside the parallelogram. Just write down an input, the input keyword, followed by what name you want to give the input.

so i'm going to give it a name of n1 so what this does is that what this parallelogram does is that it asks the user for a number and then store it inside this left rectangle here is that let's say the user input a value of 5 this value will be stored inside this variable we call it variable called n1 all right so when we want to refer to the input by the user we can just write the text n1 okay so i will show you how it works exactly don't worry if you feel confused now let's just move on so we also need another number so let's do the same diagram again so we put the input followed by the n2 the n2 variable by the way you can name it anything you want i just put it as n1 to just make it short so just treat our n2 as another box here that stores a value that the stored value input by the user So here's the interesting part. After having gotten two numbers from the user, we want to multiply it before we check whether they are greater than 100. So we're going to draw a rectangle which represents a process. We're going to create a new variable here called the product is equal to n1 multiplied by n2. Sorry, I used the wrong symbol.

N1 multiplied by N2. So what this process does is that it creates another variable called the product here that stores the product of N1 and N2. So imagine that my N2 has a value of 3 now.

So the user input the value 5 and 3. And what this process does is that... it takes whatever that is inside n1 and n2 put it in and multiply them as you can see from this process so the product variable here will store the value 15 all right so that's how it works and having done the process part now we want to check whether this product is greater than 100 so how can we do this so i'll just make it smaller to save space So we need something called the decision variable. All right.

I think I missed this out when I'm explaining this. I'm going to go back. So what the decision variable does here, as you can see from the slide, is that at this decision variable point, it will ask a question. So for the alarm clock app, it's that they will ask whether time is equal to alarm clock. If and there will be two arrows coming out from this decision variable from this diamond and depending on the result yes or no they will do different thing so i'm going to show this in this example here so we want to check whether product is greater than 100 so first of all i'm going to draw a diamond here okay a diamond here and i'm going to ask the question is product greater than So this is the point where the flow chart gets a little bit complicated.

So let's first draw two lines, two arrows out of this decision variable. So it's either a yes or no question. According to the specification, we should output the message greater than 100 if the product is greater than 100. So from this variable here, product greater than 100, If it is greater than 100, what we want to do is that we want to output a message 100. So I'm going to draw the output symbol, which is again a parallelogram, and then write out the output keyword.

Okay, let me write it, the output keyword, and followed by the message I want to tell the user, which is greater than 100. 100. and that's that's the end of it we have done it but if the product is not more than 100 what do we do so let's look the program say the program will loop until the user does not want to input any more number it doesn't give instruction on what we should do if price less than 100 which means we do nothing so we after we do nothing we want to ask the user again whether they want to continue the program so i'm going to create another input variable here as, let's give it a variable called continuum. continue basically store let's create it continue continue variable basically store the answers of whether the user user want to continue or not so if the user key in the value y es yes i want to continue then we just continue if not we do we just terminate the program and let me show you how to do this so so we will from this diagram here the output i'll go back to here because after we can output a message we want to do ask the user again whether they want to continue so we give a input here and we want to check the continue variable whether it is equal to yes so if the user type yes this yes variable will be value will be stored into the container variable and now we are actually matching this to this referring to this value, check whether continue is equal to yes. So let me again resize it to make it more manageable. So again, we have another decision to make. If the user said I want to continue the program, that means he wants to do it all over again.

What we'll do is just draw an arrow back. This is basically called loop to the beginning of the algorithm so that the user can key in the value and one and two again. Whereas if the user type, so let's put a yes here.

If the user said, respond is not a yes, which means he wants to terminate the program. We'll just draw a terminal diagram here, which basically say the value is automatic. So I mean, draw a rounded rectangle.

This is very important. We just type in a value stop here. So that's. At the end of the algorithm, we have drawn the full flowchart of how this algorithm works. If you look at the beginning, first of all, we ask the user where to input two numbers and one and two by using the input output symbol, the parallelogram.

And the user's input will be stored into the n1 and n2 variable. Okay, whatever they type in will be stored here. And after we got the numbers, we multiply them. and store the result into a product variable. Alright, again, you can name this variable any names that you want.

And we'll check whether this product is greater than 100. If it's greater than 100, we'll output a message using the output component. If not, we'll ask them, hey, do you want to continue? And depending on the result they give us, we could draw an arrow to the different parts of the program.

Eventually, we terminate it. if the program user doesn't want to continue so that's flowchart one flowchart so let's try to solve another problem to give you a better understanding so let's go back to our slide here so another problem here is that tickets are sold for a concert at twenty dollars each and if ten tickets are bought then the discount is ten percent twenty tickets then the discount is 20 No more than 25 tickets can be bought in a single transaction. So you are asked to design a program that can automatically calculate the cost of the purchase. Draw a flowchart.

So again, if you want to try this out using whatever you have learned so far, just try to do it and instead of waiting for my answers. If not, you can just watch how I do it. Again, let's just break this problem down into input output and process. So the input is that we want the number of tickets.

that the user wants and the limit is 25. And the output will be the total cost need to be paid by the user. And the process is that if the user buy 10 tickets, we're gonna give 10%, 20 tickets, just 20%. So let's go to this app here to draw the program. Again, in the beginning, we usually draw this symbol called the Terminal symbol.

Start, we just draw. a start program here and now we want to ask the user for a value which is how many tickets do you want to buy so we need the input components the input components and let's name it as number of ticket no of tickets so i'm going to create i've created a no of ticket variable here this is just for illustration So. Followed by K.

Now here is the point, here is the key thing we need to ask, which is, is the value entered by the user the one that is required by the program? If you look at the program requirement, they said no more than 25 tickets can be bought, which means if the user were to key in the value 26, we are going to ask him or she he or she to key in again so here we need a decision variable so let's draw a diamond here is that we want to ask whether number of ticket here is greater than 25 is greater than 25 and if you want to be more detailed you want to include more details you can also add another one is that all or the number of ticket is less than zero less than one which means the user cannot key in any value like negative five or zero it just doesn't make sense what are you trying to do right so you can use the all keyword here is that either one of this if the condition is met we're going to do the yes path so again we have a yes and no path so if the condition here is yes the user has entered an invalid input we'll just draw an arrow back to ask the user to input again so we don't allow them to proceed to the the other part of the program and if it's not which means the user has key in a valid value we are going to write a no here so we can the user can continue to do what it's doing so let's say let's continue the no part here which means the user has key in the value between 1 to 25 so now we need to check whether the user is eligible for a discount And the specification here is that if 10 tickets are bought, more than 10 tickets is 10% off. If he got 20 tickets more, he's going to get 20% off.

So we need a decision variable to check whether the number of tickets met a condition here. So this is going to get a bit complicated. Bear with me.

So first of all, we need to check whether the number of tickets here is greater than 10. It's greater than 10. because it's only when the number of tickets greater than 10 that he or she is going to get a discount so if it's greater than 10 yes here and no here let's do the no part here if it's not greater than 10 is maybe it's from zero to one to nine so we'll set the discount let's create another new variable d as zero the d stand for discount let's give it a value of zero if the number of ticket is not greater than 10. Whereas if the number of ticket is greater than 10, we have some stuff to do. We need to do some more decision variable. Is that even if the number of tickets is greater than 10, we need to check whether it's greater than 20. So let's do another check.

Is the number of ticket here greater than 20. Again, this is important because number greater than 10 and 20 have different discount rate. So if it's not greater than 20, we know that it's from the margin 10 to 19. So the user will get a discount of 10%. Let's put 0.1. I will show you how we use this value later.

Whereas if the user key in the number of 20 more than 20 the discount is going to be d equal to 0.2 all right so again i need to resize it so try to absorb whatever i'm saying now so having calculated like what is the discount we are going to use this discount value to calculate the total ticket price so we know So we know that the price for one ticket is $20. And we need to calculate how much exactly does the user need to pay. So let's create another variable here called price. So we're going to write price is equal to 20 multiplied, sorry, is the number of ticket, how much, how many tickets the user bought, multiplied by 20, which is basic calculation.

number of tickets multiplied by price and followed by how much discount we want to give it to him So I'm going to write 1 minus D, which means that if the D here is equal to 0, which means no discount, the price will be multiplied by 1. Whereas if the discount is equal to 0.1, it's going to be 1 minus 0.1, 0.9, which will give you the price for the ticket after a 10% discount. This is the same for D equal to 0.2. So let's link. this back to okay we can do it here just to ensure the continuity of the program all right so by using this process variable we have calculated the total price for tickets so after that we will we can output this output this price for our user and that's the end the entire flowchart for this I know this is a little bit hard, it requires some problem solving skill.

So make sure if you still don't understand, try to do it yourself first and then rewatch this part of the video to understand how I use the different components of flowchart to solve the problem. All right, let's move on to another way we can design a program, which is algorithm and pseudocode. So let's define pseudocode. The word pseudo means fake and pretentious.

So think of pseudocode as kind of fake codes. They are not like programming languages that require syntax, but they are just a way for us to write down the algorithm of a program. So let's say I have an example here. This entire pseudocode, you don't have to understand what it means now. Basically, talk about how we can sort an array of numbers.

So pseudocode is the standard method of showing an algorithm. Computer scientists love to use this. It uses English keywords that are very similar to those used in high level programming language. This is important so that after the user written this pseudocode, they can translate this pseudocode pretty easily into any programming language they want.

It's like a framework that you need when you are writing an essay like that. and it's not bounded by the straight syntax rule of a programming language. And the person reading the pseudocode does not need to have knowledge in the programming language. So you don't have to know Python or Java to understand this code. So this is the reason why it's used in various computer science research paper, because different computer scientists, they learn different programming language.

So by using pseudocode, this information becomes universally understandable. So to understand how to write pseudocode like this, we first need to learn the different elements of the pseudocode. You'll find things that are very similar to the flowchart. So let's start off with assignment statement.

So a value is assigned to items or variable. So this value will usually be assigned to variable. If you remember the question that we do in flowchart, we kind of assign this variable n1 to whatever the user has key in. So think of the word variable as a box here that stores a value that we can refer to it whenever we want to. Alright, and the value on the right here, it can have one value or multiple value.

Let me show you how to do this. Let's say you want to give the variable x a value of 5. You can write in this way which the 5 is pointed to the x which means it's stored inside the variable a box here called x similarly with the name adam and if you would you can also assign a variable with multiple values by using something called the operator by the way this is the multiply operator so by doing x equal to 5 times 6 the x variable here will contain the value 30. all right the name here will contain the variable hello Adam and so on and so forth. So here are some of the operator that you might find useful whenever you want to assign, create a variable.

So the benefits of having a variable is that instead of doing whatever that is on the left-hand side, let's say I want to output all this value many, many times instead of copy and pasting this value one by one, I can assign this value, this random string here. to a variable called name and whenever i want to refer to this name instead of outputting the entire string again and again i can just say i want to output name and then the computer program will find out whatever that is stored inside name variable and output accordingly so let's try to do some exercise to explain this entire pseudocode things i know it sounds pretty abstract but let's try to do this So your task is that, so let's say I have a variable called cost. Again, picture it as a box here that has a variable five and this the name that we give this box is called cost. And by using this variable, you need to create the following variable using a different operator.

So let's try to solve it. My working space. So I want to create a price variable.

It says this price variable should have twice the value of cost well one common mistake is that people will just write okay 10. well this is not what we want to do because cost can be any value we want it to be dynamic which means whatever cost is i want the price variable to have double the value of it so what i'm going to do is that i'm going to write cost price is equal to cost multiplied by 2. You can always refer to this mathematical operators to find out the symbol for a different operation. Alright, multiply here is a star. So I'm going to write price equal to cost multiplied by 2. And the second part, tax is equal to 12% of the total price. So let's create a tax variable that stores the tax value is equal to 12% of the total price.

So what I can do is that can use a price multiplied by 0.12. So that's the way we calculate a portion of the value. And followed by another value, selling price. And it's calculated using price plus tax.

So let's do this, price plus tax. And the fourth variable they want us to do, the gender variable that contains the value m. So I will write gender here, assign it. to have a value of m and another valuable chosen variable assign it to a value of false so this is how we can assign variables either using just one value here or multiple value by using mathematical operators so you might not find it so this is the answer so another stuff before besides um and one value we can also assign multiple values to one variable.

This is what we call an array. We can assign an array to a variable. So what an array is, think of it as a container that can store multiple items.

A container that can store multiple items. What we have done so far is that let's x equal to 5. Let's say we create this variable. We are saying that this x variable has one value called 5. But what the array variable does is that it creates for one thing, for one variable name, let's say my class, I assign it a value of an array of value, a list of value here to this one variable.

So for instance, in the my class variable, you can store a list of names of your classmates or your students into just one variable. So, and this is how we the pseudocode the syntax that we use to declare an array and pseudocode first of all we use the declare keyword followed by the name that we give to the variable and the array keyword 1 to 10 here means that we are creating an array of size 10 which means this variable can store 10 values of string string is a data type basically means um like name here it an alternative could be an integer integer all right which store number so you need when you assign create an array variable you will need to first declare what this array stores what data type is stores so I'm not a way to access one particular value in a array so sometimes we just want to access let's say the third third item in the array we can do this we can just draw a square bracket all right followed by a number let's say i want to do my class i want to assign a value of my name james into this array variable what i can do is that i use the variable name my class followed by a square bracket with a number inside denoting the position and then put in my vowel and this james will be stored inside this array variable so this is how we can access a particular element in an array. So here we have a question here. Your task is to create an array variable called marklist.

The array size is 20 and it stores the integer data type. How to access the 12 elements? So I will just draw it here. I have not prepared a space.

What we can do is that we can write declare. let's declare a variable called marklist. Basically store a list of marks obtained by a student.

Marklist. And we write down the array keyword. So the array size is 20. So I'm going to write 120 of string.

Because it's storing marks, I'm going to write integer data type. Integer. And how do we access the 12 elements?

It's easy. we can just do mark list let's say i want to output the 12 element i'll just put output mark list followed by a square bracket number 12 and it will show me the 12 elements of the array so that's um just some understanding okay so let's move on to the next elements of pseudocode which is the input and output input keyword is used to ask the user for an input and the output keyword is to use to display information on a screen so it's again very similar to what we learned in flowchart so i have an application here that i want to show which i have written the program in python because one thing i realized when people learn pseudocode is that they can't picture what is actually happening in the program so i will move on to this application here called repo let me uncomment this So this is a platform where you can write down your code to just show how the program is working. So remember the program that we does, we create a bunch of variable for cost, tax and price.

So when we write it, all this code in programming language, this is how it works. So let me run the program. It will prompt me to enter the cost.

So this is the input component of the serial code or the flowchart. so i can just enter any numbers so let's say my cost is 10 and what this program does is that it will calculate for me the selling price which which is which i you can got it by multiply the cost by two and then and sum it up with the text variable so we have this since this class is not about programming we'll do more in chapter eight but i just i'm just showing it here so that you know what this pseudocode will do when they are being translated into a programming language like Python. Alright, so this is how it works.

As you can see, again, I want to emphasize the benefit of using variables here, is that it can be dynamic. Let's say I were to run it again, and I were to type in the value 1000. And depending on the cost value, different output will be produced. So this is the power of programming. and the power of input and output code here and variable.

All right, so let's go back to this code. First of all, what this code does is that it asks the user for an input. So when we write the keyword input cost, the program actually does two things. First of all, it asks the user for a number, for a input. And after that, the second part of this line is that Whatever the user key in, it will be stored inside a cost variable.

A cost variable. Alright, so let's say I key in the value 1000. The cost variable will now contain the value 1000. So if you look at the second line of code, price is calculated using cost multiplied by 2. So what this program will do is that it will look into the cost variable and see, alright, what is inside? the cost variable and then multiply it by 2. So in this case the price variable will have 2000. and followed by all these lines of codes. And last but not least, we learned the new thing is the output variable. Not the variable, the output keyword.

It will output whatever the program has calculated, just like what I have shown you in my program. So that's the input and output part of the application. So let's move on to conditionals, which is an element of the serial code that allow the algorithm to do different things depending on different input and scenario again you can relate this back to what we learned in flowchart the decision variable element the keyword that we use is if then else and case of otherwise and case i'm going to explain it one by one by first showing you the code followed by a proper dissect of how this keyword works so just try to read this code here we have this bunch of code and as you can see it's pretty similar to what we do when we read in English so you can roughly get the idea of what this is doing it basically first asks the user hey what is your name what is your age and if the user age is less than 18 then it will output something called a child if not the statement under the else element will be output you know we say is and you are an adult you are a child if then else and if so if we were to properly dissect it let me explain we first if you look at those words that are highlighted in red color these are the keywords which means you have to include them every time you write a conditionals if then else and if all right so the key the word after the word if is we call them a condition. A condition will produce a value of either true or false.

If you look at our example here, if age is less than 18. So in this case, it can either be a true or false scenario. Either you are younger than 18 or you are older than 18. So we have the true and false value here. And followed by the then keyword which means the condition is met if h is less than 18 then we output the child name so this is the part so let me use another color the code that is encapsulated by this rectangle is the code that will be run if the condition is true but if the condition is false this will be the code that will be run all right and after the we will the end if statement has ended we'll just use an and if statement to signify the end of the algorithm all right so this is how we use the if then else statement so to to use this statement better you will need the comparison operator like the greater than less than equal to to basically creates the condition here the condition here you need to use the comparison operator so again uh let's try out a question so using what you learn try to do a to this before i explain the answer you can pause the video because ultimately there's no use remember all the syntax if you can't use them to solve a problem all right so let's try to solve this problem our pro our question is that we need to write a program in studio code that asks the user to input his height and weight.

And if the height is greater than 170 and weight is lighter than 85, we'll open the message, you'll meet the requirement. Otherwise you need to be taller and lighter. So let's try to write it out.

First of all, we need to ask the user to input his height and weight. So I'll just write an input keyword again, remember a weight. So this does two things. it asks the user for his weight and then store this number into the weight variable okay we have a weight variable here and then let's input another variable called the height so this is the point uh where we want to make a decision we want to use an if statement so we'll do this if let's use another color for condition the condition for um the question is that the height is greater than 170. So just write height is greater than 170. But that's not the only condition that needs to be met.

The other condition needs to be met too. We'll use the AND keyword to state that these two conditions need to be true in order for the whole condition to be true. And weight is lighter than 85. If that is so, let's use, we'll have the THEN keyword.

If this condition is true, according to the question, what we need to do is that we need to output the message output you meet the requirement you meet the requirement if not we are going to output another message else you need to be taller you need to be taller i'll just skip this to save time and follow by the end if statement and that's it that is how we use an if statement to write a program that makes decision based on what the user key in so this is the answer hopefully you got it right too if not you can just do it again so let's move on to another way we can do conditions the another way program can make a decision is to use the case of otherwise an end case statement So here we have a code here again, you can read it too and see whether you understand it before you dissect. And how this work is that we have three different keywords here. First of all, we have case of basically look into the variable. So first of all, this program asks the user what food do you want to buy? And it store the result into this variable called food to buy.

And if you look at these two lines here, these are the options that the user can key in so if the user will key in the word Milo we will do this line of code the blue color block boxes this line of statement will be executed and if the user were to key in the word basket this is the word that will be output the code will be run instead alright what if the user doesn't key in any of these two values say he key in I want ice cream In this case, every other input will fall into this otherwise condition, in which if we need to output, the canteen does not have this foot, and followed by the keyword encase. All right, so again, let's do an example to show you how this works. Our problem statement here is that we need to ask the user to input his or her grades, and if the grade enter is A, we output a message excellent.

B, not bad, huh? C, fair. And D, why do you sleep in my class? For all the other values, we output invalid input.

So how can we do around this? Again, let's start off with the input. We can write something like the input, great. Let's give it a name of great.

And we are going to use the keyword of case of, case of, followed by the variable that we want to look into. when solving this, when making a decision. And the variable that we care is the variable grade.

And we have different conditions here, right? So if the condition is A, oh, I've used the wrong pen. The condition is A, we are going to follow by a colon. This is, these are all the syntax that you need to remember.

Colon, output, we'll do output. excellent and if the grade is b right output not bad huh so these are all the options c and you do fair and i believe you can see this is pretty easy and not very hard quite straightforward and d will write something like why you sleep in my class i'm gonna skip that to save time and Here's the interesting point. We need another keyword called the otherwise keywords, in which if the user enter a value, like besides A, B, C, or D, we are going to do the otherwise point. I'm going to output invalid input. Last but not least, we can end the statement with an end case statement.

That's the end of this question and how we do this so this is the answer you can check it out So having done the conditional pseudocode part, let's move on to the iterative statement, in which it is one component of pseudocode that is super duper useful, in which they allow the algorithms to repeat certain action without writing it over and over again. So let me explain. We have three different types of statement here. I'm going to explain it one by one.

So the first statement that we will be using is the forToNap statement. It basically, it is basically used when a set number of repetitions are required. That means we know how many times do we want to repeat, how many times to repeat.

So I have a code here. I'm going to dissect it and I'm going to show you in Python code the power of this. So we have a few keywords here is that first one is for two and next all right so what this does is that for counter equal to one what we are doing is that this okay let me um just clear all the marking or use a blue color pen so what this counter one does is that hey we taught we are telling our program hey i want to create a variable called counter and store a value of 1 here.

Alright, to just initialize the stuff, my counter value is equal to 1. And let's look at the first line of code for counter equal to 1 to 10. What they are saying here is that I want the program to run continuously repeatedly until the counter value reaches 10. So this line of code, the second line of code will be run repeatedly. repeatedly until the counter value reaches 10. Alright, so and followed by the third line, we're going to increment that counter by 1. So how it works is that, let me clear everything, is that at the beginning, the counter value has a value of 1 and you're going to run this code. So the device will output a value 1 here and then we'll move on to the next line.

where you say next counter so this line of code basically increment the counter value by one so if you look at this counter valuable we can increase it from one to two so now counter handler have two and instead of moving down the code what this program does is that it will go back up to this first line of code it will check now my counter value is two it will check whether this counter value is equal to 10. So since my counter value is 2, it's not equal to 10, it's not going to do anything. It will continue to the second line of code. It will output the value 2. So output that value 2, and then it'll go back to the third line of code where we call nextCounter, then the counter value will go to 3, 4, 5, 6, 7, 8, where it will be output continuously. And until a point where the counter value is incremented until 10. So let's look at the first line of code for counter 1 to 10. At this point, the counter value is already 10. And instead of executing the second line of code, what this program does, if the counter is 10, it will go, it will skip the code here and instead do whatever code that is here.

And this is how a for loop works. So I'm going to show you how it works in Python. Let me delete the first part of the code to show you how. So I have written this for loop codes in Python form.

And if I were to run this code, although it's only two lines of code, you can see that the value from 1 to 9 is being output, which means this in line find the keyword print counter. it's being executed over and over again until the counter value is equal to 10. And if I were to increase it to, let's say, 100. Okay. All right. I'll just, if you were to replace this 10 here into 15, you're going to get 1 to 15. Of course, there is some syntax spec here. If you write 10, the Python is going to run until the word 9. We'll definitely look more into how you can write this code in Python in chapter 8, so stay tuned for that.

So this is one iteration, which is the forToNext iteration. We already discussed this. So here we have a code here. So try to read this and see whether you can grasp what it is doing.

So I have for counter from 1 to 10, which means I have a counter value of initial value of 1 and the 10 is the ending value. And for each iteration, I'm going to output, hey, please enter the name of the student. Output keyword.

So I'm going to say another thing, hey, input. anything and then store it into this student name counter. As you can see, we have a square bracket here.

This means that this is an array. So what this code actually does is that it asks the user for 10 names iteratively, and then place each name into the array. So if you can see the initial counter value is one. So let's go through the code. I'll put enter name of student.

Let's say I enter a name called ABC. Input and whatever I key in here will be stored into student name square bracket counter. So since my counter value is equal to one here, so I'm actually inputting my counter into student name one.

Remember, this is how we refer to a variable. So my ABC will be inputted into this array. And when they click next counter, it will increment the counter value by one. So now my counter value is equal to two. And if you were to input your value again, let's say you put DEF, sorry, I'm not very creative and DEF will be entered into this, the second element of the array until the counter value reaches 10 and basically this code populates the entire array with all the values.

So this is one, just one method of how you can use iteration. And let's proceed to another form of iteration, iterative statement that you can use in which in this case, the number of repeats is not known, but this statement is completed at least one. So we have two keywords, the repeat keywords until it's pretty intuitive.

What is this that basically we repeat whatever code that is written here until the keyword here until option is equal to negative one option is a variable so let's go through this program first of all the program will output hey hello and then you'll ask us for an input and the input that we get will be stored inside this variable called the option variable and if we were to T in let's say I key in the value 5 okay receive and we'll move on to the next line we'll check whether option is equal to negative one if it's not again this line of code will go back to the repeat here and until i key in it will say hello again until i key in the value negative one option including negative one if the condition again this is a condition here if this condition is negative one yeah then i won't go the code won't go back to the repeat statement again it will just move on to whatever code that is beneath it so that's the end of it let's repeat until so um here we have a question what does the code below do pause the video and see whether you can get it all right so what this does is that in each iteration we ask the user for a mark so if you can see this code this chunk of code is being repeated and we we just keep asking the user for a mark and then we sum it up with in the total variable and the program will stop when the user key a value negative one as shown by the condition here. So one special thing about this statement is that this code will run at least one time because the repeat statement is here and let me erase this. This code will run one time regardless of what the user key in the beginning. Because the condition will only be checked here.

all right if i were to create a variable called um mark equal to negative two it will still this chunk of code part a here will still run despite um the condition not being met sorry because the condition is not checked until this chunk of code is run so this is one thing repeat until run at least one time regardless of the condition so There's another type of iterative statement, the third type. is that where the number of repeat is not known is that it will it might never get completed even for one which is the while do and while loop the same program here we ask the user for mark until and sum it all up until the user key and the value negative one so you can see here we have three keywords the wow keyword do keyword and n while keyword so it reads pretty intuitively what i say here is that wow mark this is the non-equal sign. While mark is not equal to negative 1, I'm going to run this chunk of code. I'm going to sum up the total mark. I'm going to ask you to input your mark again.

But if the mark statement is equal to negative 1, I am going to jump straight to the end while statement. So this is the breakdown. This is the condition where which determines whether the loop continues.

And this is the chunk of code that will be run if the condition is met. And this is the end while keyword that signifies the end of the key loop, the while loop. So we have another question here that says, we are going to ask the user for a number iteratively.

All right, then the square of the number will need to be stored in an array, which means if I were to key in a value of three, the square of the value 3 which is 9 will be stored into the array the program shall halt when the user enters the value 0 so to do this we need to first declare an array so which is shown here so if you notice um we have a different kind of array here so if you don't remember this is the keyword this is the name of the array and this is the keyword again if you look at this array it doesn't have the end value this means that this is a dynamic array which means we don't know how many how big the array should be so we just create an array that can be of unlimited size which is doable in python nowadays and of integer which means we want to store an integer value so you can see here i've placed an illustration here to show you what is being created an array of unfixed size so i'll create a variable called counter1 as my counter and then i'll ask the user for an input hey can you give me a number again remember the specification is that we square the number store into your array and then we just do this code while the number is n equal to zero we'll do this so and let's say that the user first key in the value 5 so what the code does is that it will check is the number, while the number is not equal to zero. Is it not equal to zero? Yeah, we got the five.

So what it will do is that it will run this chunk of code, point A here, and it will assign the value number power of two. This is the word for power. Basically, we're doing this power of two, which is 25. We are going to assign it to this square num array. the counter variable.

Alright so this counter we know that the value is 1 so in other words this value 25 is going to be stored in the first position of this array. Okay and after this step we are going to increment the counter by 1 because in the next iteration we don't want to put the value into this position 1 again we want to put it into position 2. Alright so Again, we'll ask the user to input a number after the first step. So let me first erase this.

I'm going to put 25 here. So let's say the user keyed another value. Let's say 10. Again, this algorithm will check.

Is the number not equal to 0? Yeah, 10 is not equal to 0. I'm going to run this code again. Number power of 2. I got 10 to the power of 2, which is 100. will be stored assigned into this array in element counter so remember just now we just incremented the counter so counter value is now two so i'm storing this 100 into the second value second position of this array and again i increment the counter by one and then i'll ask the user again for a value again i need to erase this so now let's say the user don't want to doesn't want to enter a value again he'll just enter value zero Again, this loop will check, is the number not equal to zero?

No, it is equal to zero. So what this does is that instead of running this chunk of code, this code will go straight to the end while statement here. So the program will terminate and the program won't be running again. So this is the way we use iterative statements and conditionals. And I understand that In the beginning, it might be very hard, difficult to digest it to even solve the problem.

But just like mathematics, for programming and writing codes, you need a lot of practice to know when to use the right syntax. So my advice for you is that just try to pause my video whenever I do exercise and try to do it yourself and cross check with my answer to check whether you got the correct answer or not. All right. So that's the basic part of pseudocode. We'll definitely go through more of it in chapter 8, where we can write it in Python, where you can see the real effect.

But before we go to chapter 8, let's move on to the next part in this chapter, called the standard methods of solutions. So what are they? So when you are doing programming, there are sometimes many operations that need to be done on a regular basis.

So computer scientists, as maybe the author of this syllabus has classify it into multiple operations that we need to do in a regular basis. So first of all is the totaling method. Totaling means keeping a total that values are added to. So here we have an example, keeping a running total of the marks awarded to each student in the class.

Let's say you have an array here called student mark. You have an array here and you want to write a program that calculate the sum of all these marks okay so what we can do here is that you can use an iterative statement to go through to go through this element one by one you can create a total variable that's not a total and then just create an iterative statement to go through this value one by one and sum them all up stall it into a variable and boom you get the result you get the total and this is exactly what this code is showing us first of all we create a total variable which has a value zero and the class size to for our follow-up to know when to stop our loop and by just doing this follow we increment the value that is stored inside the student market rate one by one and then store the sum into this total variable all right and then after a teacher rating iteration we increment the counter. So this is total. We total everything that is being stored in an array. So another very similar operation is counting.

So imagine the same array and you want to calculate how many of the students here pass their exam given that the pass mark is 50. So this is what the code will do. You will create a variable called pass count which stores the record of how many student have passed and you just go through the array one by one and if you find a mark that is above 50 let's say you will do this you then increment the pass count by one so in another word you are creating a counter program that calculates how many elements inside your array that meets a certain condition in our case how many students pass the exam so if you can see here this is how they increment it. One fact is that notice there's no else statement. This is because we simply don't want anything to be done if the condition doesn't matter. That's why we dismiss the else statement here.

And this is the counting operation. So we have a question here. Write an algorithm to find the total number of students whose name is Ali.

I know it's kind of weird, but we can write it. So let's just create a variable called AliCount that stores a value of 0. And we are going to go through this array, check one by one whether any one here is called Ali. So I'm going to create a for loop.

For counter, let's give it a value of 1. To, let's say my array size is 6, I'm going to write 6. For counter 1 to 6, I'm going to check whether... If the student name In this position, student name is an array, so when I write a square bracket, I'm accessing one element inside the array. If the element is equal to Ali, then I'm going to increment my Ali count by 1. Ali count equal to the existing Ali count plus 1. And then I'm going to write our next counter.

which increment the counter value so that I can check another value. So this is how we solve it. And this is the solution. Again, try to solve it if you don't know how to do it.

The way we can, you can use multiple methods to solve it. And this is basically counting. And another common mechanism operation that needs to be done is the maximum and minimum operation, in which we write code to find what is the maximum value in an array.

So how we can do this, I'll just explain it in simple form, instead of just showing you the code. What we can do is, again, we go through this array, the element in the array one by one, and we create a variable called max, and then whenever we find a value that is the biggest among all, we'll store this value into this max variable. So imagine i'm just going to clear this and type so let's start with the first value so i'll just create a variable variable called max and min um let's say the max i give it a value of 100 mean i give it a value of 0 all right all right sorry i used the wrong thing max i give it a value of 0 mean is equal to 100 um the reason i'm doing this is because i want any values to be that is bigger than zero to have the max value first so let me show you imagine i'm going this first element which is value 70 i'm looking at it if it is greater than max i'm going to put it as max so since 70 is greater than 0 i'm going to update my max to have the value 70 and if it's less than the minimum i'm going to get give it a value of 70 again i know it's a bit confusing bear with me.

And let's move on to the second element, 75. So now you should notice that 75 is greater than 70. So we can write our algorithm in a way using conditionals. If 75 is greater than 70, I'm going to update this max to store the latest maximum value. So if 75 equal to 70, yes, I'm going to update this to store 75. And at the same time, I'll check whether 75 is less than 70. Is it the minimum?

It's not. So I'm going to skip this. Not going to do anything.

So move on to the third element, 80. Is 80 greater than 75? Yes, it is the maximum now. I'm going to update this. Is it the minimum? No.

And I'm going to just go through this algorithm again and again until the last element. And eventually, my max element... Oops. my max element here will show me the greatest value in memory greatest value and my minimum will show the smallest smallest value and this is the algorithm i'm not going to go through one by line one because it's pretty confusing there are two methods first of all you just set the maximum to zero minimum to 100 so that um the first value is always the maximum and minimum Or you can do a more straightforward approach. You set the minimum and max to be the first element in the array.

So another operation that is often need to be done is to find the average, which is used frequently in an Excel file. So this is the code for averaging. Basically, it's just we first total something and then we divide the result by the class size.

So if you look at this element here, we just find the sum of this everything and divide by how many one two three four five six the ten elements here so average is just totaling divided by the number of students in the array or items in the array so linear search this is the type of search this is interesting in which this operation what it does is it searches the array for a particular item requested by the user So I have a bunch of student name here. All right. So let's say. I want to write a program to search for a student name called LYN.

And how we can write this code is that we can just go through each element in the array one by one and check whether the existing element is equal to what we want. So first of all, we just go through decade first element. Is it equal to LYN?

If it's not, I'm going to go to the next element in the array. and check whether it matches our existing requirement. So if it found the item, it will just exit the program and tell the user I have found. So this is the essence of linear search. It searches the program in a linear fashion until it finds the item, or if it doesn't find it, it will just tell the user I don't find the item.

So this is a useful algorithm. So this is the code for a linear search. Again, I'm not going to go through that one by one just to give you an idea of linear search.

So another the last operation that is very common is bubble sort and think about it when we are using an array we want sometimes we want them to be sorted either alphabetically or in numbers so for instance if you have a list of numbers you want to sort it to from big to small small to big and so on if you have names you want to sort it in alphabetical orders if you look at all the apps that we have now they usually have this function so um to explain how bubble salt work i have this video here which i believe that if you watch this video it's going to be more effective than me explaining here because the author of this video has created animation to show you how numbers are being rearranged during the algorithm to to achieve the final form the sorted form so put down the video link and the video description so that you can check it out all right so that's the end of this um subtopic we'll move on to validation and verification which so in order for computer system to only accept data inputs that are reasonable and accurate so for instance if you want to request for the user and for an email you want them to really type in an email instead of a number so instead when we want a specific input to be given, we need to examine them before we accept them into our system, before you use them to calculate anything. So we have two ways for validation. No, we have two ways to check the inputs, which is validation and verification. Let's start off with validation, which ensures that only data that is reasonable is accepted. and there are multiple ways of validation okay it's checking if the data is reasonable and if the data is rejected the message should be output to tell the user hey you are not giving me the correct input so the first type of validation is range check is they basically check the value of a number is between an upper or lower value so for instance when you're writing a program for um an exam mark you don't want the user to key in a value of negative five because no one can have negative five in the exam it would be pretty sad so you want to limit them to enter the value from 0 to 100 so this is what you can do you can write code um the iterated statement that we have learned is that we keep asking them for a student mark for a mark until they give us a value that is reasonable It means we don't let them to proceed to the next part of the program unless they give us the correct value.

And these are the codes that you can write to check that. I'm not going to go through that. Range check, basically check the range of number.

The other type of check is the length check operation. They are usually used when we want to check whether your user has entered, let's say a password, we want them to enter a specific length, not too short, not too long. So Here we have an example here. This input will be accepted, this will not, this will not. By this way, we can limit what are the types of information that's being entered by the user.

So here is the code of how to do it. If you notice, we have used something called a subroutine here. I will talk more about it in chapter eight. But what this does is that it will take in the password value and return the length of the password. So in this case, the code here is that Please continue to input a value here.

And if not, if you look here, if the user doesn't key in a proper value, let's say if the length of the password is greater than 20 or less than 2, the program will output a message telling the user, this is what is wrong. Your message is too long or too short. But if the user has keyed in the correct length, in other words, it meets this condition, all right and the program will then continue instead of we keep asking the user to enter the value so this is slang check followed by the next type of check is type check so sometimes we want the user to enter a numbers instead of alphabet we can do that too so say for example they only accept an integer so we only want a whole number instead of you know 3.2 or 3 in alphabetical order so Again, this is how we can write it in code. So notice that I don't go through a lot of the code very well because I think the point here is just to give you an idea of what types of check we can impose on the user. All right.

So a special note code here, they basically said input the number of mails. So obviously we want the whole number instead of a value like ABC. Wouldn't make sense.

Now what this code here highlighted in yellow does is that this will return whether the variable is a whole number. All right. So this is the essence of it.

We'll learn more about the diff function, basically taking a value to see the division, but it is also, it can also be used to check whether a number is a whole number. So that's type check and presence check. so a present check ensures that some data has been entered and that value has not been left blank so for example when you sign up for an online website when they request for you for a website they'll check whether you have the word at gmail.com or whether you have actually entered anything we'll check the presence of something and this is it if the email address entered is an empty string we call it empty string it is nothing there's nothing else we'll ask them to output something else input something else so followed by format check checks that the character enter confined to a predefined pattern let's say we want to a program that only accept a text code and text code usually have start with tc all right so you want them to enter it in this format so you can check whether okay this will be accepted Second one will also be accepted.

But if the user key in AC, it won't be accepted. All right. We want them to confine to a predefined format.

So last but not least is the check digit operation. It is used in barcode. We also learned it is in chapter two.

What it does is that the final digit of the code will be used to check whether there's any data entry arrow in the first 12 digits. So feel free to refer to my chapter two video where I explain this in depth. but this is also a type of validation check so here we i have some question please feel free to pause it to see whether you know for each bullet point do you know which validation check you should be using so i'm gonna pause it so the answer for this first one entering a telephone number we can do a line length check um so for instance one two three the one two three cannot be a telephone number we want to check the length type whether it's a number format whether it matches the format set by all the used by all the mobile phones in your country enter a pupil's name again type check we want it to be alphabet and also a part number in the form something like that it will be a format check because it needs to match this format so this is testing okay how we can ensure the user enters values that we want, the format that we set, the length that we require. And another way we can do check on the user input is through verification.

It's a lot simpler than validation. There's no code required. For verification, it is used to verify whether a user has entered the correct data.

And the first method is very simple, double entry. It is used in almost all the websites when you key in passwords. is that you key in your password two times so that you know you don't have any typos when keying in your password.

And the second thing is even I feel it's a bit nonsensical, but it's included in the syllabus. It's called screen and visual track. Basically, just after you enter your data, you check whether the data entered is the one that you intend to enter by just looking at it. So that's screen and visual track.

All right. So that's how we check the the input data we use validation or verification so let's move on to testing which is the fourth step of our pdlc remember after coding we test the data so just some information here a program needs to be tested thoroughly to ensure that it is working as intended because sometimes programmers could make mistake we could have tasks so we need to do some testing before we launch the program to the whole to everyone So before each a whole system is tested, each subsystem is tested separately. Just like what we said, a software program can be separated into multiple small parts.

So we test them one by one. So in order to test a solution throughout, it may needs to be worked several times with different types of test data. So we need different types of test data for testing. So here are the four types of data that we can use to test our program. So imagine that you are writing a program to calculate the average mark for all the marks in an array.

The normal data that is entered, again, mark the value should be from 0 to 100. So normal data will be valued in this slide here. Every value here is within 1 to 100. So we get correct average score if we were to calculate the average mark. And that's for... Second type of data is called the abnormal data in regards to your problem. Data that a program should reject.

So for instance, here we reject the value negative five because well, no one can get negative value for the exam. So, but if we, our program were to allow this program to go through without checking, we'll get an incorrect average market because the negative five value will lower down the average mark for the class. So there's in other words, there's a bug in the program.

So this is abnormal data. Extreme data, the third type of data, which is the largest and smallest value that normal data can take. So in our case here, since marks is 0 to 100, the extreme data, very extreme at 2n is the value 0 to 100. All right.

Last but not least, we have the boundary data. This is a bit tricky. This one, At each boundary, two values are required. One is accepted and the other is rejected. So scenario, we have a scenario here.

Let's say your program only accepts the value from 1 to 10. And we know that the extreme data that we learned just now is 1 and 10. So the boundary data is the data just one place outside our extreme data. So here I have in my slide, the extreme data is 10. Therefore, the boundary data, just beside the extreme data, will be 11. Another extreme data is 1. Therefore, our boundary data, another value for boundary, is 0. So as you can see, in a program, there are two different extreme data here. Two extreme data, we also have two boundary data. So let's have another example.

Let's say a program. only accept value from 4 to 15. We know that the extreme data here will be 4 and 15. That's the biggest and smallest value the program can take. So the rejected data, the boundary data, first one, should be one place below 4, which is 3, and one place after the maximum, which is 16. So we can check it. Yes, this is the boundary data. So what this chapter basically says is that for testing to be done, to ensure that our program is running well, we need to ensure that the program is being tested with all this type of data so that we can expect the unexpected.

We can know, okay, if the user entered this abnormal data, what should we react, so on and so forth. So that's all about testing and let's move on to a very useful testing mechanism we know we have learned that testing data can be used to test a program and now we'll see how they can be used, different types of data, to test a program. Which is the trace table. It's a thorough structured approach that is required to find out the purpose of an algorithm. So this is the purpose of trace table and this involves recording and studying the result from each step in the algorithm and requires the use of data.

I don't think reading the definition helps. So let's move on. directly into how we can solve the problem.

So here we have a flow chart. Imagine that you want to test whether this algorithm is correct. What we can do is that we can use something called a trace table alongside with some test data.

All right. So let's try to do this. This trace table basically records the value of each variable at each stage.

And usually in the exam you'll be given the test data so let's start the algorithm so first of all we'll start the program we know that the initial value for these three variables a b and c is 0 0 100 so what we can do is that we will put all this value into our traceable in their respective column all right since x is not defined yet i'm not going to write it and there's no nothing to be output i'm also going to leave it blank and after the variable has been initialized we'll move on to the input x again what does input x do it asks the user for a value and store the value into the variable called x but at this point you might ask where does the input come from is our testing data here look at it our test data the first one is nine which means the first input is nine so our x value here will be equal to nine okay look at Notice I write in the second row to just separate them. So let's proceed with our algorithm. Here they say if we have a decision variable x greater than 5. So x greater than b, sorry. Is it? So we see that x has a value of 9, b has the value of 0. Yes.

So we'll move on to the yes path. So what they want us to do here is that assign. the value of x to b so originally b has the value of 0 now b has the value of x which is 9. so i just fill in the blank so let's proceed with our algorithm no so again another question here is is x less than c so again it's 9 less than 100 yes we got the answer what do we do c will be assigned the value of x, 9. Alright, and we'll move on in the algorithm. a is it, we'll update the value from a to a plus 1. So our value of a is 0. So now it has the value of 1. So in this algorithm, that's it. It's another decision variable.

Is a less than 4? Is our a less than 4? It's not. It's 1 currently.

if it's not yes it's less than 4 i'm going to go back again we will do it all over again so i won't draw ever again so our next input is 7 i'll put 7 into x x is greater than b which is not 7 is not greater than 9 so i'm not going to do anything my b will remain the same second question is x less than c yes 7 is less than 9 so i'm going to update c to have the value of 7. increment a by one okay another condition is a less than four now it's not yet so i'm gonna go back our next test data is three is x greater than b is three greater than nine here no it's not so nine will remain the same and i'm gonna update c because x is less than c so c is great increment a followed by next input input update and form and until a is equal to four a is no longer less than four so we move on to the node path and we'll output b and c so what is inside our b and c is nine and one so i'm going to do this nine one all right so that's um the entire program for it by by looking at the trace table we can construct this trace table by looking at the flow chart we can construct this trace table pretty easily So another way we can construct a trace table is by using pseudocode. It's the same program as our last question here. It's the same program. They did the same thing.

It's just that it's written in pseudocode. So again, I'll just go through this very quickly. You can try it out yourself. Yeah, pause the video. And let's first write down the initial value.

00100 and output enter 10 values. So we have enter 10 values here. What is being output?

Your 10th values, blah, and i'm going to repeat this statement so input x our input data is 13 is 13 greater than b it is then b equal to x is x 13 less than 100 yes it is increment a and we just do it repeatedly using what we have on the left hand side to update so which is not so i'm going to increase that 21 21 I'm just skimming it through because it's not very hard. And last but not least, 11. Is x greater than b? No, it's not. So I'm going to remain this.

Is x less than c? Yes. So c will have the value of 11. And last but not least, I'll put the value b and c, which is 21, 11. So this is how we construct a trace table. Let me know in the description, comment if you want me to do more past year paper questions to help you practice. and i'll try to make them for you so in our next last subsection of the video it's actually it has been two hours which is um writing and amending algorithm so how do we know if an algorithm is wrong right how do we and ensure that these actually do what we do the first way we can identify the arrow is to use a tab trace table which is what we just learned so I'm going to do this trace table again.

And the description of this algorithm, what they say is that this algorithm will output, not out, output the maximum and minimum value. But we want to know whether this work. We want to know whether this program is legit. So one way to do this is that we're going to complete this trace table. So I'll just do the initial value and input x.

our test data here first test data is 400 we'll ask is x greater than b yes so i'm going to assign it to 400 is x less than c um no it's not so c will remain 100 and a are incremented next input 101 and i'm going to increase it to is 101 greater than b it's not and i'm gonna go on see is it one is 101 less than c no it's not um i hopefully you can see some problems here the algorithm doesn't really find the minimum value all right so followed by 200 it's not it's remain so i'm going to speed this up 700 is greater than b we're going to update it and 4 and ultimately the program will output 700 and 100 so this will be the maximum and minimum maximum and minimum but if you look at the output does the program do well does the program um meets its requirement it does not because looking at all this test data here the maximum yeah it is 700 however the minimum here should be 101 and not 100 100 is just the the first value that's being defined So we can say that, hey, this algorithm doesn't do what it's intended to do. And the reason this algorithm fail is that by declaring C to 100 at the beginning, if you look at our algorithm, we are declaring our C into 100 here. This means that if the entire test data, the minimum is greater than 100, the algorithm is not going to detect it.

This is why. All the test data cannot be classified as the minimum data because we initialize the C value to 100 in the beginning. It still cannot be detected. The same thing can be done here if our test data is all negative.

I'm going to do this again. Feel free to skip this to the end. So our first data is negative 7. Is negative 7 greater than B?

Notice that my b here is initialized to 0, so it's not. negative 7 is not 0.5, but it's smaller, yes, it will be able to find the minimum, it will remain the same until, yes, it will just remain the same because negative 7 is the minimum among everything, negative 6, negative 7, 0, and 4, and it will output 0 and negative 7. Again, this program can find the minimum value is correct, but the maximum value here among the test data should be negative 3 so it is outputting 0 this means that this algorithm has done something wrong so max min is not correct again by declaring b to 0 we can only detect a maximum value that is above 0. since our test data nothing is above 0 the maximum cannot be detected so of course we can have a quick face here yeah we can assign the b to have a very low value and c to have a maximum value like 1 million but this program will still fail if we were to input we have a test data of 1 million and 1 and negative 1 million and 1. so the best way to do this the ultimate solution here let's review it is to let the first number to be the maximum and minimum so that we don't give a benchmark like that we let this to be the first element all right so this is one way we as from this example you should be able to see that now we can use a trace table to find out what's wrong with an algorithm the second way to identify the error of an algorithm is use self-generated test data so here we have a problem here you can read it basically we are used we use this to calculate ticket price we have done it in our flowchart example so this is the code for calculating the ticket price we first ask the user for an input which has to be again in the limit otherwise we'll just repeat the operation and then we'll do stuff so well how we can know whether this code is correct is that we create some test data to check the correctness of an algorithm so let's say i want to check whether this program calculate the ticket price correctly what i can do is that i can create a test data test data let's say my test data is that i want to have 15 tickets so if i have 15 tickets i should have discount should be 10 so what i would do is that i will calculate manually just what is the price 15 multiplied by 20 and since it's 10 i'm going to multiply by 0.9 and i'll have the value of 270 buck so this is the correct value for the price so now we've gotten the answer we know that it's 20 270 for 15 tickets we can just go through the algorithm by hand to check to go to check whether they are correct so let's say my input is 15 i already got the correct input I'll just go through this algorithm. If number of ticket is less than 10, is it less than 10? It's not, it's 15. So I'll just move on to this else statement.

This code will be implemented. Is it less than 20? Yes, it is. Then discount will be 0.1. And now and if and if.

So when they calculate the cost, they will use number of tickets, which is 15, multiply by 20, multiply by 1 minus 0.1. So I got 0.9. And I can just calculate this to see whether this result matches the test data that I have given. So by using this method, by creating test data, you'll be able to identify the errors in your algorithm.

So that's the end of this chapter. I spent two hours. Hopefully you have learned a lot.

Let me know in the comment section on which topic you still need further clarification so that I can delve deeper into that and create videos for you to consume. And thank you for watching. That's the end of it. See you soon.