 Welcome back to 105. More practice today because yeah, we could use it. But it's definitely almost reading week. It's probably your last class before you get to be actually free. So let us get to it. So next question. So we didn't start the programming questions. Again, this is the 2020 exam. So this is the infamous one where the average was about 50. So let's see what we can do. So this is the first programming question. So the value of the math constant E can be expressed using infant series E to the blah, blah, blah write a C program that approximates E by approximating the value of that infant series. But it should stop adding terms until that value is less than 0.01. Your program should print the approximation to E and the number of terms used to determine the approximation. The terms of the series are one, then one divided by one factorial, then one over two factorial and so on. So if we wanted to simplify this too, we could just say, well, zero factorial is also one. So if we just write that instead, looks like it lends itself more easily to a loop. So we should just have a loop. Here we can see the bottom increases all the time. So it's like n one over n factorial plus one over n plus one factorial. And we start with n equals to zero. So looking at this, we need probably to define a function so we can compute a factorial. So we can just, I'll just do that quick. So we could create a function in this case while we'll eventually get a double out. So we may as well just say it returns a double and we can say fact with some int n. Oops, not a function declaration. So we can create a function called factorial. Maze will save yourself the writing, not write out factorial, just write fact. And that would be something like double. So we're producing like a product because, you know, n factorial is equal to n times n minus one and that n minus two all the way up to, well, essentially two times one. So I can just do the short version of that and do product equal to zero that'll go or equal to one that'll go ahead and sum all of my products together. And then I could just have a simple for loop. So for four int called x equals two. So I can just start at two. I don't need to multiply by one or anything. I can just say term, oops, sorry, x is less than or equal to n. And then plus plus x. And then I can just say product times equals x return product. All right, something like that. So that would be my factorial function. So any questions about that, I'm just making it return a double because I know I'm gonna have to use a double later. Yeah, so how the for loop works is, you know, it'll take this condition and then always execute that. So it will execute that first and then check the condition. And then if it's true, then it does this. So yeah, if we give it like one or something like that, it would just won't go into the loop. It would set x equal to two, then check this two less than or equal to one and then that would be false. So it wouldn't even bother going into the body. So that's a good thing to think about what happens if n is like zero or n is one. So we want one back out of it and that works with this. All right, so for our main, oh yeah, another question. So y plus plus x, not x plus plus. So again, remember that the post fix is the weird one that always like returns a value that was the original one in the for loop. This, the expression here, the value is not used for anything. All we care about is that it changes the value. So here I could write x plus plus plus plus x. Doesn't matter, but the default rule should be unless you actually need the old value out of that expression, always prefer this one because well, when it does matter, it might make your program really, really slow for no reason. So pretty much always prefer the prefix version. All right, so then we could have int main. So it takes a void. And we're trying to calculate the value of e here. So I can just create a variable. So called e and then initialize it to zero. So now I basically just want to just write a loop that computes the next term. So I'll keep track of what term I'm going to compute by just saying, oh, let's just create a variable called n. So I could just say int n equals zero, something like that. And then maybe I just have a value called term. So the value of the current term and maybe I'll just set it equal to one because well, that's the value of the first term. So in my while loop, I could just do while and then use their condition. So while the term is less than 0.01, so while term is greater than or equal to 0.01, I just want to keep on computing the next term. So I would add the current term to e then increment n and then I can compute the next term, which would be something like one divided by the factorial of whatever n currently is. So I could just have my loop that'll go for as long as I need to to actually compute the value based off what it told me. So I just keep on going until my term is less than that. It wants me to print the approximation of e and also the number of terms used to determine the approximation. So I can just create another value for that. So I could just do like int num terms. And while you're writing the exam, I would suggest you like just flip to the back page and just write space between your lines. So if you need to do stuff like this and insert stuff, you don't have as ugly writing as I have. So what I call it num terms, let's just say I initialize it to one and then I add the term to it. Then each time through the loop, I would just increment num terms and then I print it at the end, all that stuff. My writing is terrible. So this question isn't too bad. So I might have an off by one error here. I can check that later and post it for you on Discord, but this isn't the hard question. So this is the infamous question. So I wanna go through them too so you have the solution for them and then you can ask on Discord and all that. So this question, they still talk about it to this day. So it says an int in C is represented and stored with a certain number of bits inside the memory of a computer. This number can differ depending on the actual computer. Say the number of bits is n and the bits are indexed zero to n minus one. The rightmost bit in the binary representation is bit zero. So remember when we had columns, that was like the two to the zero column. So that was initially one and they called the least significant bit. I use that word too when we were talking about how an integer is represented. And so conversely, the leftmost bit is bit n minus one and it is the most significant bit in a n bit integer n, n bit n minus one is used to represent the sign. When bit n minus one is a one, the integer is negative, otherwise the integer is positive. Then it says write a C program to determine how many number of bits are used to represent a sign integer of size n on a given computer. For example, on a computer that uses 32 bits to represent an it type integer, your program will print n equals 32. You are not allowed to use size of and your program should just make use of loops. Any ideas? Yeah, kind of, yeah. Keep on dividing by two until it hits one. So let's say we can start off. So that's a good idea but it's gonna be easier to go the other way. So what's saying is if we have like all these bits, so we have like bit n minus one all the way here over to zero, we know that if this bit is one, then the number should be like a one, right? So what we can do is essentially, if this is like the sign bit, so this is like what the sign should be, how we can think about it is we can just try and move this one, column by column by column by column by column by column by column until it eventually makes it to the sign bit and then it changes to be a negative number. So your way would be like, we start off with the negative number and then just start dividing it, which is close, but it's gonna be easier to just start with one because we know there should be at least one bit in another. So it's, yeah. And then we can count the number of bits from them until it turns to zero. But this is a weird question because if you wanted to know the size of an int, you would just use size of int and then you'd be done. But if you were to think of that, so if you didn't think of that, don't feel bad because I looked at that and well, when the fourth year student told me about their trauma, because they do still remember this, so yeah, that says something. They're like, yeah, we, I still remember this and I, whenever they told me I was like, yeah, that's weird, why would you ever have to do that? But what we can do using this idea, oops, is our program could start with just some int called x and then it's the idea we have up here. So we could start with the value one and then we know we could have a integer called n that keeps track of the number of bits and we're essentially going to try and move it, oops, move this bit up by one each time. So each time we move it up, that's another number it can represent and we're just gonna keep doing that until it becomes negative. So we don't know how long we have to do this for so we probably want a while loop. So we could do while x is positive. So when x is greater than one, how would I just move this one up a column? So if this was like, if this was a base 10 number and I gave you, let's say, I don't know, say I gave you the digit two and I told you to move the two up one column, what you have to do to move it up one column. So, so rare it's like the 10s column, the ones column and the hundreds column. So if I just gave you the number two and I said, okay, just move it another column so that you got the number 20, what did I have to do to the two to get a 20? Yeah, multiply by 10, right? And if I multiply it by 10 again, I get 200. So I moved it up another column. Well, that works with base 10 numbers, computers are base two. So if I want to move this bit up one column, I just multiply by two. So I could do x minus equals two and then I could do plus plus n. So that way I'm just trying to move it up a column and then I stop whenever I see it flips negative and then I know I'm at the last bit. So I can stop and then I can just print off however many number of bits I have. n equals d and then we can make a fully functioning program, return zero this. So this would be the full solution. In first year, there'd be no way in hell I would get that. So don't have to worry about that. But yeah, so main thing with these, a lot of it is just going to be reading the question coming up with a plan before. And most of them, it'll be like, there'll be some combination of like a loop and if statement, something like that. That's the main constructs we know. So that was a one. All right, so. And there'll also be some question probably involving arrays like one or two or something like that. All right, so this program is a bit more doable if you get the right idea. So it says, given an array of six integers, write a function that prints them in order of the second digit from the right in the manner shown. So for example, if the array held all these elements, so we want to be able to extract whatever the second digit from the right is. So for the first number, it would be a six. Then next number, it should be a two. And for the next number, it would be a six. What would be the second digit from the right for this number? Zero, right? Yeah, so just be a zero. And then here would be a one. Here it would be a three. So we're supposed to print them in order, I guess ascending order. Yeah, I guess that's what it says based off the example. So I should be able to print off something like this or there's one number that has, there's a six here and here. So I could flip this order to either solution would be valid. So also says you should print out the commas as shown. So usually the way I like doing this is I think about what I should print if there's just one number. So if there's only one number, I would just print a five and then for every other number it would be like I'd have to do a comma and then a space and then the number. So those are my two cases for printing which probably means I need a Boolean to keep track of whether or not I need to print a comma and a space or not. So that's another thing we should probably keep an eye on. Other than that. Oh yeah, sorry, explain the space one more. So when we have to print these numbers with commas between them, right? There's six numbers, but there's gonna be five commas. So while we're gonna do something for each of the six numbers, we're gonna print the number. So we also have to decide what we're gonna do with the commas. So one thing you can do since there's one less comma than numbers, easiest way I think about it is if there was only one number, I wouldn't even need to print a comma, right? So for the first number, just print the number and then for any other number, you would print like the comma, space and then the number. So comma, space, number, that's like the common pattern between all of these and then they end here. So the first one is a bit special because it's just the number and then comma, space, number. All right, so when you think about this one, we want to print them in order. So one thing we can do is maybe we think about, okay, well, I need to print them in order. Like I can't just go over the array and then check what the second less digit is and like decide whether or not to print it by just going over the array first. I need to know, I need to kind of respect my order. So I might need a loop that goes over the digits in order first. So for this question, how I would probably think about it is I probably need to go over the digits like in order, like one, zero, one, two, three. Da, da, da, da, da. And then for each of these digits, I can like look at my array and see if the number has that second lowest digit. So if I can look at the zeros and say, oh, do I need to print any zeros? So I could look over the array. In this case, I would be like, no, don't need to print that. No, don't need to print that. No, don't need to print that. Oh, okay, I should print that. And then I would look at the rest of them. Don't need to print that because it's not a zero. Don't need to print that because it's not a zero. And then I could see, oh, okay. Now do I need to print any ones? Go through it, check if there's any ones. Then okay, done the ones. Do the twos, da, da, da, da, da. Go over to the nines. So I did that in order so I could probably do something like, okay, I'm gonna leave myself some room so we can fix up the print. So first thing we want to do is go over the digits in order. So I could start at digit zero and go all the way up to, whoops, digit is less than 10 or I could say less than or equal to nine, whatever makes you feel better. So, and we can just ignore the space for now. So this is for every digit. So I need to see if I need to print the element in the array. So let's give ourselves some space here. So I could say, so now I just have to iterate over every single element of the array. So I equals zero all the way up to the length of the array, which they don't have that macro or anything. They just tell you that the array is always six integers. So you just gotta believe them. So we could go all the way up to I is less than six plus plus I. All right, so then we just need to figure out what the second digit of the current array element is. So let's assume, and usually I like to get like some examples. So say our first digit is 269. So how do I get the second digit out of that number? Yeah, mod 100 divided by 10. So yeah, mod 100 would give me this and I can divide by 10. So that is a perfectly good thing to do. So the current number I'm looking at is called array and I'm looking at array I. So mod 100 divided by 10. So I could do that. If you thought of it a different way, there's more than one way to do it. So you could do like another version of it. You could do array I divided by 10. So that will chop off this nine. So you get 29 and then you could just mod 10 that. So those two things, both the same way to get the second digit, whatever you think off on the exam, just write it. Don't worry about it. You don't have to like simplify it or do anything with it. So after that, all we have to do is like check. Whoops. So if the second digit is the same as the digit, so it matches the digit in the order I'm going through them. So zero, one, two, three, da, da, da. So if it matches, that means I should print this number now. So we won't worry about the comma for now and we can do that in a second. So I would just print off the number and just say I print off the number. They're gonna be all smooshed together if I would actually run this, but this gets us most of the way there, right? So this would just print off all the numbers all smooshed together, but they would be in the correct order. So in this case, I would get exactly this. So if I want to go ahead and obey their comma rules, well, I probably need to just create a Boolean that just says, hey, am I printing the first number or not? Because well, that's the, like we did when we argued about what's different between all the one we print them for the first one, we should probably just print the number for the rest of them to have a comma in the space. So I don't know which one I'm going to have to print first. So maybe I just say a Boole and then I call it first print. And then I say, I'll just set it to true initially. And then in here, I know I have to print a number in this if statement. So I have to expand this a little bit. So, whoops. So if I need to print off this digit, I should probably check if, let me get it back to the color. So if this is first print, then I just print off just the digit by itself. And then I would set like first print equal to false. And then it will just not, it'll only print the first digit without anything else. And then in the else branch, I can just do like print F comma space, the digit and then array and then we're done. So if you ran out of time, then you could go ahead and you could just not worry about the commas. You'd probably get most of the grade for having like your two loops and everything. So you can, if you are tight on time, you can do that. And if you leave space, you have enough room to write it. So I'd probably recommend doing something like that. So questions about this one. So I'll zoom out so we can see it. Yep, sorry. No, we don't need to do it for the last digit. So the last digit is just the number by itself. So like there's no comma after the last digit or anything. So here, so for all these numbers, it would be like the first one here is just the number by itself. And then everything after it is comma space digit all the way up to and including the last one, right? They're all the same. So you could, so that's the way, you could also think about it like, you could always print like a comma space until you get to the last one and then just print a digit. But usually I don't like doing that because sometimes you might not know when the last one is. And if you only have one, well then you just wanna print just a number by itself. Yeah, if you like, yeah, there's other ways to do it. Like create another array and try and put them in order or something like that. So you could do that but you're probably gonna run out of time. So there's more than one way to do that. For this, you probably wanna do the easiest thing possible. And yeah, so for the mod 100 divide 10 so yeah, there's a question and discord. So the mod 100 of a number, that will just essentially give you the last two digits and then dividing by 10 will always just chop off the last digit because it's truncated integer division. It just goes away. All right, so now, oh, this question. So Pythagorean triple is a triple of integers x, y, z such that x to the power of two plus y to the power of two equals z to the power of two complete this function that takes a single positive integer x. So we can highlight what our arguments are. So we want a single positive integer x as an argument and then prints three positive integer values x, y and z such that well, they have to follow all these rules. So knowing this, all we can do is like just check whether or not we have a Pythagorean triple and we know the range of values for y. So we would know x and we have a range for y so we can just check them all until we find something that fits. If not, well, we have like, there's only a bounded number of them we can be so we can just try them all and if we happen to find one, we can just say, hey, we found one, print it off and just end the function. Otherwise, we just say, what's it say? It says, just print no solution exists if it doesn't and if there is one, it wants us to just print off x, y and z and it doesn't seem to be too concerned about the format. So the way this equation is set up to, well, since x is positive and we have some positive number plus some other positive number equals z, then this condition will always hold so we don't really need to know x is greater than z. It will definitely be greater than z, sorry, Canada. So for this, we know all the values of y so we can just do them in order if we want. So what's the first value of i that is greater than zero? Hopefully one, so we could have our for loop so we could do int y equals one all the way up to y equals 100 plus plus y and then we can just try and find the solution to this Pythagorean triple. So essentially we have to do some algebra, I guess. So if we want to solve for z, well, we just take the square root of both sides so it's something like this, right? And we know x, we know, well, we're trying to guess y and we know how to do a square root. If you didn't, just make up some function if you don't know it just so you can complete the question. So if we wanted to solve for z, it'd probably be like double z equals square root of x times x, so that's just x to the power of two. If you wanted to use like power x two, go for it, you don't have to. Again, more than one way to do these types of questions, whatever you remember at the time. So I would just do something like that. So what it wants is it wants to make sure that it is an integer. So we know that x is an integer and y is an integer. So we want to make sure that z is actually an integer because right now it's a double, right? Square root returns a double. We might get like 1.1, something like that. So any idea is how we would check if a double is an integer. Yeah, so create an integer value, let's call it i. So just assign equal to the double and then so what does this line do? If I assign a double equal to an integer. Yeah, it'll just truncate it, right? So if I get something like 1.14, it would truncate it to like one, well just one straight up, whoops. All right, so I try and smush it into a, wow, what the hell? Okay, so I try and smush it into a variable called i and now what you want me to do with it. So check if i is equal to z. So if I do that, I'm comparing a double to an integer. So it would convert this integer back to a double. So just put 0.0 at the end. So in this case, it would like compare 1.0 equal equal to 4.1, something like that. And in that case, well, that's false. So we know that that z is not an integer. So we're just trying to make sure that it is actually an integer. So if they're the same, then we could go ahead and say, okay, while we found our whatever triple, we could print f. Man, this exam has a lot of writing, just print f. You better be, yeah, if you wanna practice this, write print f like 5,000 times and then you can go faster. All right, so we print off our three numbers. So x, y, and I guess I'll just use i or I could use z and then convert it to double whatever. And then I can just return after I print. So after I found the triple, I can just return. I don't need to look for any other ones. There should be only one valid solution anyways. So don't have to try again. And if this one is confusing, other ways you can do it is one of the other ways I wrote it, I said if z is equal to, if you remember that function, this means the same thing. So the double z is equal to, if I just take the double and truncate it, maybe that's easier to read than the integer conversions, mean the same thing, but I could rewrite my if statement that way if I wanted to. And then here, if I make it to the end, so I try all the y's, I don't find a solution, then I can just print f said no solution and then a new line. All right, whoo, questions about that one, yeah. So you want z equal equal to int z? Yeah, that'd be fine. So that's another way to write the if statement if you want. I'll mean the same thing, just whatever makes you feel good. And then some question that I'll take up later about using a void function, we should only use pointers. So I'll take that up after, but any questions about this one? All right, let's see if we can squeeze another one in. Oh, this one's fun. So complete the definition of a C function, largest sum whose prototype is shown below. The function takes an int array type list with count elements. So the list in this example would be this full thing. And in this example, our count is one, two, three, four. Whoops, five, six, seven, eight, nine. So in this example, the count would be nine for that array, right? I hope there's nine elements, yes. All right, so it says takes an array list with count elements, finds the largest contiguous sub-array containing at least one element in the array which has the largest sum. It returns the sum it found. For example, in the figure below, if the list pass to the array is what it says, the function returns six as this highlighted part is the largest sum, which is six. So four plus negative one plus two plus one is the largest sum. So if I tried another sum of numbers like this, this is like negative two plus one, negative three. So that's what, negative four. So that would be not the largest sum of all this. So for this, easiest thing to do is probably think about, okay, I should probably just compute all the sums of all the sub-arrays. So I need to figure out how many sub-arrays there are, including, I assume it's including the actual array. It says it has to have at least one element. So it could have an element. So I could have, you know, a length of just one as my sub-array. So I could compute the sum of this, the sum of this, the sum of this, the sum of this, this, this, this, this, this. So I have nine choices for sub-arrays of length one. And then I might also be like, okay, well, what about sub-arrays of length two? Well, then I have to do this, these two, these two, these two, these two, these two, these two, like this. And go all the way through them. And then for length three, it's like this, this, this, this, this, I'm just getting messy now, something like that. So you might develop a pattern for this. So it's like, if I'm doing sub-arrays of length, like if my sub-array length is three, well, I start computing like the sum of three elements, starting at zero, and then starting at one, and then starting at two, then three, then four, five, six, and then I stop at six because I would just go all the way to the end there. And then I have three. And then so you might just have a loop. And here you know the array length because it tells you that the entire array length is count. So if I think about it that way, it's like how many different lengths, like you just break it up into parts. So you can have multiple for loops. So the first part could be like, okay, well, I need to compute the sum of different lengths of sub-arrays. So I can just say, let's use black. So I could say four, I need to compute different lengths. So maybe I just make my loop variable length, started equal to one because I have to compute at least one element. And then I could go all the way up to length is less than or equal to count, which is like every element in the array. All right, so that, let's put an end bracket down there. So that gets me part of the way there. So that gets me the idea of, okay, well I've started one and I have to compute the sum of all of the sub-arrays of length one. So here, if there's sub-arrays of length one, I have to start at index zero, index one, index two, three, four, five, six, seven, eight. Like I have to go through all of them, right? So I might notice that no matter what the length of sub-array I'm computing is, I always start at index zero, right? So I might just say, okay, well, what is my first index? So the first index would always be zero, no matter what the big length of the array is, if I'm computing all of it, I still have to start at index zero. And then I have to go all the way up to essentially, so in this case, if I'm doing it and my length is equal to one, and my count is always going to be equal to nine, I should go all the way up essentially to eight, which would be, in this case, since the count is nine, I can argue that I need to go all the way up to, I could just guess, count minus length. Like I need to go all the way up to that index and include everything. And to give yourself a sandy check, I would check at least two cases. So that's when length is equal to one. So let's see what we need to go up to when length is equal to two. So I'm computing like this, this, this, this, this, this, this, and this. Might notice that there's eight different pairs of that. And I have to go all the way up to seven. So I go up to seven. So my last sum of two elements is index seven plus eight. So my guess of going up to count minus length seems to be correct because count is still nine, minus length in this case is two, which is seven. And that's what I have to go up to. So you kind of have to relate them together. So I could say first, so the first index is less than or equal to the count minus the length I'm currently at. And my writing is getting worse by the minute. So do not write your exam standing up if you can avoid it. So some, oops, so something like that. So now I know what the first index I need to start off and I know how big my array length is. So I can compute now my sum. So initially, if I sum everything together, good initial value for it is zero. So I could say I have another loop. Oh God, this is loops on loops on loops. So I have to compute the sum of, in the case of two, like I have to compute this sum. So I know how many elements I have to go. So it is zero up to and not including the length. And then I just kind of shift them over by the starting index. So I could say for int equals zero, oops, in equals zero. Yeah, that's not good. I have to give it a name for int i equals zero to i is less than the length of the sub array. Then I just have to do sum plus equals what's the, so it's called list. And then everything starts at first index. So it's just first index plus i. Or you could say i plus first index, whatever you want. So to sanity check that, you can say, okay, if the length is currently, let's say two, then say my first index, well, it could go all the way up to seven. So if it's seven, I want to add together or index seven plus index eight. So here, my length is two. So I would be going from zero, then two for this loop. And then in this case, since first index is equal to seven, I would add together list seven plus list eight. Something like that. But this question is pretty long. So that gets us our sum. So we're almost there. So remember, the point of the question was to get the longest or largest sum. So if I need to get the largest sum, maybe I just, I'm thankful I gave myself room. So I'll need a variable to keep track of it. I don't know what it is initially. So maybe I just give it just some super negative value. So I just say something like that. Doesn't matter. No, you haven't learned that, but there's like a macro that's like the most negative value you can get if you wanted to. But for the purposes of this, probably negative sum large number gets the idea across. So I go through, I calculate the sum. After I calculate the sum, I just need to check if the sum is greater than the largest I've seen so far then the largest is equal to the sum. And then after that, I believe I'm done. So then I just say return largest. And that would be done at the very end of the loop. So it'd be something like that. That's a lot of writing. All right, any questions about that? I thankfully made it through it. Yeah, I'd have to get back to you because I can't think of that off the top of my head. But like it's an exam. So first thing you can think of, write that. All right, we are now out of time. So just remember, pulling for you, we're all in this together.