 Alrighty, welcome back to 105. So, midterm is coming up, probably sooner than we would like it and the midterm is hard as all get out. So, we will start practicing early and we'll start just, I didn't see any particular questions so either people haven't looked at the midterms yet or they're all good. So, just in case, we will start going through in like reverse chronological order and we'll have all week since we're a bit ahead and if we need to go ahead and brush up on something or there's something we may have skipped we can take it up with this. So, this is just done live so no slides today we just get into it. So, here is the 2023 exam and I was talking to past students of this course and not to scare you but like their average was like 50 percent or something like that. So, we're going to do a lot better than that because we have like a whole week to practice and hopefully at least the questions I've said so far they're not too bad. So, let's see if we can get through this I think it's lengthy it's like two hours as well. So, let's see how much of this exam we can get through in just 50 minutes. So, first question write a single C statement that adds a square root of each digit in a two digit number. So, we'll just assume that our input is always two digits and it's in a variable called num. In the same statement declare and assign the result to a double variable named square. So, for example, if num is 64 then you should assign the value square root of 6 and square root of 4. So, anyone recall how to get the digits out? So, if we have an integer num and has the value 64 how do I get the 4 out of it? Yeah, yeah mod it by 10 right? So, that number mod 10 that gives me the remainder left over if I did the actual division by 10 and that would be 0 to 9. So, if in order to get the 4 that's 10 or sorry num mod 10 and to get the other one since we only have two digits that can just be since we're using integer division just num divided by 10 and then we'll get the essentially we'll get the tens column. So, to do that this is an easy three mark question. Whoops. So, it said we define a double called square. So, we can do that and then it said handily gives us the square root function from the math library. So, we get square root of num which is the input divided by 10 plus square root of num mod 10. That's it. Yay three marks. Alright, our confidence is soaring. Any questions about that? Alright, confidence is soaring. Let's see. Next question. Write a single state single C statement that declares a bool variable named positive odd and assigns true to it if and only if the value of the variable is positive is a positive odd number. So, also three marks. So, what is the true statement I need to do to get num is positive? So, if it's greater than if it's greater than zero. So, yeah. So, if num is greater than zero that gets that's a positive part and then what about if it's an odd number? How do I check if it's an odd number? A few ways to do that. So, yeah. Yeah, mod two equals one. So, if I do num mod two is equal equal. So, if I just do single equals that's an assignment statement not good. So, if we want true statement we have to use equal equals. So, that means it is odd. I could also write. So, remember in this case we're making sure num is also positive. So, we don't get any weird things with mod. But sometimes another way to write that if we want is that num mod two does not equal to zero. So, because it'll either be one or zero. And remember we get that weird thing where the result of the mod could be like a negative if it's a negative number and all that stuff. But in this case it's positive anyways. So, mod two it's either going to be one or zero. So, these two things actually mean the same thing. So, it doesn't matter. And I want this bull positive odd to be true only if both these things are true. So, what is the logic statement I have to do to connect these two things together? Yeah. And, right? And and is just two ampersands. So, we just got bull positive odd. We assign that to be num greater than zero and and num. We can take the first answer. So, num mod two is equal to one. And if I want to make sure that I don't have to remember the precedence or anything like that. I can just go ahead and throw this in parentheses if I want. But turns out works the way we expect. All right. We've got six marks. Questions about that? Things may go downhill. All right. So, this one is a bit weird. So, says you have a sequence of ten lines of code that are part of a C program. Right in the table which of the following lines result in an error and explain the reason indicate only the lines in error. All right. Wording is a bit strange but basically just write down lines that have an error in it. So, line one. Can I do all this stuff? Yeah. So, I'm just declaring a bunch of variables. And remember this is taking advantage of that silly rule about the comma. So, I is going to be like an actual int. So, if I wrote this out on separate lines it would be int i. We get int j, int k. And then we'd have int pointer p, int pointer q. So, that just does it all in one line. Not an error. We can do something like that. All right. This has an integer array of 123456789. We're allowed to do that. C will go ahead. We don't have to say the size of the array. C will figure it out for us. In this case the size or the length of that array is nine. So, that is all good. All right. Line three. So, remember arrays are just represented essentially by a pointer to the first element. So, well we can convert an array like an array to ints to just a pointer to an int. So, we can, we're allowed to do something like that. So, we can say p equals array. So, starts at the start of the array. So, here we can do this as well. So, this will let us access this value. So, index one, two, three, four, five, six. So, this lets us access this value. So, just this array at index five. So, that gives us six. And then we take the address of it. So, we essentially have, whoops, that's an eraser. Yikes. So, we essentially have q is equal to a pointer to this six. So, we're allowed to do that too. Now, we say p equals q plus one. So, we're doing some pointer arithmetic here, which is fun because q is a pointer to an integer. So, q is currently pointing to the six. So, q plus one means we go forward one integer. So, that means p is pointing to the seven here. So, we're also allowed to do that. All right. Then it says p equals q. So, q is still pointing at six. So, it says q minus two. So, we're allowed to minus, that means we go backwards two. So, now after that, whoops, let me get a more precise eraser. So, q is pointing to six. And if we go backwards two from q, we're now pointing at four. Right. All right. Then we say array equals eight. Not allowed to do that. We define the array in this function. We're not allowed to try and read a sign it. An array is an array. You can't just make a new one. So, line seven is an error. Cannot reassign array. All right. So, now we're at line eight. So, eight is p, which is currently pointing at four plus two, which brings us a pointer back to six. And then it dereferences it. So, we get the value at that location. So, that is six. So, i will be equal to six. That's fine. Now, we have k equals array p. So, p, remember, is a pointer to an integer. And, well, for an index, we need an integer. So, this is also an error. So, line nine has an error index must be int not int star, something like that. Then line ten, c will let you do this. But technically, it's, I guess, an error because we're out of bounds, right? So, this array has nine elements in it and things start at zero. So, arrays will start at zero. So, we are allowed to index all the way up to eight. So, if we try an index element nine beyond the length of the array, so that's an error. As we discovered, the compiler will let us do it. So, this doesn't say what type of error, like if it's a runtime error or compiler error or whatever. So, if there's a definite no-no, I guess we have to state it. So, questions about those three? All right. We're still all right-ish. All right. Here's where it starts to get weird, because this code is, like, really ill-advised. So, this is just asking you to, so far, we're not really writing any code. It's just, write the output of the following C program in the box found in the code below, da-da-da. So, we have a main, we have two arrays. And if you see this one just with a zero initializer, it fills that whole thing up with zeros. Turns out it doesn't really matter in this question. And then we declare an integer called P and we have a for loop that starts at my array. So, that's essentially a pointer. So, it starts at the start of my array. And for some reason uses pointer arithmetic the whole time. So, it goes up to my array plus five, which is just beyond the end of the array, but it makes sure it's less than that. And then plus plus i, which, since it's a pointer, it will go like P equals P plus one. So, it will move it forward one integer. So, this will essentially, in the for loop P, we'll start here, then go to here, then here, then here, then here, and then it'll stop. So, if we go through the for loop, first thing is pointing out is two. So, we dereference it to get the value at two. In this case, it just checks essentially if it's even. So, if the value mod two is equal to zero means it's even. So, in this case it's two even. Hopefully, it's like the most even number you can get. So, we go into the if statement. So, this does a weird pointer arithmetic, but it essentially just fills in the value. So, this takes the current pointer P and then subtracts my array. So, it essentially gives you the number of ints between those addresses. So, in this case, my array, you can think of it as a pointer to here. So, my array and then P. So, if you do P minus my array, it's basically how many ints are between this. So, in this case right now, P minus array is equal to zero because there's no integers between it's pointing at the same thing. So, this would be zero. So, all we do is we set another array at index zero to be equal to the current value pointed out which is two and then we just print the same thing. So, we print that same thing. So, it's just the index and then the value. So, we would see printed out another zero equals two. All right. Questions about that? It's a bit weird. I don't think we've seen like where we just do pointer arithmetic between two pointers and get a number, but it's essentially what we had before like if we took a pointer and we added or subtracted one or two or whatever to it, we got a new pointer back. So, this is just the same thing except we're doing the arithmetic on two pointers and it tells us how many, in this case, how many ints are between them. All right. Wow, that's a lot of things pointing places. All right. So, that's the first iteration of the loop. Next iteration of the loop P. Whoops. We do some pointer arithmetic on P. It goes to four. Oh, okay. Four. The value at P is even. So, because it's a four, then we do the same old pointer arithmetic. Let me erase that. So, now in this case, my array is still pointing to the beginning of the array because, well, we can't change the array. And P is pointing to the next element. So, the number of ints between P and my array in this case is one if we do the pointer arithmetic on that. So, we'd get another one equals essentially that value that P is pointing to which is four. So, we would get another one equals four. All right. Question so far. Bit weird with the two pointers. All right. So, next iteration of the loop, P is going to be pointing to five, not even. So, we won't go inside the if statement. So, we'll just go to the next, we'll just increment P again. P goes to seven. That's not even. Then P goes to eight. And then, okay, well, oops, eight is even. So, this line is true. Now, P minus my array is one, two, three, four. So, we're four integers away now. So, we would get another four equals eight. Questions about that other than being kind of weird. All right. And if you wrote something like this, I would probably be very displeased because, well, shouldn't do pointer arithmetic, this thing should probably just go from like i to i less than five plus plus i. Oops. And then, just do another i equals my array. That would probably read a lot better if you just used array indexes the whole time and not use pointer arithmetic. So, I don't know. Maybe this is to confuse you and discourage you from ever writing something like this because you spent like 10 or 15 minutes on the exam. All right. So, fun one. The following program has a function named, I'll make that bigger, named find largest digit that should return the largest digit in the int num. However, this function has errors and does not return what it should. You are required to fix the function find largest digit such that the output produced is in the, is the required output shown below. Fill in the table with the line number and its correction per error found. Leave other lines blank if necessary. You will lose marks for any errors indicated incorrectly. So, here's some examples. So, actually, so, what it should return for this should return nine. So, it's just the largest digit of any digit in that number. For this, it should return nine and for this, it should return seven. So, it only asks you to look at the function. So, with that function, where are my issues? Yeah. Yeah. So, the first obvious one is, well, num shouldn't be less than zero. I should be going through this while num is greater than zero. So, I have some digits left. So, that would be one correction. So, this should be num greater than zero. All right. Uno. Yeah. Yeah. So, this, this largest digit, this looks a bit suspect too. So, the largest digit is nine. And if I look at the rest of the code, if I initially set largest digit to nine, well, digit from here is always going to be some number between zero and nine. So, it will never be bigger and this function will just always return nine all the time. It doesn't matter what number you give it. So, that is also an error. The current largest digit, what should it be if I haven't seen it before? Yeah. Yeah. Oh, that was a zero. Yeah, zero. Right. Largest digit, I should set it equal to zero, not nine. All right. Any other mistakes here? Someone knew. Yeah. The order of the if? So, not you. Yeah. In line nine, num. I shouldn't be using num. I should be comparing it to the digit because if I compare it to num, well, let's, with an example. So, say num was in like 90, something like that. First time through, I might set it equal to zero, some weird things might happen. I should only be looking at the current digit. So, three errors, anything else? Looks like it probably works now. So, those are our three errors. So, we would just, you know, write them out. So, what? We had line four, line six, and line nine. So, those would be our three errors. Can we do some student math? Oh, they tried to get you. So, it's worth four marks, not three marks. So, you tried to trip you up a little bit. So, you can't like try and figure out if you're right or not from the number of marks it's worth. So, that's a trick I used to use. It doesn't always work sometimes, you know. If I've used, or if other professors use that when they were students, they probably are have, they probably just want to screw with you. So, all right. Oh, here's a good fun one. So, write the output. So, so far we haven't had to write any code thankfully, although that will probably change. So, write the output of the following C program in the box found below the code. All right. So, for this one, I'm just going to blindly follow the rules. So, remember when we were going through examples and we had variables living in memory and I drew them at the bottom. I'll do the same thing here. So, initially, we're going to start executing our code at main because that is how C works. And we create three variables. So, I'm currently here in main and I'm going to create three variables all in a row. So, I'm going to have A with the value 5, B with the value 6, and C with the value 3. And then we create a variable called P. We have no idea what it is yet and then we go ahead and we call modify. So, we're doing a function call. It's C. So, everything is passed by value. So, it gets a copy of all the values we passed to it. So, it will get a copy of the address of A, a copy of the address of B, and a copy of C. So, how we like to write it out is we can draw a new little line because it will be a new function scope as well. So, we'll call it modify. And we'll write the arguments as they appear in this function. So, I have a C in modify, I have a B, and I have an A. So, from here we can get their values because they'll be copies of whatever they are. So, C is currently three. So, we get a copy of three in modify. B matches up to this B. So, I'll match them up like that. So, B in the function gets the address of B in main. So, this is just an address and it points, actually I'll make it a different color and it points to B. Main's B, right? So, A is also an address and it points to main's A. Everyone okay with that before we start executing stuff in modify? All right. So, let us erase all of this. So, remember all the variables in here are modifies values. So, C mod two, what's C mod two? One, right? Does one equal one? Sure does, one does equal one, at least in this universe. So, it wants us to write out things that get printed. So, we would execute this line. So, we'd execute in the, so we'd print out in the if statement and then we have to evaluate this line. So, what currently is the value at A? Yeah, yeah. So, this would be the same as the value currently pointed to by A is five. So, that's it's highlighting it. So, dereferencing A in modify gives us that value. So, that value five. So, we get here five times minus one. So, that's if we compute that, that's minus five. So, we're updating the value at A to be minus five. So, this right here what we're pointing to because we're dereferencing it. Wow, I shouldn't have erased everything, that was the wrong tool. I'll just move that over and we update it to minus five. And now here we go through B. So, we go through B, find what B points at and we update this value to seven. Right? So, we dereference B and we assign its value to seven. So, this gets updated as well. So, all good so far survived that. All right. So, now we hit this print statement. So, we have to write out what things are. So, it prints out the value at A equals something, the value at B equals something, and then C equals something. So, what is dereference A equal? Negative five, right? What is dereference B equal? Seven. Right? And what is C equal to in modify? Three. All right. Questions about that before we carry on? Point is great. All right. So, now we get this one. This says we update the value or we update the address of A to point to the same thing that B is pointing to. So, in this case B is pointing to that seven. So, after that line we reassign. So, we're changing A to also point to the same thing. So, B is currently pointing to that seven. So, now A is currently pointing through that seven as well. Now in the next line, when we dereference A. So, right here when we dereference A what value do I get? Seven. Seven. Right? So, this will be equal to seven plus five. Oh, what's seven plus five? This is higher than my limit. 12? All right. You guys are way smarter than me. All right. 12. So, that is 12. So, we'll update the value at A to 12. So, currently the value at point through pointer A in modify is pointing to B or is pointing to the seven or mains B and we're updating it what to 12. All right. And then we're changing C in modify equal to C plus seven. So, currently C is equal to three which is this one since we're in modify. So, we'll be updating. Let's see how good my math is. Three plus seven is ten, right? Yeah. All right. Math. All right. And then it returns B. So, it returns a pointer to whatever B is currently pointing to which is mains B which is there just to confuse you and it turns out while the value we get from the function, well, let's just reassign it. So, we return a pointer to whatever that modifies B is currently pointing to and that goes into the variable P. So, now main or modify is done and P gets set to an address and it points to that B. Turns out we don't actually use that P ever again, but that's what it's pointing to just in case. All right. So, now we print off A, B, C and A, B, C, right? Whoops. So, A equals something, B equals something, C equals something. All right. What was A equal? Negative five, right? So, we're just reading them from over here. So, B, what's B, 12 and C is still three, right? C doesn't change to 10 or something like that because, well, when we change it here, we changed modifies C not mains C. So, we wouldn't see C equal to 10 ever we'd see C equals three both times. So, questions about that? All right. Oh, we even had a page for draft paper, didn't need it. All right. This question is hella wordy. I'm skipping it. I don't like it. All right. We can do this one. So, this is like one of the first programming questions and they are 10 marks. So, this says implement a function named dot product that gets the dot product between two vectors or two arrays. They sometimes we call them the same thing. We haven't discussed that yet but they're essentially the same thing. The function receives two arrays each having the positive integer elements of the vector. Each array is terminated by the value negative one. This value is not an element and it can only be used to determine the size of the array. So, we're determining the size of an array it ends with negative one and that tells us not to go any further. So, we don't need the size it's kind of implicitly in there. So, to get the dot product between two vectors their sizes should match. If their sizes match the function should calculate the dot product between the vectors by getting the cumulative sum of the products of their individual elements. If their sizes do not match the function should return negative one. For example s equals yeah we have like two arrays values a b c and another with d e f the dot product should be this math thing. Their sizes match and the function should return an int. All right so this gives us a better example. So, if we have some array a equal to one two three negative one and another array a. All right they shouldn't have done that. This should be called array b. So, correcting the exam live. So, if we have these two we should get the result six. If we have again array a equal to this and array b equal to this and the function should return negative one because the size of one array is larger than the other. So, lots of ways to go about this problem some are longer than others. So, the one way the solution suggests is to like count how many elements there are until you hit like calculate the length of the array of the first array. So, you just iterate through it until you get a value negative one and then you get its length from wherever you stop that and then you do the same thing for the next array get its length and then check if they're equal to each other. If they're not equal to each other you return negative one if they are equal to each other well then you forward the length of the array you just go through it and then you sum all their products together and you can do it in two steps. So, we can kind of do it in a single step if we're a bit smarter and it will save us some space. So, what so how would we go about starting this problem? Yeah. So, we don't know the length of them yet. Yeah, we can. So, one solution if you don't want to calculate the size of the array. So, I'll show just a different solution. So, one thing you can actually do here is well I'll just try and iterate through them. So, I'll just try and iterate through both these arrays until I see a negative one in either of them. So, I could just say in index equals zero and maybe I create a variable called result where I'll just go ahead and I'll assume I can just sum everything together and get the dot product. So, if this was just you know a normal array and they were the same size we could probably write something like this plus plus i. So, we could probably just say result plus equals like array a at i times array. So, this would be our solution assuming we know the length of the array and they're both equal right. So, that's not too bad to write yourself and well that would get you some marks too if you just wrote that. So, that didn't take very long. So, what I can do for this question is okay it's a little bit different. I can essentially I don't actually know the length and maybe I only want to iterate through this array once. Well, I'll just hope for the best. So, I will iterate through it. So, I'll do like while in this case because I don't know essentially how long I'm going to go through it. So, I'll just guess maybe I say something like while array a index does not equal to negative one and and array index does not equal negative one. So, I just keep on going through that when they're both not equal to negative one. So, if any if I hit either of being negative one I immediately break out of this while loop right and if I wanted to I could rewrite this to I could use the Morgan's law on this and rewrite it if I thought that was ugly. So, I could just say not and just flip the conditions. So, array index equal equal to negative one or array B index equal equal negative one. They're both the same thing right one may be more clear than the other I don't know what your preference is. So, basically I just go through it if I see either value is negative one then I just get out of that loop. Otherwise I can just assume that I otherwise the values neither value is negative one. So, I can just do result plus equals array a at index plus array B at index. Oh sorry this is a multiply yeah yeah yeah okay so now at this point let's just go through an example that should return negative one. So, if it's something like this I would break out of the loop let's write the indices. So, this is index 0, 1, 2, 3, 4 and this is 0, 1, 2, 3. So, if I walk through the code that I have now so say I get rid of I'll just get rid of this to clear space. So, how my code currently works is it will go through and at index 0 neither of the values in either of those arrays is equal to negative one. So, it would do result plus equals 1 times 1. So, that would be this term and then it would go through again then it would do two times two then three times two and we'll eventually end the loop when index is equal to three right. So, we'll break out of this loop when index is equal to three but turns out that the lengths are not equal in this case right. So, if we break out of the loop with the index equal to three because array b in this case was shorter than array a well we can check if their lengths are equal by just if they're equal whatever the index is we broke out of the loop with well the index in both arrays should be equal to negative one right. If it's equal to negative one in both that means they're the same length. So, here I do not know if both of the arrays are the same lengths but I can check. So, if array at index is equal to negative one and array b index is equal to negative one then I know both the arrays are the same length and then I can just simply return result and then if they're not oh god where at the bottom I just returned negative one right and then I can just do it all in one step all right that code I will show you a better version of that code because I ran out of room I should have taken this page but yeah I didn't so I'll just leave that up for a bit so here is the one I wrote so yeah there's solution to these two if you look at the solution for the dot product like I said there's many ways of doing this one's not better than the other but this one's a lot shorter than the other ones so if you can just try and do it and try and do multiple things at once that will help but again it needs practice so if you are somewhat confused about this one doing it all in one step or maybe you couldn't have thought of that well other solutions so you can just iterate until you hit a negative one calculate the length maybe write a little helper function that you know returns you the length of the array and then you call it on both and you see if the array lengths are equal or not and then you go ahead and you know then you do the dot product assuming they're the same all right any questions on that one because I don't think yeah I don't think in four minutes I can answer another one so I'll go through them really really really quick if you want to tell me on discord which one is more interesting than the other so this one question nine so this has dynamic arrays we don't have to worry about this one so we haven't for this semester we won't do dynamic arrays before the midterm so thank you thank gibus and then this one so this is like a UPC code or a barcode it just gives you a whole bunch of rules and says like using these rules how do you get the last digit so it's another you know implement a function we can go over that later and then I believe this is the final question it's like a whole program where you take a user input of like the number of rows and then you're supposed to go ahead and come up with how to print this pattern of stars based off these two examples and then that's the whole midterm and it is it was two hours in length and you know we only had an hour we explained through a bunch of them I skip well I skipped three questions but still that's not like over half of the exam so yeah I guess we can end early because there's no way I can do another question three minutes so just remember pulling for you we're all in this together