hey guys um so this is part two of my series of videos on uh pseudocode and topic four and pseudocode for the IB computer science exam um this is probably going to be the last part I was going to make three parts um with another video of me just solving coding questions but honestly like that's just super tedious and it would take a ton of time so I'm just going to focus on mod and div uh validation and then methods in this particular video all involving pseudocode My Hope Is that once I've covered these you can take what I've taught you and then um and then solve the problems on your own that are included in the slideshow so let's get started uh the first one we're going to learn about is the mod operator which sensor modulo like modern div in general are favorites of the IB for reasons that I don't really understand so you really need to understand how both of these work um we'll start with the mod over there now the mod operator is used to figure out the remainder when two integers are divided by each other so let's say we have 25 mod uh six okay now if we do old-fashioned division like from the sixth grade or the third grade really we would get uh we'd probably get like four remainder one right because four times six is 24 and then we have one left over I'm talking about doing long division so in this case 25 mod 6 is going to be equal to one because when we divide this number by this number what's left over is going to be the mod so let's take a look at a another example let's say we do uh 36 36 divided by 8. there's 37 mod 8 rather okay if we do 36 divided by 8 then we are going to get four remainder 4. so 4 times 8 is 32 and then we have 4 left over so that means that 36 mod 8 is just going to be equal to 4. now I would say that the most common usage of this in the IB exam is to test for divisibility so for example if you want to check if a number is divisible by three we can do input s just get s from the user okay and we can say if s mod three equal zero then output ah then we can do output is divisible by three so let's just kind of see how this works let's say the user puts an S and let's say they put in uh 27. if we were to do 20 if we were to do 27 uh 27 divided by 3 the old-fashioned way we would get nine uh sorry yeah we would get nine mod zero remainder zero right because 27 evenly divides into three so that means that 27 mod 3 would be equal to zero hence the equality to zero right here on the other hand if the user type if the user typed in 28 then we would get nine remainder one and that means that 28 mod 3 would be equal to 1. now what that means is that this statement is not going to be true it's Boolean statement's not going to be true and we're not going to print this out right here we could actually just print out instead we have an else statement and we could just say output not divisible by three a by three and just do end so that's that's an example the most common use case for mod now mod is also I would say like even more than divisibility modern diver used heavily in Trace tables so you're very likely to get a trace table problem where you have a series of mods and divs and if statements and you need to figure out what the output is that being said let's move on to div let's get rid of this move on to div now div operator can be viewed in two ways uh it's you can view it as rounding up the result when two numbers are divided by each other and finding out how many times one number fits into another so basically previously when we did 28 mod 3 we had nine remainder one okay so 28 mod 3 equals sorry this is an equal nine remainder one this equals one because of the fact that 28 just divided by three equals nine remainder one now if we do 28 div three which is different from this slash right here that means that we're just going to get nine so we're just discounting the remainder and we're just getting the maximum number of times this number fits into this number similarly let's go to our previous example we add 36 mod 8. if we do 36 divided by 8 then we are going to get 4 remainder 4. so that means that 36 mod 8 is going to be equal to 4. now if we did 36 mod or div 8 we're just going to get 4 because this is 4 right here because 8 goes into 36 four times and no more one last example let's say we have uh let's say we have 17 mod 3. that's going to be equal to 2. because 17 divided by 3 is going to be equal to 15 remainder 2. so that means that 17 div 3 will be equal to 15. all right that's basically a div Works um I I don't think there's anything more to say about that I think one thing that I do still want to say about mod is that uh if you have so generally when with Mod you're going to have the first value being greater than the second value just because of the fact that you're generally trying to figure out whether how whether this value divides into this value and then what the result is what the remainder is um it's just it's generally generally a relationship between quotients right um or between sorry not between quotients but it's it's basically like the idea is that you're taking one number I'm seeing and then seeing what's left over when you put into another number it's just the way that the questions are posed really however if you do have something like this so if you do have something like three mod 28 then if you think about division like three divided by 28 if you think about long division right is it's going to be zero remainder three three does not go into 28 any number of times so that means that in this case your um answers is going to be three okay so in the very specific case in which uh you do have a smaller number mod you do have a smaller number uh mod and then a larger number you're just going to get whatever that smaller number was on the left side anyways now that we've covered the basics of modern div let's go over and solve an IB style mod and div question so write a program that takes an input of up to 99 Cents based on the amount of cents entered by the user calculate the minimum number of coins of the given types necessary to represent the amount you can use four types of coins but remember you must calculate the fewest number of coins possible so if so this is actually based on the American system so if quarters which are 25 cents dimes for 10 cents nickels or five cents and pennies which are one cent for example if the user enters 97 cents your output should be three quarters two dimes and two pennies as this is the minimum number of coins with types required to represent 97 cents so how are we going to write this in pseudocode is we're going to get some input from the user so we'll just get input and we'll say sets okay um what we're going to do as some validation and I'm going to go over this well this is what I was going to go over next but I might as well do it now it will say um we'll say while sense uh less is greater than 99 which is invalid then make them read and then make them re-enter it so while sense is greater than 99 we're going to Output a message we're going to say output uh enter a valid uh number of sets okay and we're going to say sense equals input or actually we can just do input sense again rather so this is a form of validation so basically we enter sense if the if that value is greater than 99 we're going to display a message and we're going to prompt the user to enter in a number of cents again and then we're going to go back up here and check the condition if the number of cents is still greater than 99 then we're going to go back to this code if it is actually we should do greater than or equal greater than if the if the value is less than or equal to 99 then we can move on to the next block of code I'm sure we should do a loop Wilds follow the pseudocode conventions and Looper honestly sometimes even I I accidentally drop the conventions because it is so artificial like no one actually uses like a proprietary pseudocode anyways moving on um what we're going to do here is we are now that we've kind of gotten the scent stuff out of the way we're going to say um we're going to take our number of cents and we'll say well we're going to say quarters equals okay we're going to say quarters equals cents div um 25. so what this means is that let's say that someone for example enters in uh 76 76 right we're going to do 76 div 25 which is going to give us three we're going to use 76 as our example okay because 25 goes into 76 three times but we still have some sense left over once we do that right so next we're going to do [Music] um Port we're going to do remain remaining will do remaining uh equals sense mod 25. and if we do 76 mod 25 if we just divided 26 by 76 by 25 we would have 3 remainder one so that means right here we're going to have one we just have one cent left okay um next we're going to do uh I guess it's dimes right dimes equals uh cents div 10. actually we're going to do remaining div yeah we should do remaining div 10. so we're going to say uh dimes equals remaining div 10. so right here we have one cent remaining and we're going to see how many times 10 fits into one which is this going to be zero okay um and then we get to say our new value remaining would just be dimes mod um actually you know let's just let's switch our example around right here because this actually ends really quick we'd only have one extra Penny right so let's say we have like uh let's say we have 36 okay now if we have 36 Cents then we would do 36 div 25 which would give us one um and we would have about nine left over so remaining would be nine then we would have dimes equals 9 div 10 which is just equal zero because 10 can go into 9. and we have remaining equals dimes mod 10. so we'd have 9 mod 10 which is still 9 actually okay and then now we're going to approach uh nickels so we'll say nickels equals remaining uh Mod Five and if we did so right now we're at nine for remaining so nine Mod Five is going to equal four uh because if we did 9 divided by 5 we would get one remainder 4. now remaining is going to be equal to uh nickels mod so actually we should just have sorry was this before right there so remaining equals nickels sorry this should be remaining div five I bet uh nickels Mod Five would then be equal to so we're gonna basically going to have um so Nichols is going to be four by this point so we're going to have four Mod Five so it's going to be four well I'll put that in the comments because that's not actually what we're going to have then finally we're going to have sense equals remaining because right now we've got four cents remaining right we're going to do remaining uh div uh one because each Cent I mean just one cent right and if we do that then we're actually going to end up having we're actually gonna end up having [Music] see so for div one one fits into four four times so we're going to end up with remaining and then we're going to end up with the remaining which actually I guess we don't even really need this at this point but if we did if we had remaining equals um sense uh mod four you would just have zero at this point right um so what we could actually do right here is we could do output quarters and then our quarters variable dimes uh nickels and then sense now I hope you guys understood that um it can be kind of difficult to explain this example particularly when like I know I just got a lot of questions and I usually explain it but that's kind of the most complicated uh explanation of or those one of the most complicated problems you could solve using divid mod I would probably say that as we saw in our previous example the divisibility or just being able to understand what divin mod does in a in a block of code is most probably like the maximum you're going to have to do this is just to give you a better sense of understanding some things to point out as well are the div and mod operators these are lowercase mod lowercase div we've kind of already gone over this the next thing I want to talk about is validation so validation makes sure that user input fits certain parameters so in this case and make sure that sense is greater than 99 well you should probably have since is greater than 99 and cents is greater than negative one right or is less so we should have senses less than negative one because basically what this is seeing right here is that if the um send if this number that was entered is greater than 99 and it's or I guess we could say or actually rather than end so if it's greater than 99 or it's less than or it's less than negative one then we're going to have the user input the number again so we're basically making sure that the user input which we had right here fits into certain parameters which is that our input is going to be less than or equal to 99 and or or well I guess or greater than or and greater than uh than negative one so zero between 0 and 99 basically so the or means that it's going to fit both of those categories so it should be less than or equal to 99 and greater than negative one so zero or greater zero or greater now that's an example of validation um we're using a while loop we're always going to use a while loop to do this because that that's the easiest way for us to um basically trap someone inside a loop according to certain conditions like so we can't do it the same way with a for Loop um and I mean also like the other thing too is that this is an infinite Loop and we can't really create an infinite loop with a for Loop so this is the best way to validate your input if and when a question asks for it um again as I said before it traps the user in the loop until they enter a valid value okay so at the last major topic we're going to cover is going to be methods also referred to as functions basically on the SL exam and the HL exam for that matter you need to be able to write and call functions in pseudocode the HL exam gets a bit more complicated with stuff like recursion now typically methods are used to simplify an existing problem and generally I would say the methods you write aren't going to be terribly complex but you do need to know how they work and the syntax in terms of pseudocode so what we're going to do is we're going to just look at how to create a basic function and then we're going to look at an IB problem on slide 37 to just really kind of get more in depth into how methods work in pseudocode so what we're going to do is we're going to create a function called circle area Circle underscore area that calculates our area given a parameter of radius and basically how that's going to look in pseudocode is we're going to have the method name so we're just going to have circle area there's no def like in Python it's more similar to how methods be defined in Java which after all that's inspired so it doesn't does inspire pseudocode so you've got circle area then we're going to have our parameter radius we've got other parameters like let's say for example we're finding the area of a cone you would have like slant height or something like that but in this case we only have one parameter so it's just going to be radius now now that we've done that we're just going to go ahead and calculate the radius or the sorry the area so we're going to have area equals 3.14 times radius times radius now we don't really have a squared operator in pseudocode probably if you use a carrot like this so what people would understand or sorry your examiners understand not just people but I haven't seen it so far so I just prefer to do this for radius squared um once you've calculated the area pi r squared we're just going to return that area using a return statement and then this is important in order to end the function we're going to do uh end and then the name of the method so it's just going to be circle area without a space so the name of the name of the function is circle area then we're going to have end no space the name of the function again that's the basic Syntax for calling them for creating a method in pseudocode if we want to call this method we might say area equals circle area and then a radius of I don't know like five and then we would get our answer which would be stored in area so now that we have a basic idea of the syntax of methods in pseudocode let's go ahead and let's look at a coding example let's go to slide 37 so right here we have the following method calc BMI accepts person's height which is going to be H and weight which is going to be w and returns their body mass index so we have a function here it's called calc BMI notice the end calc BMI here our input is H for height in meters and weight w for weight in kilograms and we're returning B which is going to be their body mass index so right here it says Baris weighs 104 kilograms and is 2 meters tall his BMI can be calculated by calling method calc BMI as follows so basically we're calling this function right here they're giving us that our input is going to be 2 meters and it's going to be 104 kilograms right here so H is going to be 2 meters W is going to be 104 so X right here is going to be H squared which is going to be 4 in this case and then B is going to be equal to 104 divided by 4 which should just give us 26. so State the value of variable buddies BMI basically we calculated 26 that gets stored in Buddy's BMI and our answer is 26. so that one we just kind of to trace the value or we to treat the area to trace our return value from what is BMI we're given some parameters we need to figure out what the output is okay so right here we've got a very function heavy question so it says a person can belong to one of the four weight categories and these are basically the ranges that correspond to different weight categories now B so the actual questions use pseudocode to construct an algorithm which accepts a person's BMI and outputs the weight category the person belongs to now this question is terribly unclear now it doesn't say whether we should accept their BMI as input so using the input keyword or whether it should be in the form of a function and if it is in the form the function should we return it or should we outputted so what we're going to do especially since this is already part of a question that very heavily relies on functions we're going to go ahead and we're going to write a function in order to fulfill these requirements which is actually what we end up needing to do according to the solution anyways so what I'm going to do is I'm just going to create the name I'm just going to create a function called category and since our input is BMI we're just going to have a variable called BMI okay so what we're going to say is if BMI is I guess that's what BMI is less than 18.5 so BMI is less than 18.5 then uh then we're going to output underweight else if let's say BMI is okay it's going to be greater than 18.5 or 18 if it's going to include 18.5 would be less than 25.0 so if BMI is greater than or equal to 18.5 and BMI is less than 25.0 then we're going to Output I think that was like something like normal what isn't it normal way um we'll have another actually we shouldn't put e lift the the actual like the formal syntax is else if so let's do that then we'll have elsif [Applause] um so we're going to have we'll just kind of copy this because it's basically what we need to do again so if BMI is greater than or equal to 25 0.0 and BMI is less than 30. you're overweight I guess I don't know I don't really trust BMI um okay so output overweight and then we're going to do else if uh BMI is greater than or equal to 30 or obese Joseph BMI is greater than or equal to 30. then you are I guess obese doesn't say what happens if you have a lot of muscle but anyways so this is going to be our function and then because it said output we're just outputting it so we're basically just printing out uh the output right here if it was a function we'd obviously have a return statement in each of these places and then we're just going to do n category to close out the function um I guess this is like more of a sub program really than even a method but that's actually just what the uh what the mark seam tells us now the interesting thing is that is even unclear to me is whether this actually needs to be in a function format or not because like nothing here really says anything about a function definition but all the all the example answers indicated that you had to have a function with some sort of name now given the mark scheme I would argue that while this is the way that we did it in order to put in a function that also if we just did like if we did that and then we just said input BMI that would also be acceptable so I think that either one is actually acceptable given the mark scheme um but I guess that was a good that was a great way for us to just be able to create a method in pseudocode and also I mean that is what the example answer says again the IB exam is not the most ambiguous or it's not the most clear sorry it's definitely ambiguous now moving on right here it says the data about a group of adults and their height measurement in meters and weight is held in three one-dimensional arrays um so we've got three Rays we've got one called name weight and height and importantly like in most IB questions these all match up to each other so we've basically got um we've got a name a weight and a height and they all have the same indices so like buddies has a weight of 100 kilograms and a height of 2 meters and so name one matches up to weight one which matches up to height one now to State the name of the person whose height is held in height three therefore by the logic that we just sort of went through this is high three so that person is going to be Paul now moving on to the rest of this question so we identify one reason why binary search algorithm cannot be used to find the name of the person whose height is given the reason for that is because to use a binary search algorithm everything has to be sorted either in ascending or descending order which none of these are so we can't really use either height or weight in order to find or I guess in this case just height but we can't use either one to find the name of a person based on their height at least via a binary search algorithm we could use a linear search algorithm but not a binary search algorithm because height is not sorted describe how describe how the name of a person whose height is given could be output so the easy answer to that is we would just look through the height array right here for the height we're given and then based on the index of that height we could just go back and get their name now this of course assumes that there are no duplicate Heights which we're not really told again but I guess that's true based on the mark scheme I mean honestly this is a bit ridiculous um so that's basically what the mark scene said okay so this is the last question basically we need to do is construct an algorithm which will output the names of all people whose BMI is greater than the group's average BMI and we're going to use the method calc BMI to do that so we're going to break this down into a couple of steps so first we need to get the groups uh BMI so the the well I get yeah the group's average BMI then we need to compare the average BMI to the individual BMI of each member and then thirdly but kind of concurrently what we're going to be doing is outputting the names those who have higher than the average then and what we need to do is we need to use calc BMI which basically has two parameters and that has let's just go back here and review basically we've got calc BMI which accepts a height a rather yeah so it accepts a height and a weight so we're going to have height and weight and in order to do this as well we're going to have access to three arrays which are all in sequence so we've gotten the name array we've got the weight array and we've got the height array and all of these indices correspond to each other as we talked about so Annie 52.4 1.56 index 0. so let's make a list of those right here we've got name weight and height okay so you know what to do we know what we have to do and we know what data structures we have to work with so first let's get the group's average BMI I'm going to start by just adding them all up I'm going to create a variable called sum instead of equal to zero then I'm going to loop from 0 to name dot length minus one so that's going to be so well each of these have 29 each of these have 30 elements so we can just say name 30 just to be clear and height 30. now this isn't part of our pseudocode this is just me kind of putting this here so I just understand what I'm working with so we can do name.length minus one which will take us from 0 to 29 or probably just to be even clearer we can just do we can just do this and now that we've done that we're going to do sum equals sum plus um well we want to add up the bmis so we're going to do calc BMI weight or rather height I weight I start basically going getting the height and weight for each and every person sorry this should be 29 from the index 0 to 29 and after and up right after we do that we're calculating the BMI using those values and adding it to our current sum so that way we'll get a sum of all bmis for everyone from 0 to 29. we're gonna do end Loop and then we're gonna do average equals sum divided by 30 because that's the number of of names or people that we have the number of elements you really have in each array so now we have the average we can compare the average BMI to the individual BMI of each member so what we're going to do here is again we're going to do loop I from 0 to 29 because we're going to go through every single member get their individual BMI and then compare it to our average in an if statement actually let's go ahead and just call this average BMI just to be a bit more clear so now that we've got that we're going to say if uh calcium I see we're just going to copy this right here now this would work we can use I for both of these but let's just say to be a bit more clear I'll just change the um the name of the counter to J so we'll just say if calc so this is for every single member if calc BMI height J weight J is greater than um uh average BMI then we're going to Output their name so we'll just say uh name j let's just make sure all of these are the same so height weight and name I just want to go back okay name Way Type I just want to make sure that the names of those arrays were correct um also I just want to go back to the question as well just before we go on so we are outputting the names of all people's BMI is greater than the group's average not greater than or equal to so we're going to say okay yeah so I guess we did that right I'll put name j and if and uh and loop right there and that's actually about it so that brings us to the end of part two as I said before hopefully so far I'll hope hopefully now after part one and part two you have all the tools necessary to be able to uh complete the SL exam or the programming portion of the SL exam again like I I was going to go through the practice problems but I don't think that I'm going to just because honestly I think it's pretty tedious for everyone involved however if you do want to see me work through those go ahead and put a comment below this video anyways if you found value in this video and want to see more like it please remember to like And subscribe have a nice day