 Welcome back to 105. So how are we feeling about the midterm? Ish? All right, well, that's good. We get to do more practice. So just warning, if you think it's too easy so far, still practice and go through the other ones. So there's not solutions to like the 2020, if anyone's looked at them yet. So we can do that one after this 2023. But again, just practice, practice, practice. That's the only way to kind of learn programming and kind of get good at it. So again, just doing it live. All right, so we left off, we were done. We skipped one question because we haven't covered that yet. We don't cover it before the midterm this semester. So we only have two programming questions left, which is pretty good. So next one is the universal product code, barcode system, used on lots of items, has a 12-digit number. The 12th digit is kind of like a check on the other digit. So there's a formula to calculate it. So if you know how to calculate it, given the other 11, you can go ahead and check what the 12th is supposed to be. If it doesn't match what it's supposed to be, your barcode's probably wrong. So here were the rules for it. So first rule is to sum all the digits in odd-numbered positions, then multiply that result by three. Add the result to the sum of the digits in even-numbered positions. So by looking at this, we need to calculate the value of at least two things. So we got the sum of the odd and then a sum of the even. And then we're supposed to multiply the odd by three. Then add the result to the sum of the digits to get the even. Then find the result, mod 10. So we get some answer called m. And then if m0, then the 12th digit, 0. Otherwise, the 12th digit is 10 minus m. So if we look at the steps, they give us an example. So our goal is to write a function that takes an 11-digit number and then calculates that final 12th digit, which is what it says at the bottom. Should have said that at the top. So we're taking an 11-digit barcode input. That's just an integer. And we're supposed to calculate the 12th digit. So for this example, this is our 11-digit number. And we're calculating x here. So the sum of all of the odd numbered positions are like 5, 1, 2, 0, 6, 0. So those are odd numbers. So this is what we would calculate. We probably want to put it in a variable called odd sum or something like that. And then here we take the result, multiply it by 3. We get 42. And then 42 we add to here is the sum of all of the even numbers. So we need to be able to come up with the even sum. And then pass that. We get the result mod 10, check if it's equal to 0, and then do something with that. So any questions about the thinking for that one? So we should be able to do this by just looking through the input and essentially calculating the even sum and the odd sum, lots of ways to do this. So any thoughts as to what we should do to solve this, to get the even sum and the odd sum? Yeah, so this, we have 11 numbers. We might think, OK, while we have to do something, we have to go through each digit of this and either add it to even or add it to sum. So I might start with that. I know that there's 11 digits. So probably brings me to a for loop. So I should have a for loop that goes over every single digit. So I might do something like just write, let's see how much room we have, 4 in i equals 0. 4 i is less than 11 plus plus i. So here in our function, it's called int barcode digit, and we get the barcode as an int. So that should be our 11 digit number. So if we want to go through each digit, well, to get the current digit, we could do like int digit equals barcode mod 10. So that will give us the last digit. So that'll give us the last digit. And then we captured it in a variable called digit. So we could do something with it in this for loop. And to just get rid of the digit and update the barcode, I could do barcode. Actually, I'll just write out a longer version. I could do barcode equals barcode divided by 10, which is integer division. So get rid of the last digit. So I'm just going to do something with that digit now. So if I look at this, I might see, OK, well, the first digit here, that's part of my odd sum. So I might want to create a variable called int odd sum. Started off at 0. Let's make an even sum. And we can start that at 0. So here, OK, well, I either want to add this digit to an even sum or an odd sum. So maybe I need something to keep track of that. So maybe I start off with a boolean. I create a boolean. I look at this and I note that my first digit is odd. So maybe I'm just keeping track of what sum I add this digit to. So maybe I'll call it bool is odd. And then I'll set it to true initially. So then here I can check, OK, well, if is odd, then maybe I want to do odd sum plus equals digit. And maybe I can spell digit. Spelling's going to be the hardest part of this exam. So if I do that, OK, if I just have it as it's written right now, the next time through the loop, well, is odd is still going to be true. So it would just keep on adding over to is odd over and over again. So I should probably make sure that I set is odd equal to false. And then I can have an else statement here. So if the current digit shouldn't be added to the odd sum, well, then otherwise, it should be added to the even sum. And again, I can't spell. So even sum plus equals digit. And I could do is odd equals true. All right, are we good with that? So after this, so this will go through all 11 digits. Extract the digit, and then either it will add the digit to the odd sum or the even sum, and it will start adding it to the odd. So in this example, I would add five to the odd sum, then four to the even, then one to the odd, then nine to the even, da, da, da, da, da, da, da, da until we're done. So I essentially got this calculated and this calculated. Whoops, not the 42. I got those two calculated, right? So with that, I can just follow the direction. So I have the odd number sum, so I can just multiply it by three. And then I add that to the sum of the even numbered digits. And then I find the whole mod of that. So maybe I just created a variable called value, and that should be equal to here the odd sum times three plus the even sum. So I would have odd sum times three plus the even sum. All right, we're good with that. So that gets us all the way to this 58 here. So I would now have the 58. Now I can calculate m. So m is just whatever the result we got, mod 10. So now we could just say int m equals value mod 10. And then it tells us that if m is not 0, then the 12th digit is 10 minus 8, like 10 minus m, whatever that is. Otherwise, if the digit is 0, well, then the last digit's 0. So I could put in here, if m equal equal 0, then return 0. Otherwise, I will return 10 minus m. Boom, that's our whole function. So questions about that? So there was an actual shorter way to write that. The solution has a slightly shorter way that still uses this for loop, but a slightly smarter way to do this one is, again, you probably, I mean, it's the exam, so you probably don't want to go back and think about it and try and save two lines. But just in case to have another point of view, it could also look like this. So I could just have a while loop. So while the barcode is greater than 0, then I know that there's still another digit I have to handle, and I can just get rid of the variable and just do the odd sum, because I'll always go odd, even, odd, even. So while I can just do the odd first, then the even, and just keep on doing that over and over and over again until, eventually, barcode is equal to 0. So I don't have any digits left. And I don't really care about anything invalid. So if barcode happens to be 0, then 0 mod 10 is just 0. So I'm not really breaking the sum or anything like that. So all I have to do is I could do that and do it a bit shorter. The solution, I think they have a for loop above this. So they do this thing 11 times, which would be like 22 digits, which is a bit weird. But again, it doesn't break anything. It just runs more than it has to. So that is another one. All right. Yeah. I think I'm pretty sure it doesn't fit. Yeah, so I put it as long. But they assumed it fit. And yeah, it technically doesn't fit. But what can you do? Yeah, there are only four types we have to know for the exam, just double int char and bool. And don't worry about other things. Yep. Is they exact? No, just functionality for the list. And even if it's your syntax isn't exactly right, it's like the thought process of solving the problem. So it should be all right. Yeah. Yeah, you could have written a billion variables for everything. Your only limit here is you only have two hours to write. So if your solution is longer than it needs to be, that's fine. But if it takes you half an hour to write it, probably you don't want to do that. All right, I've seen requests for this one. This one looks fun. So ready to see program that would print the following pattern based off the number of rows entered by the user. So all we have here is just tells you we input the number of rows and we have some type of pattern. So the first part is going to be easy. It's just to get some input. So I could do like print F, enter number of rows. You can't see the screen. There we go. All right, so I should start over again. All right, so ready to see program that would print the following depending on the number of rows inputted by the user. So we have five rows. We're supposed to draw that weird thing. And then we're also supposed to, if we get an input of six, we're supposed to draw that thing. So without even looking at it, so without even having to think about the pattern, well, we have to get some input. We have to get the number of rows. So I can just do the easy part before I have to think of anything more complicated. So I could just do print F, enter number of rows, and then create a variable called, I don't know, number. Maybe I just call it num rows. And then I can do my scan F. It's an integer. And then give it the address of num rows so it can go ahead and update the value for me. All right, so that would be at least one mark, so that's good. Didn't even have to think for that. And it's out of 10. All right, so now I have to think about how the hell do I print that pattern? And what even is that pattern? So I'll give you a sec to look at it and see if you can think of how you would write a program that would print this. So some things to note as a hint. So just think about the number of different lines and maybe probably some relationship between the rows and the columns for each of the distinct lines in that. Because there seems to be a line across the top, a line across the bottom, a line along the side, a line along this side, and then a diagonal up this way and a diagonal down that way. Wow, that just looks like colored barf now. Nice. Essentially, that's like one, two, three, four, five, that's like six different lines there. Any thoughts or easy ways to get some of the lines without trying too hard? Yeah, so here probably our first step is to figure out the number of columns we have. So here, if I have five rows, so I have row one, two, three, four, five, I should probably figure out if there's a relationship between that and the number of columns. So here, there's one, two, three, four, five, six, seven, eight, nine. And then if I look at this one, this one has six rows. And it will have was 11. So one, two, three, four, five, six, seven, eight, nine, 10, 11. OK, so this has 11 columns. So it was a relationship between the number of rows and number of columns. That's probably the first thing we should get. Yeah, so the number of columns is twice the number of rows minus one. So with that, OK, I know the number of rows and the number of columns. I should probably just have a for loop that goes over every single cell. So I go over every row and then I go over every column. And then in that, I should probably have an if statement that figures out if I need to print a star or I need to print off a space. So I could do four int row equals one. I'll just keep it at one just because we're humans and we probably like that a bit better. And I could go row less than or equal to, which I guess on the exam, you would not be penalized for that, but you're not allowed to write that signal or you have to do two characters. So here, row up to or equal number rows, plus, plus row. And then I can have another for loop. So for my int call equals one. Call is less than or equal to num rows multiplied by two minus one and then plus, plus call. And then within this for loop, I need to figure out when I need to print an asterisk versus when I need to print a space, basically. So questions about that fun for loop. So can anyone tell me what the condition is to essentially print the line at the top? So is there anything that's common between all of those asterisks in terms of what row and column they are in? They're all in the same row. Which row? One. So they're all in the first row. So I can just check. So if, and I'll probably have to add a whole lot of things, but one of the things that means I print a star is if row is equal to, is row equal equal one? So I'll save some space because I know I have to print a lot of crap. So I would print f star. And then here after every row, I should remember I have to print f a new line. All right, so that gets me my line at the top. For the line at the bottom, it's basically the last row. So this line here is the last row. So I could just add that to my if statement. So if the row is one, I definitely want to print a star. Or if the row is equal to num rows, which means it's the last row. So if it's the last row, then I also want to print a star. And then here I could just do else, print f, and just print a space. All right, so other ones, OK, well, the other easy ones are the sides. So this is the first column. So or if the column is one, then I can print a star. OK, well, what about this one? So this is the last column. So we know what the last column is. The last column is num rows times 2 minus 1. So I could just say or column equal equal to num rows times 2 minus 1. All right, so now that gives us those four, right? So that wasn't too bad. If you stopped right now, you would probably get a majority of the marks without having to figure out the diagonal line. So that's pretty good. But if we want to figure out the diagonal line, well, there's probably a relationship between the row and the column we have to figure out. So maybe it helps if we just go ahead and we try to figure out what the relationship actually is. So if we look at this diagonal line, everything that has a star in it, well, I have a star in the first row at 1, 2, 3, 4, 5, 6. So at row, call, I'll just write out whatever pairs gives me a star. So it was row 6 column or row 1 column 6. And then in row 2, it was in column 5. In row 3, it was in column 4. So it just keeps on going down. On row 4, it was column 3. On row 5, it was column 2. On row 6, it was column 1, which got us to that one. So we can come up with some type of relationship between these numbers. So basically, well, I'll give you a sec to think about that. So anyone think about what the relationship is between the numbers in terms of column and row. And probably, num rows being 6 probably has to do with it as well. Because in this one, if we did the same thing where we did row column, well, the first one was at 1, 1, 2, 3, 4, 5. So it was row 1 column 5, and 2, 3, 4, 5, 4, 3, 2, 1. Something like that. Any thoughts of their relationship? Yep. To num rows plus 1, so if column is equal to row plus column, so num rows so row plus column is equal to num rows plus 1. So that could be my other condition. So here, if row plus column is equal to num rows plus 1, then that also means I need to print a star. And then am I OK to leave the last diagonal for you to do? You can do that one. All right, we seem OK, because the next exam is kind of bad. So open that so I can give you another solution just so you can have it to print. So here is another way to do it. So if I didn't want to just do, essentially, just took his solution, which was row plus call, equals num rows minus 1 for that bottom left diagonal, and just made it so he had call equal equal to something. So the final solution would look something like this. So this would draw all the stars across the top. So the first row, this is the bottom, and then this is that bottom left diagonal, and then this is the bottom right diagonal that comes up, and then this is the left side, and then this is the right side. So those are all the conditions I print a star, and otherwise I print a space. And at the end of every one, I just have a new line just to make sure that works. So we can see that it does need work if we go ahead and try that. So we get that fun little pattern. But if you ran out of time on the exam, maybe figure out the diagonal lines, take too much time, just make it so that it was pretty fast once we got to like this. And if you did that, your solution would look like this, which is like most of the way there. And I didn't really have to think that hard. All right, so also anyone looked at the exams, the past exams by the way yet, by yourself? So at least unless I'm missing something, the 2021 does not have any solutions. And that was the one that is infamous. So talking the fourth years, this is the one that I know for sure their average was 50. And they still talk about one of the questions today, this day. So they got scarred by it, so let's see if we can do it. All right, ready for pain? All right, so let's start this off. So question one, I'm going to leave because that is the only thing that is easy. All right, next question is, what is the output of the following program? So we just have main in n, n starts at 0. The condition of this for loop goes while n does not equal 0, and then I subtract 2 from n. So what is the output of this program? Anyone want to give me like the first, what's the first line this is going to print? Nine. All right, what's the second line it's going to print? Sorry? Seven. What's the next line? Five, three. Yeah, this loop's not going to stop, so I don't know why they. So it's kind of impossible to write the output, so I imagine at some point you're allowed to just write dot, dot, dot. So it only stops when n is exactly equal to 0, at least for the foreseeable future, n will not be exactly 0. So this will just keep on going over and over again. If you have an infinite loop like this, I would suggest stopping after like max 10. So after a while, it's an infinite loop. Just stop writing, because technically you could spend your whole exam just answering this question. If you just went until you couldn't write anymore, you could actually spend the whole two hours on this. So that was fun. All right, yeah. So it will keep printing, and then eventually, right, it can't represent a number, so we'll get so negative that it's with it, it's like over the limit, and then it might, so depending on some computers, they have different rules. This one, it would like loop back to the most positive value and start subtracting, and then maybe we get lucky and it hit zero, probably don't, and it just does it again and again and again and again and it might, at some point, it might be really negative, go up to really positive, start going down again. So this year, this was a year that they decided to not like students. All right, so this one says if use count is declared as a variable of int type, and x and y are both declared as variables of the int pointer type. Okay, so I don't like reading that. That means we have int use count. We have an int star y, and we have an int star x, and I don't know why I wrote y first, instead of x, it doesn't really matter. All right, so it says we have these three variables, say which of these statements cause compile time warnings or compile time errors. So first one is, okay, so x is a pointer to an int, and I am trying to dereference use count. Use count is an integer. So is this an error, a warning, or good? So I can only dereference pointers. Is use count a pointer? Hopefully, no, so this is just straight up error. All right, next one is I create some variable called p, which is a pointer to an int, and then assign it equal to x, which is also a pointer to an int, so that one's fine. Next one, I have a pointer to an int q, and try and assign it to ampersand the address of y. So the type of this is going to be like int star star. So c will, as long as it's a pointer, c will just let you assign one pointer to another, and just issue you a warning that you're probably doing something bad. You probably don't need to know this level of weird c-ness, but this will just be a warning. So you can assign, you can just change the type of pointers if you want, c will convert it for you and just warn you that, hey, your types don't quite match up, probably you're doing something wrong. Then the last line is our multi-assign statement. So in here, the type of use count, so I take the address of an int, so the type of this is going to be int star. So I can assign that to y, and then the result of that is going to be the same address of use count, and then I can go ahead and assign that to x, so that is also a-ok. All right, kind of weird, yeah. Yeah, these are pointers on the last lines. Yeah, all right, oh, this question's like real fun because I got some of this one wrong in the first time through. So they really want to trick you on this. So here we have, it says consider sets of code embedded in the following. So for each of these, like one, two, so basically in each case, all we want to do is embed like this here and then say what is going to be printed, aka what's going to be the value of i at the end of it. So for the first one, if we just put i plus plus here, what is going to be the value of i that's printed? Four, right? Boom, four. All right, what about for this line? What about if we put minus minus i? Two. All right, so they don't got you so far. What about if we put i equals j divided by 10? Zero, all right, we are sharp because this is essentially, well, this is integer division. So truncated integer division, so it would take what the number should be, which is like 0.3 and then just chop this off, doesn't matter what the value is, doesn't round, doesn't do anything. So we get zero. All right, what about the next one? What about four? All right, nine, right? So if I take a double and I do int, it just truncates it, so I just get the int nine. All right, see how sharp you are. For number five, six, three? So it's either three or six. Any, well, let's do a, I'll give you like a few seconds to think about and we can do a fun poll to see where we'd stand on the three versus six. How many threes we got? Okay, good majority. How many six, sixers? All right, the sixers are correct. So this, just a single, it's a single equal sign. So that's a assignment statement. It's not just a Boolean operator, right? So what this will do is it will assign six to i and then the result of this is going to be six. And if they ask you what the value of j was, well, six is not zero. So six is true because of the weird Boolean conversion. So this would also execute this line, but it doesn't ask you about j. So the answer to this is six. So this was the first trick. All right, how about for the next line? So what happens if I put six here? Eight, so we got an eight or what? Three. So we got a three. Any other options? What about any sixers? No one seems convinced about the six. So why is it, someone wanna tell me why it's three? Yeah, yeah, yeah. Yeah, so in this case, because I have int i equals something in my for loop, right? That declares a new variable called i that exists only in the scope of the for loop. So it shadows this i so you can't access it anymore. So it creates a new variable called i that only exists in the loop, updates it all the way to six, and then it's gone by the end of the loop. So it doesn't actually touch the original i. So it would be three. So because we shadow it. So what would be the answer if I deleted this though? So if I delete that int, well the i refers to this i. So we're actually updating that i. So it'll go through and i will be equal to one. Then i will be equal to two, then three, then four, da, da, da, all the way up to six. Then that condition is going to be false and then it's going to actually be six in that case if I get rid of that int. So fun, right? We have to remember that things shadow. Yay, yeah. Yeah, so after a for loop you're allowed to just have an expression, but just one that executes for each iteration of the loop. But it's like really kind of hard to read and leads to some accents, but it's the same thing as doing this. But typically this is also like if I just see a single statement after a for loop without any curly brackets, you shouldn't write that because if you try and like add something off, so like say I want to do something like minus minus j after that. Well, I meant to put it as body of the loop, but it's going to only execute after the loop's done because I kind of assume it's including brackets, but it's not. So it's generally a poor thing to do, but I guess they did that because they wanted to fit it on a line instead of doing a line break. Don't ask me why. I didn't write this, don't blame me. This is like, this is before I was here. All right, questions about that? All right, so yeah, I think this is the exam where they got like a solid 50. So here's a program that is not too bad, which is basically where most of the marks came from. So it says just write the output of the following program. So it would start at main, declare some const variable called t sides. It's equal to five defines an int, does a skip space of t size. So let's see what skip space does. So this for loop, okay, well, it takes an input n and it executes n times and prints n spaces. So I know that this prints n spaces and then in this case, n is equal to five. So I guess in the output, I would just write spaces like this. Here, maybe I make them a bit bigger. So one, two, three, four, five. So that would be my spaces from here and then it's a new line. So any other input I should just write on a different line. Then here we have a for loop. So it goes from i equals zero all the way up to t size minus two, which I'll just write that as three. So it goes all the way up to i is less than three. So this loop is going to go for i equals zero, then i equals one, then i equals two. So what happens when we have i equals two zero? Okay, well, we'll have t size minus one. So this is a k four, I'll just write four because I know t size isn't gonna change. So I'm supposed to print four minus i spaces. So first time through this loop, I can just write to the side, i is equal to zero. So I should print off four spaces, right? Four spaces on a new line. So I'd get one, two, three, four. Everyone okay so far? All right. So next function is print left. So print left gets i plus one. So in this case, oh, whoops, sorry. Here I forgot the star. So sorry, on this line here, after printing five spaces, I'm supposed to print star than a new line. So sorry, I forgot that, I thought it was just a new line. All right. So now here we have print left with i plus one. So it's going to have the value one. So what does print left do? Okay, well, it executes this for loop n times and prints just a forward slash or a slash. I guess it's just a forward slash. So in this case, it would print one slash. Okay, so that's the end of that function. Now it prints a straight line and now it will do print right with equal to one and print right prints backslash backslash. So that won't actually print a backslash because a backslash is like an escape code. So remember like we tell it, oh, backslash end, that's the ASCII representation of hitting enter. So if I just do backslash by itself, that's escape code so it doesn't know what else you meant. So if I want to print off a literal backslash, that means backslash, backslash, like literal backslash character, that's what I meant. So it would only print off just one like that. All right, and then prints a new line. So I could also represent new lines here if I wanted to. I'll just do a little space character but you probably don't want to write that on the exam. All right, questions about where we got this far? All right, not too bad. Oh, Jesus, okay, that was mostly, okay, let's do this quick. So next time through the loop, I'm going to skip space. Essentially, this was four, whoops. So I'm gonna skip spaces, come on, four minus I. So in this case, I is equal to one, so it'll be three spaces and then I'll print two forward slashes, the one thing here and then two backslashes. Then next time through the loop, I print three and then I believe I am done at that point, right? And then next time through, I do skip space. So T size is five, one, two, three, four, five and then I print off a little stump and it looks like this. It looks like a Christmas tree, cool, yay. All right, so yeah, we only have two minutes so I'll just show you the next ones and let me know if there's one you wanna focus on, otherwise I will do them quickly. So next question approximates E by essentially writing a loop. This one is literally insane. This is the one they've been talking about forever. So you're supposed to figure out how many bits are in an integer without using size of. Good luck, so try that yourself, get back to me. This one, you're getting an array of six integers and you're supposed to print them out in order of their second last digit. So in order of this digit in all of them, whoops. So two, six, in this case it'd be zero, one, nine. So you're supposed to print them out in order. Next question, find a Pythagorean triple. That one's not too bad. This one is slightly bad. It's like find the largest subarray sum in this array. So that's fun. And then this one is a lot of text. So have a look at this, this is a 2021. So again, no one's really been using it but let me know one on Discord or whatever if you want me to take up any particular one. And just remember, phone for you, we're holding this together. Toot toot.