 Welcome back to some 105. So hopefully the rest of your exams have been going good. So, what shall we do? Any burning questions anyone has? I'm monitoring the discord so we can do that or I can do other exam questions or whatever you want. Oh thank you. All right what shall we do? Oops I need to plug this in. Welcome to the void of review so anyone have anything pressing that we should go over? Saw a bunch of piazza posts asking about quicksort and insertion sort don't have to know them don't worry about it um then question about strings multiple choice sure you can ask anything you want in general or we can do exam questions whatever you would like and luckily uh the exam we wrote the exam quite a while ago so I forget what's exactly on it all right we got a dynamic memory 2019 question 11 all right let's go check what that is maybe it has dynamic memory maybe we can uh do two things at once let me go all right so 2019 number 11 in general the bubble sort algorithm can be explained in two steps for each pair of adjacent elements if they are out of order swap repeat the first step till no swaps are done write a function bubble sort link list that sorts a link list using the bubble sort algorithm the function has one parameter pointer to the link list assume that this pointer is not null this function will modify the link list in place so that the values are in ascending order i.e one comes before two the definition of link list and node are shown below you must abide by the following constraints failure to meet the constraint will result in a grade of zero the function must not modify the next pointer of any node you cannot create any other data structures another array link list your function cannot call other functions your function must not call a or cause a segmentation fault do any drawing here so we want to do this one it's it's a strange kind of question but i mean we can yeah so if we do this question so because my initial thought reading it was like oh okay well let's just go ahead and change the next pointers um and like swap them swap the nodes but we don't have to so this is trying to tell us that we should probably swap the values instead of the next pointers so writing like a helper swap function it's probably not the way to go so this will probably look like our other bubble sort so the way it usually goes is well usually um usually it's like a do while loop because you want to go over and over again and you're just trying to keep track of the number of swaps you have done so we could create a boolean whoops that is a large thing so let's create a boolean just to keep track if we have swapped in like one entire iteration so we can create a boolean called uh swapped and we can initialize that to well well doesn't really matter we'll initialize it in the loop so we create a variable called swap then we could have our do kind of like i think this is how we wrote it in the lecture so we could just say swapped equals false and then we could go ahead and actually try to swap all the values so this because it's a link list we can't just write like our normal for loop that has uh array bound so we have to iterate over a link list and how we did that we did that a bunch of times during the lecture um we start off with a node pointer called current and it starts at the link list head so that should just be list and because it's a pointer we have to use the arrow so i think on the exam they'll just let you draw the arrow or if they're really being pendantic we can go ahead do the uh dash greater than doesn't really matter i just draw the arrow uh so the first element is called the head and then what you probably commit to memory is like to iterate over every element of a link list it's just well current does not equal null then we iterate we do our thing and then we can do current equals current next so that will go to the next node over and over and over and over again so let's give us some room because we know that's pretty much all we're going to do and then here we can do this loop while uh while swapped so if we have made at least one swap we got to do it again all right are we all right with that so far so this is a little bit trickier since it is a link list so current is like the current value we're at and we also want to check the next one but like check the next one to see if we need to swap the values so turns out um we have to be a bit careful here because we might be currently at the last element of the list so the next might be null so we can go ahead and create another variable just called whoops node next equals current next just to save ourselves a bit of writing and we can go ahead and just update the variable there instead make that a little bit shorter so here if this was just the normal bubble sort like we just check the element above make sure like just check if it's out of order and if it is we go ahead and we swap them so here same idea but we have to make sure next does not equal null otherwise we'd be accessing uh invalid memory if we try to go ahead and access the data so if we go back in the node the data is just a field called data so we can access it by just um we can access it by just you know doing the arrow to data so then we get the actual integer so we're just writing the condition to swap so we want to swap if current arrow value so if the current value is greater than the next value which we're allowed to do because we know that next is not null so remember the arrow sign so if we do next arrow data well that's the same and like I'll write the type so the type of next is a node pointer so writing the arrow is the exact same thing as if we just did we dereference next and remember when we dereference a pointer the resulting type if you want to remember that way is you just remove one of the stars so if we dereference next and it is a pointer to a node then it is just a node then we can go ahead and access that value or that variable within the structure by doing dot data so just remember that these two things are equivalent all right so now going back to the question this is our whole if condition so this makes sure that the next value is actually valid and that they're out of order so if they are out of order then we have to swap the values so like with swapping we have to create a temporary variable to capture the value of let's say current data and then we can make current current data equal to next data so if current was two and next was one well we just made current one and we want to make next two and temp currently has that value too so now we do next data equals temp on that if and we want to make sure that we set swapped equal to true so that's all we got and uh this will end so scheduled for two hours if we're out of questions and we don't want to do anything we can end early but i'm here for at least the two hours um so other question can clarify what we need to know about searching and sorting so like with this question that says hey here's the idea of a bubble sort you should know the idea of bubble sort and selection sort and be able to implement them so like this we implemented bubble sort they can't give us an idea of it and give us some hints um but we implemented bubble sort and then question should it not be pointing to value instead of data if you define that in the if condition uh sorry my writing is a bit sloppy here let's move some stuff oops so value i don't think is actually anything so not be pointing to values of data so the the field is called just data unless i called it value no i called it data oh sorry yeah yeah here sorry in the if condition it's not called value it was called data sorry i usually call it value data data all right there we go yeah so question uh so bubble sort generally just wouldn't we want to swap adjacent elements right not connected to for loops and arrays nope if uh bubble sort just a way to sort things doesn't matter if it's arrays or linked lists or anything else as long as you like there's you can access them in a certain order then you can sort them so linked lists you know we can access them in order arrays we can access them in order of their indices so we can go ahead sort things in order that way um but yeah bubble sort idea behind that is we scan the we scan like adjacent elements in the array if they're out of order we swap them and we just do that for not every pair but like just adjacent ones and then well if we swap something then we know the array might not be completely sorted yet so we just do it again and again and again and again until it is actually sorted and the idea behind that is because um it's sometimes called like I guess no one's actually like tried to prospect for gold or anything but uh it's like sinking kind of so like values kind of migrate towards where they're going to so if you think of the larger numbers it's heavier they kind of migrate at least one space towards the end every time you go through the loop and then all the lighter values migrate one space up each time you go through the loop hopefully all right so we got that one another one question 11 for 2018 all right question 11's are popular um whoops 2018 19 where we got 18 2023 all right let's take a look at this function oh midterm not final okay sorry I don't I don't have the midterm ones uh pulled up so I can't do those ones I can try and discuss the idea but I can't pull them up on my thing let's see what was see if I can look it up on my other monitor okay so yeah that one's weird I think I kind of covered it during the midterm review session but yeah it says uh I was posted in chat ready to see program that returns a randomly jerry prime number wait what exam is this 2018 okay if you actually look at the exam it says write a program to determine the quality of rand and that's prime numbers okay so for that question they're like I don't think they really grade by how clever it is first solution that pops out is just randomly generate a number and have a function that called is prime and um then if it's not prime just generate a new number and try again over and over and over again so that's one solution you could do all right so we have a question of dynamic memory too so here let me whoops all right anyone with anything else otherwise I can try and tackle dynamic memory type things so right now I can look at the oh sorry someone was typing is that I mean give you an idea with how to solve that I can't find exactly which one exactly which one that actually refers to oh okay yeah I see that question so it's it's actually question 10 on that midterm not 11 but yeah the solution does just what I sound so generate a random number so between one and max range so you can just mod it with max range so whenever you mod it'll always give you a result from zero all the way up to that number minus one so that's why they add one there so that the lowest number they'll get is the one and the highest is a is max range um technically they shouldn't want to generate one because they would say that is prime oh they have an if condition to very one from by coming prime okay so yeah there's having a loop that just goes over and over and over again all right so so we can go so you can stop me if you want me to go I like revisit anyone in more detail um these I will just go over fast just so we have it completed and I can comment on things uh during the lecture so one the random care character one I don't know why they're modding 27 should be 26 at least in the alphabet I know about there's only 26 characters um question two says in a single statement declare initializer array 500 integers called numbers where the first four numbers are one two three four and the remaining are all zero so that means we have to we define an array they say it's called numbers and we can give it a definite size so says there's 500 and then we can just use the initializer array syntax so it's just curly brackets and then one two three four and then if we end it there by default if we don't specify the rest of them they're all zeroed so that's just what the solution has this same idea um so with mostly it says write a single write a single c statement terminated by one semicolon that declares two variables name plant one plant two of the type of the following data structure it's called plat in the same statement initialize plant one's name and height two with cactus and the other one to rows and some number so if this is confusing you can start with something simpler and then work your way up to it so assume that i want to initialize these things all on one line and they were integers instead of plants well then it would look something like int plant one wow maybe i can write a bit better int plant one equals one and then i could do a comma to introduce another variable and i could have plant two equals two with a semicolon so that's if they were integers they are not they are structures so and the structure is called plant so instead of int i can just replace it by plant hello cat thank you and then because i'm trying to initialize a structure same way to initialize arrays i can use curly brackets but with initializing a structure the order matters so the first value i give it is going to correspond with the first field in the structure so i can't write something like if i want to add cactus in five point three i can't write this and yes cat so i can't write something like that because it would try and assign five point three to a char name and then it would assign cactus to double and i'll see if the cat's being being helpful so in this case i'd have to do it in order so it'd be cactus five point three and then same thing plant two would be rows and 10.2 so okay oh geez all right and there's a cat although he looks weird because his eyes are green and there's a green screen oh we got another cat we got a kitten but yeah he looks very weird because i'm behind the green screen and his eyes are green they look weird all right yeah and the cat likes to come out while i'm trying to do stuff all right question four okay question four weird so let's give question four question five so this kind of has to do with dynamic memory and things so question five is actually pretty good the the code has a serious mistake in the git names function so things to note there is this structure called student with only one field so a char pointer to last name so that should probably be a c string so it should point to a null terminated array of characters so there are git name functions well let's just see what does it has a for loop that goes four times so i will go through all the way so it'll be zero one two three and then it will just say enter last name and then scan f with a string format specifier which is always a bad idea but that's not the serious mistake in this code then it goes ahead fills in that string for us with whatever the user typed out and then changes student's eye so it's an array of students changes student eyes last name to be equal to last name so then in main what does so declares an array of students so then declares a very large character array then it calls git names on that and then prints off all of the students last names and what happens is well if we enter four last names then it just prints off that the last name for all the students is the exact same so it says revise it by crossing out parts and writing a revision above such that the outputs above a desired you can assume the maximum size of a string is 1024 so what is happening here so what is happening here is let's draw our variables so in main wherever we start executing it so of course start executing in main so we'd create an array of students that would be on the stack so we would have students oops students zero students one students two students three and all these are just you know a pointer then we just have a giant array of that called last name so I was called last name and it's just a big array then in git names what happens is we pass it last name which will just be a pointer so it'll be a pointer somewhere like at the beginning of that array because of our good old friend array decay and then we have students same idea there so what happens here is we ask the user to enter a name and then we go ahead and we fill it here so like for the first user we would have put in the string and and then we set students last name equals to last name so this is last name here so what we're doing is we're making students lot dot last name which is the only thing in the structure point to the beginning of the array so then next time through the loop then we ask for a last name again we scan f and then we reuse that pointer so last name is still pointing to the large section of bytes so they enter in this case brown and it would overwrite that because well starting at the same memory location so brown something like that and then the next thing we do is we update students ones last name equal the last name and because it's a pointer it's all pointing here so save you the time they all point to the same thing and the last name we update was the one that gets printed over and over again so we print out something like that so because of that well we need like separate space to store each last name is kind of our issue here so because this is a function in get names so I need to you know copy some memory somewhere in this get names I probably need to get some memory and then copy the string the user entered somewhere so it doesn't interfere with you know whatever they input something else next time so I also like need it after I use the get names function because here I use it to print everything so this is where dynamic memory comes in so I probably need to use some dynamic memory whenever they enter the information and then go ahead and copy it to it so like this print in the scan f that's fine but now I have to take what is currently in last name and I have to copy it to some like new memory location so what I probably want to do is I can go ahead and like there's different things maybe I don't want to even use string copy so I don't have to use that last name parameter at all they given like I can go ahead and I know I'm going to go ahead and I need to store some memory for students eyes last name and I need some dynamic memory so then I can go ahead and malloc it so malloc that will return me a pointer back to back to the starting bite of our starting address of however many bytes I go ahead and allocate and it says that well you're guaranteed that you know we can it'll fit in 1024 so we can it's an array of char so we could do size of char times 1024 and then we could go ahead now we have memory we can ask a user to enter a last name and then instead of filling in that last name we can go ahead and we can just directly give it the students last name to fill in so something like that and yeah that that malloc line is going to be required so if you didn't have that malloc then whatever like we're using it in main so let's go back so main starts right we have students we can go ahead all the students have is a pointer to a char that should represent a C string then we had just some array just on our stack that originally we used to get the input from the user and store it there so it would be stored in last name but that didn't work before because it's shared with all of them all right so we only get the last name entered so we call it get names and then we have to print all the students last names so get names sets aside should needs to go ahead and set aside that memory so that when a user enter thing enters a name it has some place to go so just because it's written like this we need to use malloc because the memory needs to exist longer than the function so if we did something like I don't know we did like we did char or here we did like char oh actually not none of these are going to work so there's no way to put it such that it'll live outside the function if I declare just a char say I even did like a 2d array of for each last names I had something like that so I could store all of them in say 2d array well by the time this function get names actually returns at the end here all that memory is now gone and it's invalid we can't use it anymore so we have to use malloc here because I need to use memory for this function and it needs to still exist and be valid outside of the function so that's why we need to have malloc here so what that will do now whenever we call the function so here's all of our students now we actually don't need that last name so wait first off are we all okay with what the problem was before and then I'll go over why this solution works so you could malloc space for a 2d array but then it's just kind of a bit more strange but yeah you don't you know this this doesn't need a 2d array so we're okay with why this was a problem before why we got the same last name every single time a when in discord at least hopefully we're okay at this point well hopefully just assume we're okay with why this was an issue unless someone tells me otherwise so now with the malloc what's going to happen let's start off start us off at the beginning again so there is our change so now if we execute main we get space for four students that just have their last name then we have this giant last name on the stack that we're not going to use anymore so this is just like just a bunch of characters we don't really care then we call git names and it gets a pointer to students so it can go ahead and access them then in git names we go our for loop so i starts off at i equals zero then well what we're going to do is we're going to set so now these are not all not set so we're going to set students zeros last name so whatever it's pointing to over just to some big chunk of memory so this is 1024 like an array of chars of for 1024 so we make it point to that starting address so that's what malloc will give you back right and then we enter the last name in this case who is it hand uh so and since we scan f and we give it like a pointer to students last name that we just malloc it's going to fill in the value exactly at that memory location so it would fill in hand here and then well turns out we don't need this line anymore either so fill it out and then we'd be done we'd go back up to to the loop now we're i is equal to one so now we malloc space for student ones last name so we malloc that gives us a big old char array just somewhere off in memory we're not sure we're to sure it's valid so we get that and then we ask the user to enter a last name oh sorry we would then point students one to it then now when we scan f we're taking the string the user inputs and storing it at students one last name which is that space we just malloc'd which would not overwrite what we had before because it's a different pointer so now we have brown there and then similarly whenever we go for i equals two and i equals three that would get more space on the heap something like that and then whenever they enter the in jamal it is all stored there no issues whatsoever it's all different memory it's and it's all valid so then we'd be done with the function thank you for your insight cat so we'd be done the function come back and then well now when we iterate through this we go through every student's last name the pointer values are still around and whatever they're pointing to that memory is still valid we haven't freed it yet right it's always a rule with malloc you should only free it when you're like you should free it as soon as you're done with it so you weren't done with it for the function we have to remember to free it but not for this question because well we're only writing the get names function so here it would print off each student's last name so it would point to that malloc region of memory use that as a string and because we use scanf it's all it's all null terminated it's a string and we're good so any questions about that or anything anyone didn't understand about dynamic memory hopefully that that got by your dynamic memory and everything so we're good with that all right so hopefully that fixed that so question six that is quick sort oh we can skip that so wait I got another one oh quick unrelated question I understand if we're pointing to a string literal we can't change the individual elements of the string only what's pointed to however would still be possible to index a string literal or is its functionality also limited so for a string literal you can access the elements like get the value back you just can't change the values so if you think of it like as a pointer you can de-reference it read its value but you can't reassign anything so you can't change it um and also for clarity's sake could you also quickly go over when something is a string literal and when it is not so yeah the rules for that is well basically anytime you see this so anytime anyone writes something like this in C that is a string literal so that would be stored somewhere off in memory but if you do something like char temp array equals that what that's going to do is there's a special thing here where it will actually copy that string literal to the stack for you so this is still a string literal but because you're creating an array on the stack well it needs to be on the stack it's not just off somewhere else in memory like where the string literals are actually stored so it's going to copy that the contents of the string literal over to the stack for you so then you can go ahead and modify it so if I just did char s2 so a pointer called s2 that equals this well I mean it's still a string literal but now I'm pointing to it I'm not like the variable on the stack is the pointer so the pointer is mine it's exists as long as my function exists but what I'm pointing to is the string literal well if I have an array there the entire array is on the stack so I'm initialized you can think of it more as like I'm initializing my array to be the same contents as a string string literal um and then another question if we pass it into function can we still use char star in the parameter so for that same rules as if it's an array or if it's anything like if you have int whatever well let's give it a name int nums whatever int nums 50 and int star whatever like if we have a function called foo and we have int star nums or here I'll call it something different let's call it x's so if I call foo with nums well there's going to be array decay in that case right so then I just do pointer arithmetic on and I just assume it works like that and if I do that instead in my function signature it doesn't matter it doesn't change anything whatsoever so same rule with uh same rule with the string literals so if I have my if I have my array of char's on the stack and I pass it to a function then it would be a char star so it'd be pointing to you know wherever my array starts and then I can go ahead and I modify it whatever because the address is not the string literal address so does that make it clear what if we have to write a function that has char star as input can we assume the string was not declared as a string literal yeah so if it tells you you have to modify the string then you're fine so if we have something here so char so this s2 here this is s2 points directly to string so I can do if I want to see you know the value of index one which would be the a here I can write that in my code I could do char c1 equals this and then you know it's going to make it equal to a lowercase a the only thing I can't do is I can't do I can't try and like change the n to or let's say that same character I can't change that a to a no so I can't do something that that is a no no definitely can't do that but if it was declared on the stack then I could but only this weird rule for string literals okay related if you're question 14 from 2018 we could also do recursion by directly increasing the index and then comparing string index as because in the marking seem so in the solution is marked specifically reserved for changing the string pointer so changing the index wouldn't work here let's go so 2018 20 so which question is that 14 so it says consider the following function that returns the index of a char of a char c in a string string wow that's redundant that's that's not confusing at all the read so the string is called string the position of the first c in the string returns negative one if it does not occur in the string so then it says write a c function recursive find index that does not use any loops and yet behaves like find index in the function above your function may have additional parameters but at bare minimum it must include the parameter string in c so I guess they have that so we don't have to write a helper function but let's see what you're talking about so sorry what's your question for this my directly so what does the grading scheme say yeah so in this you do have to remember and somewhere in the parameters otherwise otherwise you don't know what index so I think you're thinking about like hey my recursive stuff could be going through the smaller string until I find the character but then you don't know how many steps you have taken because it needs the index of that character all right so we got a solution so here let me find my mouse so you have written a string and so here I will write what you wrote and we can take it up so the solution posted in the chat is I'll just write it out so we declare a function called find char so it takes a string char c and int index and then you know here well this is actually like our helper function right so we could have in find char that takes a string char c and return find wow sorry my writing is getting bad find char helper string c zero so you could do that and then in the body so with all of our recursive functions they all work the same we always want to make sure we have our base cases maybe multiple of them who knows and then our recursive steps our base cases are in this case what you have written is if I'll just write it verbatim and we can go ahead and we can go ahead and fix it up because I see some issues already so if this is equal to null actually that's not an issue so if we're at the end of the string then we can just return negative one like it's not found else if we could have string at index so otherwise we have found with two equal signs sorry we have found the character then we can just return the index else now we're in our recursive recursive step right so our recursive step is well we'll just make like the smaller problem is well the current character isn't the one we're looking for so we'll just make the string one shorter and try and solve that problem so it would just be find char in this case this is called helper for string so it's still the same string still the same c and then we just advance the index by one and ending bracket so could you go over what the point of the helper function is yeah so in here um our helper function like if we just wrote find char like this it doesn't match the function signature so it says write a function called recursive find index that just takes two parameters just the string and the char so when we write the recursive one we need another parameter that is keeping track of what our current index is so our actual recursive function is going to have index as one of the arguments here so because that's what we need to have to actually write our recursive function well there's an extra argument there so typically what you do is you call the function with like the recursive function with everything like the helper function or in this case yeah or find char or whatever we wanted to call it and then our helper just calls that function and starts at the whole problem so our first index should be at index zero so that's why we need a helper function here yeah so this let's see so this solution would be okay one thing I noticed here is that so if you look at the string at index and if it's equal to null typically we use null if it is a pointer not if it's like the null byte so slight difference here they're actually all zeros and this will work but technically if you want to look a bit more like it should be the null terminator at the end of the string is the character just backslash zero so I would just do that little change but this would be a fine solution so part of the marking scheme it says remembering n somehow in the parameters that's just the index so you have it right here so that would be fine and then yeah so this is not the only way to solve it so another thing you could do is well instead of like using the array indexes I can go ahead and I can just like advance the pointer of the string once so I point to the next character so instead I could so I could do this entire function like this I think is what the person was suggesting so you said not to have an index well show why that is a bad idea so let's change it a little bit and then here we could just dereference the string so it's pointing at the first character so if it's the null byte then we're done otherwise if the character we're currently pointing at is the character like if we dereference it then well now we have a problem we don't have index anymore so that's not good and in our fine char helper you probably want something like we do string plus one see something like that so that will get us most of the way there so that's a way to make the problem smaller like instead of just changing the index and using the index all the time we're just pointing to the next character and like just making our string smaller that way but we still need to return the index at the end of the day so we still need to keep track of it so you can do it like this but we just need to reintroduce our index so in index so we could have something like that and then we initialize it to zero we could keep the rest of it as it is but we just have to remember here to do index plus one as well so now unlike before where we just had to do index plus one now our recursive function looks a bit weird we have to remember that we have to do string plus one to move the pointer one forward and since we're keeping track of the index we have to add one to that too but the solution you gave would probably get full marks because it would work I would just say just that null should be like the actual null character technically but it should be fine and yeah okay answer that question 13 on the 2019 am I on the 2019 I already forgot what I'm at no I'm 2018 2019 question 13 what we got here complete the definition of a C function uh permutate whose function whose prototype is shown below that prints all permutations of the string string for example if the string passes the function is defined as abc a call to it should print all of that you must use recursion to solve this problem asterix one in fact the non recursive implementation would be more challenging so hint you may wish to define a helper function and you may also find the swap function defined below useful so we want to have permutations of literally everything so what we could do there's a few ways we could tackle it wow so the solution is strange yeah I can't think of a better way to do it than the solution so the solution keeps one thing constant just tries to swap everything around and then makes it one smaller then swaps it again and prints it so if you have something specific with that but um yeah just try reading the solution to that one um if you still trouble with that I will help you again later but um other questions otherwise I will go back to some other things I saw that were useful but yeah if you want help with that one message me uh after because I'm trying to do things more general now and try and uh try try to make a bit uh things that we can definitely do a bit different than the solution posted but yeah I can't see anything other than the solution so let's see this one question nine right a c function called diagonal sum that returns the sum of both diagonals of a square n by n array named square diagonal elements are circled in the solving figure note if n is odd the central element should be counted twice in your solution so with that the solution that is given is kind of there's a lot of writing so I'll tell you the way I think of it so the way I think of it is we just need like a single hopefully just need a single for loop right so single for loop I know that the coordinates of this diagonal right it's like one like one sorry zero zero because you know arrays are zero indexed one one two two and then in this case we go up to three three so that's like the first diagonal right and then the second diagonal well the elements there are three zero oops sorry zero three my bad row column so then we have row one column two row two column one row three column zero so look something like that so I just need to add up all of these numbers so if I'm summing things generally I know I have to create a variable called sum I can initialize it equal to zero and then uh unlike the solution I should be able to just do this all in one loop so my loop could just look like four int i equals zero all the way up to i is less than n plus plus i so I know that if I access square at i i and I go and I do that for this entire for loop well that would access these elements here right so if I want to go ahead and update the sum with them all I have to do is I could write sum plus equals square i i right so it sounds a bit like a pirate and I could also make it even so that's one diagonal right so everyone agree with me that should get this diagonal here now I could just stay in this for loop and do the other diagonal as well so the other diagonal well it's to access the elements of the other diagonal it's square i so it starts at i and then to get the column right it's just kind of inverted so it's just n minus one minus i and to verify that we can check so in this case n would be four so first time you know n would be four so four minus one is three so we get well this would always be three so then we get three minus i i is zero so we would just get three so that would give us this the first element so row zero column three then next time through i is equal to one then we would get well row i and then three minus one so that would be two so that would give us the next diagonal so it can look just this simple so sum plus equals square and that's the entire solution so here I can make this a bit more compact and then at the end of course we need to return the sum but that's what we could do for the solution so the solution given really really wordy so the solution has like a row like integers for rows integers for columns and then while loops um I find it weird so I would just do this so everyone okay with this solution should save time and hopefully be faster to think of all right any other general things or I can carry on and yeah you can feel free to ask any questions you want about the exam because I forget I forget most of it so I can't spoil anything but yeah but for most of these questions there's lots of different ways of writing it as long as your solution works um then you're good unless the solution specifically says write a recursive version of this then you have to write a recursive function otherwise you will not get a good mark and yeah this binary trees in other exams this year it's not included because you'll be doing that in one of your future courses and it was just confusing in this course so it was cut so binary trees don't worry about them binary search would be in scope for this exam though so um that like we covered it right before so that binary that searching lecture I did all good stuff but yeah don't don't worry about binary trees don't worry about insertion sort don't worry about quicksort although like that just uses the idea of recursion so you don't have to remember it or anything like that but you should kind of understand the gist of it should you know how to do binary searches recursively I mean couldn't hurt it's not that much different than doing it iteratively so it's good good to practice um it should be able to do it such that like you're not committing it to memory all you should remember is that the idea of the binary search is hey we try and cut it cut the array in half all the time because it's sorted um but aside from that you don't really have to remember anything you should get to the point where you can kind of implement it um other question when we declare a char star string for example my string can we use it like we would the name of an array like do my string plus one to access the address of the second element etc yep you can absolutely do that so it's just an array like anything else so the only thing that makes like strings special strings are just arrays of characters the only thing that makes c string special is that the last character is the null byte or zero so you know when the string ends but aside from that there's nothing special you can think you can think like I just think of strings as just an array of characters and it ends with an all byte that's it so yeah so pointer arithmetic rules all apply if you do like here was the example so if you do char star my what is it my my string uh say here let's make it equal to some string literal so that just means somewhere in memory there's going to be like an array of characters so let's draw it out like an array wow that's a really even box huh so we can draw it out like this and it would have the character h e l l o and then the null byte so it would just look like that somewhere in memory and then my string if you were to say what character points to it would be pointing to this so just be pointing to the first capital H and if I do my string plus one I'm adding one to a pointer so it's a pointer to char so it will go to the address of the next character so my string plus one would just like if it was an array of ints it would point to that lowercase e so yeah yeah the c strings just arrays of characters all those same rules apply if it's easier to think about it um just think about it as ints usually people think about especially if you're starting off easier to think about in terms of ints so make it simple think of what would happen if things were integers and then just change it so now instead of integers they're just characters but all the same rules apply pointer arithmetic arrays the only difference is that characters are smaller than integers in memory but it doesn't matter you don't need to know that if all right any other general questions or anything else fun we can do um sort and yeah also on the exam uh like I said in there do not do not do not remember bogus sort or bozo sort either uh in question wait so you we need to clear your string you can do char star and char no you can't do char so I can't say just char like this that would not work because the string literal it's a pointer to that string literal so this would give you some error saying that you can't convert a char star to a char but if I wanted to I could make it an array right I could just do square brackets like that and then I make it an array and then see you'll figure out the size for me and put it on the stack so in this case you know the size of it would be six chars because he l l o and then the null byte and then we can go ahead and modify things and it would also not point to wherever string literals are stored in memory it would just be on the stack and you can go ahead and modify it but yeah that that's the only difference but same thing if you use it in function it's now no longer an array it it's only a real array in the function it's declared in it outside of that just becomes a pointer because of our array decay all right other questions comments concerns or other things we definitely want to go over oh that is a large thing all right we got insert at tail that might have not copied and pasted on can you explain how an insert's a node at the end of the link list so if you want to post like code in discord do three of the tilled things so like it's like the key right below escape and paste the code and do three till the things to end it and it will format it as code oh not like that so three here i'll take so we want to insert till so this is from the textbook so okay so what what specifically do you want me to like do the implementation for that or what or explain what that one's doing since it's a bit stranger than what we usually do all right um so i might have to follow up after because no one can see anything so it's probably not not that effective but basically what that does is um so create node will return a pointer to some new node for value make sure that we actually have enough memory for it that's why it checks for null and then starts off at the head of the list and tries to iterate all the way to the end has a weird while loop that checks like if it's inserted or not so it'll iterate through everything also check if it's inserted so if we found the last element aka current's next is null that means this is the tail so we can change currents next to point to the node we want to make the last and the list tail point to it and then we've inserted it and then that would break the while loop otherwise if it's not inserted that means that well we didn't even enter that while loop so the list could have been empty well in this case the list is definitely empty so if the list is definitely empty then well there is nowhere to put it so this is the only element in the list so in this case the list link list structure keeps track of the head and the tail so this is the only element in the list well the head and the tail both point to this because it's the last one or it's the only element in the list so it's both the first and the last one does that make sense we can try and see if we can find a link list question too but yeah for most link lists be careful of like the edge cases of like hey the list is empty or yeah mostly the list is empty or what i'm looking for doesn't actually exist or things like that and yeah there's there's more than one way to write that function too i probably wouldn't have wrote it like that and just like checked hey if immediately if head's null then we can go ahead we can insert it we're done and just return yeah functions a bit weird i'm not a big fan of that one um all right anything else we want to go over where we got sort of ray let's see if we can find link list oh we can do some recursion sure recursion's fun right so again yeah this is the 2023 exam so it says write a recursive function named print reverse that reads a sequence of positive integers and outputs the sequence in reverse order the input sequence ends when the user enters the number zero assume that the user always enters valid input your print reverse function must be recursive otherwise the solution will not receive any marks you may not declare any arrays in your solution blow is a sample input of the execution so with this well we have to think so every time we call this function we want everyone to enter a number right so we would start off by just saying print f could say enter num and then we declare int num don't have to initialize it to everything we're assuming that the user enters valid input then we can scan f percent d because that's the format specified for an integer and we need to give it the address of num so i can go ahead and update the value for us so just remember if you scan f and it's a char i would always do like a literal space before the character format specified to get rid of any weird issues so but if it's not a character we don't have to worry about it so we get a number and then we can go ahead and well check our work our like base case so our base case is we stop if the user actually enters a zero so we can we can just go with what we think of first and then try and refine it as we go on is one way you can do this so usually with this helps to have a scrap piece of paper around so if the user enters a num well that's kind of like our base case that's our base case right then we're done we don't want to do anything anymore we're good so in this case we can't just do a return because then nothing would happen although i mean we can try with that so this is our base case we could go ahead and print what's it want reverse sequence so we could go ahead print reverse sequence and um and well if that isn't the situation we're in so we're doing the recursive case well we want to go ahead and ask the user for input before we could or we want to call the recursive function again right we want to print off the rest of them print recursive or sorry print reverse so we do print reverse and then only after we're done with that do we print f our actual number that the user inputted so we could say reversed sequence percent d and and then num and that should be it right i think that's it here we could just do if we wanted to we know that it's zero already so we could do something like that if we really wanted to but that should work so let's run through the example and see if the example works so we'd say print reverse yeah so we do uh what multiple reverse sequence so let's let's run through the code and see what would happen so always good practice to double check so we would call print reverse once enter num first time we enter 10 so the first time through it here let's write that so in print reverse num we have a variable called num equals 10 well it does not equal zero so we skip over that and we call print reverse again so now we have another function call to print reverse then we enter a num in this case we would enter five as our number so the number is not zero so we call print reverse again so now we have another function call to print reverse and now let's try and skip a few steps so call it again they enter in this case they enter two then we would call print reverse again this time the variable num in that function call is equal to one then finally we call print reverse and now num is zero so let's continue on right here in that uh that one so num is equal to zero so now if num is equal to zero so zero is equal to zero thankfully then we would print what we would see is we would see reversed here I'll just make it short reverse sequence zero get printed out and then return so now this function call is done right with num equal to zero it's done and we're going to return and we would resume wherever the last function ended off so the last function was up here so it was print reverse with num equal to one so now num is equal to one we would print off reverse sequence one and now everything tries to kind of unwind so now we're done with that we hit the end of the function which has a which just returns from the function we don't have to write anything so now this one is done and we resume print reverse with num equals to two so now it's going to print off reverse sequence two and then that function is going to be done oops then the same thing with num equals to five or we're going to resume the function with num equal to five we're going to print off reverse sequence five then we're going to print off reverse sequence ten for the last one and then our function is finally going to be done right so that prints it off into reverse so if I so I think I did this in lecture two but like what would happen if I just move this print print f up one and yeah we do want multiple reverse oh whoops oh sorry yeah that would be multiple reverse sequence sorry should just start with this and this should just be sorry the space with that yeah sorry it just wanted all in one line so this shouldn't have a new line and this shouldn't have a new line either so it should look something like that oops sorry it should all be one line I thought it was one every line oops so it would print reverse sequence zero one two five ten and if I did something like this I did the printf first well then it would print reverse sequence zero and then be done and then print off oh it would have actually printed off uh like ten oops let's do a different color if I just move the printf up one well then it would actually print ten five two one and then reverse sequence zero and that would have been bad so if we want reversed we just make sure the printf comes after the recursive call and intuition behind that is well the smaller problem is before I print my current number I have to print the rest of the numbers so that's one way you can think of it all right any other questions or any other stuff we want to go through so we kind of yeah and we did the recap lecture before or like general questions about the exam see right if we were to reach the end of a non void function without returning anything what would happen so if you reach the end of a non void function without returning anything um c says you're kind of on your own usually by default it returns a zero uh another question why is it 9 a.m. in the morning I don't uh I don't know but I'm commuting from far away so I have to get up at like some ungodly hour so hopefully yeah you have to commute less than I do but yeah I have two exams at nine uh back to back yeah so my other course is on the 30th and this is the 29th and yeah so can we assume that helper functions are allowed unless specified yep you can always write additional functions if you want unless the question specifically says which I don't think any of them do to say only write a single function you can write as many functions as you want um you just might be writing too much also no you don't remember the exam list as it was written a long time ago would you try your best to remember a sliver of it for us uh it has questions in it those questions uh have to do with the course so yeah that's pretty crazy uh I do not know what room I'm invigilating uh what it's all in the exam center right I think this one's in the exam center I get confused with my other course considering they're like the next day apart um yeah I I have no idea what room I will be in I will go and I will do what I will is told I'm told um yeah also uh fun fact with the invigilators there if you didn't know this because some of my third years didn't know this um the invigilators we get for final exams are completely random they are not your TAs for the course in fact they might not know anything about programming whatsoever they might be from a completely different department so invigilators are purely random um so you'll have no idea who the invigilators are so unless you're asking them to go to the bathroom um they won't they're told not to answer questions that's why instructors have to be around so if any question comes up make sure if you are in a room without the instructor of your section uh your instructors are myself Selma and Bruno so um yeah try and see make sure like we should announce ourselves it should be obvious who we are um but yeah you you probably don't want the invigilators answering your questions anyways but just remember make don't don't even if don't try and ask the invigilators anything if you have a question regarding the exam just tell your invigilator to flag one of us down like one of the actual instructors unless you need to go to the bathroom or something and then that's what they're there for yeah trying to bribe them is not going to work I assume I'm I've no basis of this whatsoever but I'm assume that's why most universities the invigilators you get are like the TAs in your course uh but yeah so maybe that's why Toronto doesn't maybe the TAs did get kickbacks or something I don't know there's gotta be a reason for that this rule but yeah $20 it doesn't get you too far anymore unfortunately all right anything else about the exam I would say uh exam tips I mean there's probably going to be stuff on linked lists so unlike last year's uh exams where they covered a lot more things and linked lists were done pretty quickly we spent some time on linked lists some of the sections spent almost two weeks on linked lists so that was a vast majority of the course so I would practice your linked lists for sortings um you should know bubble sort and selection sort are the two that all sections covered um you don't have to know insertion sort or quick sort and please forget I ever showed you bo go sort or bozo sort uh we another question we don't have to free anything on this exam right um I believe if you have to free something all right so this is where my memory comes into place if you have to free something the question will tell you you must properly like free your memory so the question should state that if you don't just assume you could just like write an assumption to say I assume the memory gets used afterwards so I don't have to free it or something like that but I believe one of the again don't trust my memory I believe although I probably shouldn't say this I believe one of the questions says like make sure you free memory um properly but again I don't really remember it I only vaguely remember the questions I wrote I only wrote three of the questions maybe four uh oh I can't even remember how many questions I wrote that's not good uh keep forgetting how long this exam actually is I think it's a two and a half hour exam unless the first year ones are different but it should be two and a half hours um but yeah it'll be a long one but yeah the magnitude of questions your exam your exam I believe your exam slightly on the longer side if I had to guess it but it doesn't have binary search trees so the idea is hopefully it evens out um but yeah so if you find yourself running up against time constraints like try and prioritize the questions that are worth the most marks that you can do quickly but yeah would you say this final would be harder than previous years on a kind of high midterm average so the course this year is covering different topics and stuff and have different instructors so if you guys just learn more and can demonstrate it having a higher mark is not a bad thing um the department might come at us and be like hey your average is too high historically but then we can probably say hey um we changed how the course was delivered a bit this year changed the material and there's new instructors and hey all I ask is that if the exam's high like average is high it hopefully it's uh for good reasons right so if you all go there and kill the exam that's great that's I would rather see that I don't really care if the mark's high uh so the department probably is gonna I mean not mad but like it'll probably get flagged because it's historically and historically the course average has been low um but they also asked weird questions so we'll probably get a talking to anyways don't don't worry yourselves about that um but we'll we'll all get through it together but yeah I mean the also it's I don't know what happened to you if you like what's the midterm average for other courses 75 is probably about where it should be at so I thought the midterm average was good why didn't but yeah I don't know what your averages are for other courses various per course a ec 110 yeah what's the 110 at midterm average this year it's a skull you don't want to know 50 60 yeah so people err on making things too hard and adjusting the grades but generally that's just that's just the exams too hard or things weren't clear or something like that that's just weird um yeah I had a like truss course where I mean the midterm average was like like a 40 and the professor said uh the exam was the what do you call it midterm average is 40 he said it was the technicians exam like the midterm was the technicians exam and the finals going to be the engineering exam so strap yourself in and I believe the final like the unadjusted average for the final was probably like 20% or lower that was a fun course can't say I learned anything all right that they made the file easier but yeah I mean my undergrad was different but yeah if you get this if you get destroyed on midterm and they say it's a technicians exam and the file is going to be the engineering one then uh yeah be be very worried I've only heard that once and it was not good and yeah there's a bimodal distribution you have to justify it uh like like I said this is my first time teaching this course so uh we'll just do the exam and hope for the best huh all right any other question comment concerns yeah concentrate on linked lists um rest of it problem solving just try and do past exams uh there will be some questions too that are like hey implement this recursively so practice on your recursion practice on your linked list for sure um and then aside that normal seed knowledge structures that's all handled in linked lists um what else what else oh question about linked lists yep what is the question about linked lists here I can make myself bigger too so would you recommend brute force memorizing the linked list situations in the textbook insert note at front etc so I wouldn't bother memorizing them like on what lecture did I do yeah so like we implemented most of them in which lecture lecture 30 I did we implemented most of them um the only thing you really have to remember is like that there's generally knowing how it works is practice like for most of them you will have to go over every single element so like iterating like it should kind of be second nature that kind of iterating over every single element of a linked list just looks somewhat something like this so you have a node current and then it always starts at the list head then generally you'll have while current does not equal null then you can go ahead and you know that you can access current you know maybe it has some data in it you can do something with that who knows this will all kind of depend on the question and then at the end you know you update current equal to current next and that's generally like the basis of all the questions like you just have to access every single element maybe you do something so in that lecture we did things before we did things after um past then it kind of depends but it'll always look something more or less like this so this will be pretty much common for all the linked list questions like maybe it might say like I don't know do something with the previous node we had one of those questions in in the lectures also for figuring it out like the figures I did in the lecture are probably a lot better than me trying to draw something right now so yeah and well more or less be around uh what we got we got a solid solid three days or aka the weekend for the exam so um yeah I'll try and answer questions on discord mostly sometimes I do piazza but I like piazza less than discord um so you can do that let's see if there's anything else so yeah most of the questions on piazza more or less got answered is there anything anyone here post on piazza they should definitely look at um quick sort yeah most of the questions I saw in piazza were like what sorting algorithms are on here so on so forth and yeah bubble sort selection sort things those are the only sorting algorithms no binary search do you bite we never covered binary search trees you don't know what they are so don't worry about them if you see them in past finals not included in the course this year but yeah any other questions and stuff and yeah for for the most part like I wouldn't even remember of like functions to remember let's see what what do I got off the top of my head so of functions to remember you should probably remember if you commit anything to memory rand I'll just return oops return random int on the exam you don't have to worry about setting the seed or the fact that you know if you don't set the seed it's the same thing every time you just assume it's always a random number and then what you always do with ran usually it's return some random number between zero and some giant number so to make it go to a range you always mod it so you mod it by some number and that makes the result in the inclusive range will be any number from zero all the way up to and including num minus one so that will give you a range of integers like that that many and then you know you might need to add something to that or multiply by it or divide it to get whatever the question's asking you if there's a random question so rand good function to know for the other math functions I probably wouldn't bother remembering them most of them take a single argument and you could probably make them up if you had to so like sine it's just sine square root most people like most programmers at least the standard library one has tried to make things as short as possible so like just think about writing as little as possible but it looks like something like square root so square root would be like sqrt but again wouldn't bother even remembering it um let's see other functions you should know off the top of your head while you should definitely know print f you should know scan f for any of the like even string input or anything like that just use scan f and just assume that it works fine you don't have to deal with nitty gritty memory issues with scan f that's what crashing your computer is for so know these two functions know the format specifiers so you know we got percent d for an integer percent s for a c string percent c for a char should probably have a space before it and that's pretty much all the ones you need to know aside from that so the other functions you should know from the strings good string functions to definitely know if you don't so the length of a string stir lang and again like there's stir n lang that like constrains it but for the exam you assume like your memory is fine so I wouldn't remember any of the string functions with an n in it so you should also know string copy so it's where you want to copy the string to as the first argument and what you want to copy from as the second argument and then there's like stir cat to concatenate strings so you got desk and string or source and I'll put it at the end and then the other one you should definitely know is stir cop so that will compare two strings together and then return an integer of that represents like the result of the comparison and if the strings are equal this returns zero if it returns greater than greater than zero it means s2 is greater than s1 if it returns less than zero it means s1 is less than or sorry sorry s1 is less than s2 and if it returns greater than zero it means s wow okay I'm having a brain fart because the dog it means s1 is greater than s2 sorry and then those are all the other ones like they're searching for a string but generally they'll probably just tell you to implement it so you can just try and remember search for a string is stir stir so it's searching for a string within a string so it searches that and then gives you back a pointer to wherever it's found there's also stir char which is the same thing it'll search a string for a character give you back a pointer to that character but generally you'll probably have to implement them so these I would probably not worry about but the other ones you should know so you should definitely know rand print af scan f hopefully you've used it enough times now that it's probably seared into your brain stir length stir copy for sure these two stir cat probably good stir cop definitely and then like if I had to put them in tears I would say let's say everything underlined with orange definitely remember everything underlined like stir cat probably good you could chance it and just completely forget about it and then stir stir and stir char are probably like iffy uh yeah question both a dog and cat which do you think is superior can't choose they all do different things so um yeah I have two dogs and you can probably hear the dog barking they currently have a feud so yeah um and then question can we use these string functions for any question the final so I mean I guess you could technically use them for any question but if you use them for a question that don't have strings in it probably going to be weird um you can use them unless the question specifically tells you you cannot use them then how many questions will be on the exam I have no idea somewhere in the ballpark of the same thing it kind of depends I think there might be more questions than this one but there's more shorter questions um yeah yeah we got cats dogs and there's also a nesting robin on our front porch so I guess we have birds too the dog dogs are fine I'm bigger than the dogs so yeah I'm bigger than both of the dogs put together barely so I'm I'm safe I'm bigger than the dogs but if the weight of the dogs is greater than your weight um you're probably you might be in trouble all right any other questions comments concerns so yeah you shouldn't have to memorize many functions oh sorry other functions you should know you should definitely you know malloc malloc that's a good one that's I put that under the orange tier and then you know you should probably know free too so free probably the question will probably tell you uh whether or not to use it but I believe those are all the functions you should definitely know any tips on not being super confused with pointers in linked lists um so hopefully that figures and everything in like lecture 30 and the lectures I have are helpful like we did a lot of exercises so I would just try practicing with them like hope you probably forgot them all now try and implement one of the functions we did without looking at it um go through it um I tried to make it as standalone as possible so you can you know even write it out and see I think we even wrote it out so write your linkless implementation c you can go ahead you can play with it while you still have a computer to help you out with it yeah all right because I'm having trouble with linkless head like why does head have next so how the linkless generally work is like the linkless structure itself just points to the first node in the link list and that's all the link list is and then after that every node points to what is after it so um like the list head doesn't have an like the list head would have a next but the list itself doesn't have a next if that makes sense so all the link list is it points to the first node and then every other node has a next so it's like I don't know like to keep track of everyone in line at something so if you just remember who is after you then that's all the information you need to know for no matter how many people if everyone just remembers who is after them and then you know who is at the beginning of the line then you know the complete order of the line right so if we got a b and c all standing in a line a knows b is after them b knows c is after them and I know what a is at the front of the list well I know what order they're in I can be like okay well so the first thing is a who's after you b oh who's after you c boom now I know the order of the list so that's kind of the idea of the link list because unlike an array the nodes could be anywhere in memory like an array you know they're all in order so you know you just go to next integer for link list nodes you have no idea where they are in memory yeah like the head is just a node it's just whatever is at the front so they have different names for stuff generally you know they say head is the like front of the lot if you want to think of it as a line that might be easier so head is just the front of the line tail is the back of the line and like if you want to think of it as person people nodes are people and all the people just remember who's after them and then to you as the person managing the line just have to know who's at the front may sometimes maybe you have to know who's at the back right but yeah but practice with the pointer stuff can you go over different ways we can access or edit a string not sure what you mean by that but the only thing you have to be careful about with the string is let's see where do we start writing let's just write so a string just it's just an array of characters that's it so it works exactly like if it was an integer array or anything like that modifying it is all the same the only exception is if you have something like this if I have char star s and I say it just equals to a string literal like directly if I do something like this well where this string is so where this array is in memory it's in a special place in memory so it would just look like an array of charts so it would be h e l l o and because it's a c string it has to end in an all byte so string literals they'll be stored somewhere in memory but the rule is if it's like a string literal I'm not allowed to modify any of these characters so this is like the only exception to like it's just an array of ints or anything so I'm not allowed to try and modify it if I have it pointing to a string literal because I don't know where in memory this is and a place in memory where I'm not allowed to change any values why well because for reasons so it might be clear if I tell you the reason so why it's like that is because I could have let's call this s1 and I could have char s2 maybe I make it also equal to hello so someone decided that okay well both of these like are supposed to represent the same c string so it's supposed to be hello so well I could have two hello somewhere in memory but that's wasteful why do I need two hellos I could just have them both share that one so what will actually happen is likely as long as you compile with like optimizations and things likely what will happen is the pointer to s1 and s2 will actually point to the same address so both point to the starting h so that's why you're not allowed to modify it because if I was allowed to modify it and say I did something like s1 string one at index one is equal to I don't know a well then it would change this from an e to an a and now wow now it looks like I modified string two when I didn't want to right I only modified string one I thought they were independent but because now that like c was trying to be smart and not store hello twice in memory they're both sharing memory and now if I modify it I'm not allowed to modify it because then I'm kind of like breaking the rules so now I'm modifying someone else's string it's not exclusively owned by me so I don't know what's a relay analogy you can think of it as like I don't know a library book or something I don't know or like some page someone's written like you can all share it you you can't like rip out a page and start scribbling your own stuff that's not fair to anyone else um but yeah that that's the this is the only exception for strings otherwise they're just like an array so if I just if I instead did char s3 and treated as an array and said it was hello then just like if I had int nums equal to one two three well c is going to size num so that it fits all three integers and they all exist on the stack I can modify them whatever however I want same rules with this so that string literal is just meant to initialize that array so c is going to go ahead and make sure that the length of this is going to be six so it would actually be six because it needs to hold h well basically needs to hold this which is six characters right h e l l o and then the null byte at the end so after that I mean it's just an array of characters just ends with a null byte all right any other oh we're slightly over all right any other last minute questions comments concerns knowing that yeah I don't remember what uh what we wrote on the exam but yeah on the long uh yeah I will will warn you slightly on the long side I think but the questions are easier so the idea is it evens out yeah again link list recursion focus on those ones and otherwise practice but yeah don't quote me because um I like I said haven't done an exam in this course yet I don't know how it goes but hopefully it will be all right um and yeah ask any other questions on discord I'll be more or less around on the weekend um oh another question would directly initializing string literal like that also include the null character yes it would so yeah like the string literal definitely has a null character in it so you just can't modify it yeah yeah using recursion program a beta version of chat bt chat gpt yeah I expected a lot a decent amount of recursion so yeah the other sections mostly focused on recursion and link lists so that's probably the majority of it um it's cumulative because it's c so it has to be um but definitely a focus on the second half of the course um yeah all right other people are typing yeah so I'll make sure everything's answered before I go for string literals why are you allowed to do string string plus one instead of string one so here let's go back real quick so I'm a lot so this here so here let's erase so let's assume this so this is a string literal like s2 is pointing to some you can think of it as it's pointing to an array that I'm not allowed to modify so and that's the only rule so I can't modify it so I can't do something like s whoops too bold stop it so I can't do something like s21 equals a this is a no go I cannot modify it but I can whoops I can do like char c equals s21 that's fine you're allowed to access the data and like get the values from the array no problem I just can't change the values of the array so this is a okay so I'm allowed to do that and in this case we know that um like c would be equal to a lowercase e and I'm also allowed to do s2 uh equals s2 plus one because that's just pointer arithmetic right so I'm just not allowed to change like the elements of the array but like the pointer it's just pointing to some address in memory I'm allowed to change what it points to so originally s2 it's basically you know let's draw out that array again so here's that array of chars in memory h e l l o and then the null byte so those are our six characters so initially s2 is pointing to the capital h if I do s2 plus one well just like as if it it's pointer arithmetic and just like if it was an array of ints it would make it would return a pointer that points to the next integer right so s2 plus one this is going to point to this so I'm allowed to do that I'm allowed to like breed whatever the characters are I just can't change any of these so the only thing I'm not allowed to do is I can't change this e to an a I'm not allowed to change this to j I'm not allowed to do anything I'm not allowed to modify the elements of the array but and that's it if it's a string literal yeah and towers of noi yeah so towers of noi good recursion yeah what the test should be is I should bring in the full tower of like eight pegs and uh actually no we should have a tower of 20 pegs and if you can finish it before two and a half hours you pass the course or you could just write the exam so I forget how many steps that was like several million so yeah just remember towers of noi we solved a good example of recursion probably the hardest recursion you'll see if you can understand that should be pretty good yeah that'd be an interesting interesting option you can either write the exam or try and do a 20 tower of noi in front of us wait so when you when you do s2 equals s2 plus one yeah then you're just pointing to the next element so if we do s2 equals s2 plus one so s2 plus one points to the e and then we update s2 to be equal to that so then s2 let's do blue so now s2 points to the e so now if you try and print s2 what if we just did like print f percent s and then gave it s2 well is this going to treat it as a c string so it's pointing at the first character it's pointing at is a lowercase e so it's just going to print off all the characters until it reaches the null byte and not print that so we would see ello hello so that's what we would see printed yeah if you do s2 equals s2 minus one plus one then you would get some random like if i did here let's do s2 equals s2 minus one so s2 started off pointing to here if we did minus one then we'd be pointing to whatever this element is which is undefined memory so we don't know what this is this is a big old mystery it could happen that we see some a weird character and then hello it also could happen that this random byte is just like the null byte in which case if we try and print it we don't see anything right it just prints absolutely nothing so or it could try and access this and we don't have permission to access this memory and then it seg faults so our three major options that we don't need to know for the exam either we'll see some random character that maybe can be printed and then hello maybe we see nothing or maybe we seg fault so that's the joy of programming isn't so we just minus one we're off by one and then who knows what our program does now it's only one little one little character what could one little character do huh all right all good all right should be again i will be around practice practice practice linkless recursion you'll be good so just remember pulling foyer we're all in this together and only one more weekend so good luck and i will see you hopefully on monday