 the APS 105 Programming Fundamentals. We're going to put the fun in the fundamentals. So everyone excited for this course or slightly terrified? Hopefully this course will be one of your favorites, at least my hope if you're into software at all. So I'm John Ailsen, your instructor. Do not feel bad if you cannot pronounce my last name. I could not pronounce it either until I was about 25 and then some nice Icelandic man told me how to pronounce it. So just call me John if that's easier or I will teach you a trick how to spell and pronounce my last name. So how it's spelled, kind of a mess, kind of how it's pronounced. I've heard it all sorts of different ways. Eiffel sin was probably my favorite, not even close. So here's the trick if you need to pronounce my last name. You know how to pronounce elf, right? It's just Christmas, so elves should be in your mind. So that's a perfect way to start pronouncing it. So let's say Eiffel sin, Eiffel sin, easy. And if you want to spell it, just shove the L and the F over and shove a yo in the empty space. And I can spell my last name too. In the unfortunate case, you have to spell it. I hope you don't have to spell it, but just in case there's a little hack, that's what I do. So now we get to talk about how this course is structured and all that boring admin stuff. So there are four entries in your course timetable. The first, which is in the top left there, are your lectures. That's where you are right now. That's where we learn all the concepts and we can ask questions to the instructors and all that fun stuff. What's directly related to the lectures are the tutorials. So the TAs will be at the tutorials. They will go through practice problems with you that are really close to exam problems. So I encourage you to go there. They'll start easy. They'll get progressively harder. So hopefully you can follow along or at least gives you a good idea of what to expect for the exams. And then the other half of this course are writing software and the labs. So the boxes in the green have to relate to the lab. So there's the plan area lectures. So those are additional practice for labs that run more like a lecture, but the TAs run them and they're directly related to the lab material. And then there are the lab sessions themselves. And that is where you get in-person help from the TAs. So again, I encourage you to go to that. So word of advice, since this is what? Your second semester here at U of T and I will give you my wisdom from starting off university as well and screwing it up. So don't do what I did. Start your labs early. So this is the cadence of the labs. So two weeks before they are due, there will be a handout posted. So I encourage you to go look at the handout as soon it is posted and then within a week from the handout being posted, the material will actually be covered in the lectures. So a good thing to do is look at the handouts, see what things the lab is asking about. And that is the stuff you should definitely pay attention to when it comes up in lectures or if you're trying to relate it back to the lab and you are wondering about how it actually relates to the lab or how to solve something, good time to ask the question in the lectures. So within a week of the lab being due, all the material you will need will be covered and then you should start working on the lab right away. And then of course, there is the deadline if you do not heed my advice, which is Saturday at midnight, which means if you leave it to last minute, I guess you have ruined your weekend. So that is your prerogative, I guess. You're all adults, I can't tell you what to do. I can just give you suggestions and you're free to ignore them. So I encourage you to have a weekend and have a life outside of this course. So start early. So next I have to talk about academic integrity, which is a serious topic that is no fun for anyone. So the rules are you may discuss course content, but do not copy it from each other. So you're primarily harming your own learning. The best way to learn this course is through practice. So if you are robbing yourself of practice, you're just going to fall behind. Next time you take a software course, you're already be behind and you'll get further and further behind and it will just be a mess. So most of this course is just practicing. So make sure and the labs are intended to help you practice and encourage you to practice by assigning marks since, well, that's the thing to do. So another word of advice. So you may or may not, I guess I probably shouldn't judge if you copied English assignments or something like that. Well, if you copy computer code, it's a lot easier to detect it. We might go into why that is, but trust me, you are not more clever than people who have been writing software for 30 years. So some people think they are, I think last year, so this is the first year I have taught this course, but last year I think in one of the first labs, about 20 people got caught immediately and that is no fun to everyone because then you have to meet with the course coordinator and then you have to meet with the department and then you have to meet with the thing and they tell you that it's just not good for anyone. Waste your time, waste our time, waste your learning, no good, don't do it, not fun for anyone. So now we can talk about the grade waiting. So the majority of your grade is exams, which isn't that fun, but it is what it is. So your exams are worth 70% of your total grade, midterm 30%, the final exam 40%. The other 30% is labs, which are divided into nine labs. So how that 30% works is they're progressively weighted more as you learn more and the labs get more complicated. So there is a lab zero, that's a setup lab, it is not worth anything, so its weight is zero, and then the first actual work is labs one to three, so they'll be worth 2% each for a total of six. Then labs four to six are worth 3% each instead of two, total of nine, and then the final three labs are worth 5% each, which is a total of 15, and that is how the 30% of your labs is going to work. So the official resources for this course are Corkis and then the online textbook. So the direct Corkis link is there, so how my slides work is while they're PDFs, so anything is clickable, so you can just click the link if you want to, and in addition to Corkis, which is kind of the hub for this course, there's an online textbook, learningc.org, no other materials required, don't have to go to the library, don't have to buy a book, nothing like that, and the nice thing about this book is it was written by the course coordinator and it has exercises along with the chapters and it actually follows the course quite closely. The last one is the Piazza discussion form, which is on there, so the TAs, some of their job is to monitor that, help you if you have any questions that may arise outside of the lab sessions, but the lab sessions is probably a faster way to get help from the TAs. So some additional resources for this section. I do, I livestream the lectures just in case you are sick or anything like that, or you want to stay home or you're cozy or something like that, and I have the recordings all on YouTube so you don't have to go through Corkis, you don't have to log into anything, it's nice and easy. The lecture slides are just on my website, so again, you don't have to log into anything and everything's linked on there, which is all fun, and then there is an unofficial Discord community. So if you find that link, it will join you to the Discord server and one thing I like to do, so I'll take questions just from you guys and I will also try to get to know your names. I'm bad about pronouncing names and using names and everything like that, but I will try. Again, I pronounced my own name wrong for 25 years, so don't be offended. So if you don't want to ask questions like to be directly in class, I also kind of monitor the Discord chat while I am lecturing. So if you're more comfortable with asking questions in the Discord chat, I have channel section three on my phone here and I can see that while I am lecturing and I will try my best to answer the questions if that is an easier way for you to ask questions during the lecture, and that also has the benefit of if you have to stay at home, you can also still ask questions because well, lectures are live streamed and I look at the chat. So any questions with any of the admin stuff so far? Or are we all good? Yep. Okay, yep, and name, sorry? Morris. Morris, okay, yep. The final exam, I have to double check with the course coordinator because I know she wants to do it in computer lab. But I think it's still TBD on that. Well, I'll get back to you. All right, any other questions about the course at all before we kind of dive head first into it? So we'll all cover this again. So I'll point out anything we actually need to know. So people probably have a range of experience from absolutely nothing to you don't need to be here. So this course has to be set to people that may not have any programming experience whatsoever. But again, if you have some programming experience, please still show up to lectures and everyone please still show up to lectures if you are able because I get lonely and it's no fun lecturing to an empty room. So please still show up. So how all my lectures are going to work is if you talk to someone who has been programming for a long time, it seems like they're speaking a completely different language because they use words, they use words weirdly and everything like that. So what I'm going to do is any new term that comes up will be highlighted here in blue and I will explain it on that slide. And if you do not know what that term still means, please ask me. So the first thing is, well, what the hell is a program? So a program is something that a computer can execute which is just another word for run and the art of writing a program is called programming. And it is kind of an art, it is not an exact science but some people consider it an art but it's actually kind of more like engineering which is good because hey, we're in ECE. So why are we doing C programming in this course specifically? So humans write programs in something called a programming language. C is an example of a programming language and it first appeared in the 70s. So programming language is just a set of rules that allow you to write some texts that we will see how that gets transformed into something a computer will run in a few slides. So you may have heard the term programming language before and may know some different programming languages. So specifically why we chose C for this course is it is a small language with not that many features and it's closer to how a computer works. So it is a lot more difficult to do some tasks in C but what if you have to learn eventually how a computer works which you will do over the course of your degree then C is good because basically it doesn't hide anything from you. It is really close to how a computer actually works so it immediately springboards you into the hardware courses and everything like that and again hardware is a word we may not have seen before. So we're programming a computer we have to answer what is a computer. So a computer has two parts. The first part of a computer is called hardware and that is the physical components of the computer that you can actually touch. So what that would look like so here is what is sitting at my desk at home and this is what part of a computer actually looks like. So there are three main components to a computer and the first is under that fan in the middle so that is called the CPU or central processing unit so some might consider that the brain of the computer that's where all the calculations actually happen and that is what actually does the work. The rest of your components are just storing information. So the next most important part of your computer is the RAM or random access memory and that's kind of like short-term memory. So any information that is stored in RAM the CPU can directly access and that is how that is where all your programs will primarily execute and the CPU interfaces directly with RAM and will read values from the RAM. The next piece of storage likely is probably an SSD or a solid state drive and that's long-term memory so that's where all your files are stored all of your assignments, that's where Windows is or macOS depending on what type of computer you have and that is also called long-term memory. So we model a computer as a black box we model most things in engineering as a black box so the black box just takes some input something happens, we don't really know the details of it we don't actually need to know the details of it and then we get some result out of it or in other words some output. So for a computer your input is provided by like a keyboard yeah typing letters on a keyboard moving a mouse clicking a mouse maybe swiping your fingers on a trackpad or a touchpad trackpad whatever you want to call it or a touchscreen and then that input gets fed into the computer which has again three major components so compute which is the CPU memory which is synonymous with RAM even though you might consider it short-term and long-term memory but whenever we say memory in reference to computer we're talking about RAM and then there is storage so that's SSDs or HDDs if you have a very old machine and then you have some output that will go to a monitor or screen or something that you the human can actually see so what's the other major part of a computer the other major part is well software and software is just the information that your computer needs to run so the most important information are instructions that the CPU can actually run and the instructions tell the CPU what to do so as you go on computers seem really really complicated and really really smart but in actuality they are very very stupid so CPUs aren't really that much complicated than doing addition if you can do addition of two numbers well you're pretty much as good as a CPU but the only difference between you and a CPU is really really smart people made that go really really really fast so you can do a few additions maybe a second, ten seconds something like that CPUs can do billions and trillions so that's the main reason why we use CPUs so the next question is well if software is just information how do they store that information so computers just store it using a series of numbers at the end of the day everything that goes into a computer turns into a number at some point and then we as humans assign meaning to them that is the kind of dirty secret about all computers that we just assign random meanings to numbers and then if you play with the numbers in the right way you get some result that surprises you but at the most basic level all computers can store is a one or a zero or a one that's it and since we're electrical people well what does that look like well it's kind of like a light bulb so a zero you can think of it as off that means the light is off and a one well means I turned on the light so a one is the same as on that means that means it's on that's a one so that's as complicated as computer get course done alright it gets a little more complicated than that because we need more than just a single zero or one to do something so usually as humans we represent numbers in decimal so if we really really break it down and be really really precise about how we represent numbers well we represent numbers as sequences of digits so all the digits are in an order and depending on what order they're in they have some special meaning computers aren't that much different so a digit is just a number between zero and nine so there are ten options because we are humans we like tens because we have ten fingers and ten toes at least most of us do I still do despite some close calls so computers same idea except they represent numbers as sequence of bits and what a bit is is a bit is just short for a binary digit and that means it's a digit that's either zero or one so there's only two options in this case zero one lights on lights off that's it so we can actually write numbers using just bits this is called binary so binary is just a numeral system that only uses bits so if we go back to elementary school hopefully they probably made you do this at one point so here's a decimal 176 and then they had you write out like the hundreds column the tens column the ones column and then you put a digit in each one of them everyone's done that before alright so everyone's done that maybe in grade three something like that so then as soon as they taught you what exponents were you realize that oh the columns are actually like that so the first one is ten to the power of zero second one is ten to the power of one the third one is ten to the power of two and if we had more digits it would keep going up by another exponent so if we represent 176 it's just one seven six hundreds column ten column ones column or using exponents so if we were to write a binary number the only difference is that base so instead of ten the base ten is because we're using digits so if we're using bits instead of digits well then our base becomes two so we would write out the columns you know to the power of zero to the power of one to the power of two and since we only have two options for each bit well we probably need a lot more of them to express the number 176 so in this case we actually need nine or eight of them so if we take the exponents and just make it easier on ourselves and just convert them just to an actual number they'll look like this so we'll get one two four eight sixteen thirty two sixty four one twenty eight and that those numbers probably look familiar if you have had to buy a laptop because the size of the memory is probably one of these options and why it's one of these options is because well computers inherently are binary they like powers of two so if we were to represent the number 176 in binary we could do that by just and this is how we would do it it would be one zero one one zero zero zero zero we don't really knowing how to do this is not important to this course it might be in a second year course but for now we just need to know how a computer would actually represent a number in binary so to verify this well we can do what we did before when they were all you know powers of ten being powers of two doesn't actually change anything so if we want to write the number back out you know it would be like one times a hundred plus seven times ten plus six well in this case it's the same thing but it's either zero or one so to get this number back to a decimal that we humans can understand it would be one times one times one twenty eight plus one times thirty two plus one times sixteen and we wouldn't have to add any of the other ones because anything times zero is zero so we can verify that this is the correct representation because one twenty eight plus thirty two plus sixteen equals a hundred and seventy six so they actually mean the same thing so computers they can only store information in finite amounts so a byte that's our next term so that is the only actual important thing from that long spiel know what a bit is and then know what a byte is so a byte is a binary number that is up to eight bits long or exactly eight bits long and then there'd be leading zeros so because there are eight bits and each bit can either be a zero or a one well we took some statistics in high school too so if we have two choices for all eight of our bits we have two of the eight different options so we can actually represent two hundred and fifty six different things using a single byte and why is it called a byte? it's because we do not have a sense of humor because well a bit sounds like you know you're taking a small bit of some food and then they're like well what would be bigger than a bit and then someone said ha ha a byte because that's bigger so if you had to go bigger than that maybe that's like a gulp or a chomp or something like that but I guess we'll get to that when we need to but for computers right now pretty much a byte is like the smallest useful unit of information that a computer actually uses so a few other notation things so in math you know if you were being very explicit about what base a number was in you would write it as like a subscript so this would be like five in base ten so like a normal human number and that is the exact same as one zero one in base two which would be binary so in this case in most programming languages what the equivalent would be is five so whenever you write a number in programming languages it assumes you are a human and you are writing it in decimal but if you want to write a number in a different base in a programming language instead of having a subscript or anything like that what they do is just prefix it with zero B to indicate that it is a binary number and then there would just be ones and zeroes that follow after that and that would represent your binary number so this would be zero B one zero one again in this course you won't actually have to write binary numbers but it's good to know that at some point they will get converted to binary because they will have to run on your computer so another note that trips up students even in third years for some reason whenever we just talk about binary they think all the rules just fly out the window so if I wrote the number you know 176 again but I had to use four digits for whatever reason well implicitly there's leading zeros there I can write as many zeros and from that number as I want and it won't change the meaning of it same is true for binary numbers so if I write zero B zero zero zero zero zero zero one zero one that's the same as writing just one zero one there's implicitly leading zeros so you don't need to write them if you want alright so that is how you represent numbers on a computer so we can actually represent letters and you know other numbers in binary so the way computers do this is they don't need a full byte so basically this is how it represents things you type on your keyboard as long as you have a standard keyboard so it doesn't need all 256 options your keyboard doesn't have that many keys on it if it did well that would be quite the keyboard so for keyboards you only need seven bits so there's some text on the slide that says the most significant bit is always zero just ignore that for now so for representing any character on your keyboard you only need seven bits so that would be two of the seven or two hundred or sorry one hundred and twenty eight different possibilities and what you can type on your keyboard is called a character so a character is how a computer represents English and the parts of English are like letters, digits, punctuation and spaces and some other things that we'll see in the next lecture so for instance if you type an A on your keyboard how it's likely going to be represented is it's going to be represented like this in binary or if we don't know how to read binary yet it's just the decimal sixty five so if I type an uppercase A it means sixty five and that's the magic so we'll get into more of this as we go throughout the course but just remember at the end of the day computers are not some magical thing it's just a bunch of numbers floating around and we just assign meaning to them so if you ever think they're magical just remember they don't even know what an A is you have to translate an A as sixty five and if you went up to one of your parents and say hey I got a sixty five in the course they'd probably think oh no that's not good but then you're like no it's actually an A I was speaking computer and then I don't know horrible joke don't mind me we're weird people so get used to it so your programs or another word for program is application or app or whatever you want to call them they all mean the same thing they run in some type of operating system that we have all used at this point so the operating system and from this point on I will just short-handed as OS is just software that interacts directly with the hardware and you'll learn much more about this in ECE 344 which I happen to also teach so hopefully I will see you there so other terms so code code is just text written in a programming language so just a bunch of characters written in a programming language that have to conform to some rules which we'll see as we go through the course and another term you might hear is source code so source code is just code for particular software it's just kind of a short-handed way of saying it so instead of saying something long-winded and saying you want you know I want the code that represents Firefox or something like that you just say where's Firefox's source code so then they would give you some code that's written in a programming language that if you create a program out of it it's Firefox another term that we will encounter in this course is called a library and that is just code written by someone else that you can use and C provides a library they have a very very creative imagination and their imaginative name for this library is the standard C library I could not think of a more boring name myself if I tried so you have to give them some credit for that at least so the weird thing about programming is we need to use a program in order to create programs so a compiler is the program that transforms your source code into a program that your operating system can actually run so what it looks like is it takes your source code and then that you can represent a compiler as a black box again if you want to learn more about a compiler that's a third or fourth year course here which is also a lot of fun but for now we'll treat the compiler like we're treating the computer and we'll treat it as a black box so the input of the compiler is your source code and the compiler does some magic and then at the end of the day it outputs machine code and machine code is the actual binary representation of the instructions that the CPU knows how to run so again the CPU just understands numbers there are certain numbers assigned to tell the CPU to do certain things like there is a certain number that if the CPU sees that means it will add the next two numbers for example and all the CPU will do is just run those over and over again and once you in one of the second year courses you will actually design a CPU that does this but for now we can just treat it as a black box so we often use the term compile as a verb like saying let's compile our program that just means let's take our source code and then turn it into machine code so I can actually run it on my computer and see what it actually does alright again no questions so far cool alright oops alright so we need another program to handle input and output for us so you're not going to have to write anything that like directly interacts with the keyboard or a monitor or anything like that so we're going to use primarily something called a terminal and that uses keyboard for input so it will translate the key presses for you and then it will show the result on your monitor or your screen for the output so it is a text interface it just uses characters you see on your keyboard nothing else special so if you want to see more graphical things like windows come up, fun spinning 3D windows all that fun stuff that will be a later course this is the introductory course so we are just going to use the text interface so every program we create in this course our input is going to be text and our output is going to be text just to keep things simple and have you focus on actual actually using programming to problem solve so how you present your results doesn't end up really mattering that much it just might be a bit easier to use but we're going to be programming in this course so here is an example of your first program so don't worry if this does not make any sense yet this is just to introduce you to what it looks like and I will break it down line by line but I know each line may not be comprehensible yet so I'll give a brief idea of what each line does and hopefully that will set us up for questions and we can actually see how to run something like this so this is your typical first program your first programming in any programming language is going to be to get the output hello world to see that you can actually show something that you have actually written in text so the first line here this includes STDIO.H it does not have a medical problem STD is short for standard not something else get your head out of the gutter so that include at the beginning that means just tell us a compiler to read the contents of another file in this case there is a file called STDIO.H somewhere else on your computer and it will read the contents of that file and that is short for STD short for standard and IO is short for input output so that means standard input output how to actually get input in and show output there is a function in here we don't know what function means yet but it contains a declaration of something called printf and that basically is how we can show some output so and how the compiler and you typically read a program is from top to bottom but when it actually runs the program it does not go top to bottom but first we'll see what it does so this int main void has a bunch of gibberish in it right now that we don't really know what it means what this does is define something called a function called main that outputs a number so this basically describes that black box I was talking about again so it's something the computer understands that takes some input and then produces some output in this case int is short for integer so that means the result of this function kind of like a mathematical function is a number so the rule is whenever you write a C program if you try and run this program it will start running it at main it will not start running it at the top so you can read it from the top down but when you start running it it starts running at main so if the first thing this will do if we try to run it is it will run this printf function which we can see kind of take something that looks a bit like hello world in quotes with like a weird backslash and an N we'll see what that does in the next lecture but this at the end of the day basically just calls a function called printf and that function handles output for us so it outputs a string to the terminal that we can actually see and a string is just a sequence of characters so like H, E, L, L, O so H is a character E is a character it'll be assigned some number but we don't have to worry about that because C will translate it for us but it just takes a bunch of characters and then everything looks okay hello world that's all things we can type our keyboard and then there's this backslash N which looks kind of weird we sent an email to someone with the backslash N so that actually does something else that's special that we will get into and we won't actually see it when we run this program then the final line means stop executing this program so this is how you stop executing main and then output the value zero so that is the result of running this function and the operating system takes that value and does something with it if it reads a zero that means no errors have happened everything is hunky-dory the printer didn't catch on fire or anything like that we are all good so let's see what that looks like so if you have started lab zero that is already posted the result of your lab zero you'll set up something called VS Code and VS Code is where you'll do all of your programming in the IDE, short for integrated development environment and that is where you write your code you can compile your code and run your code and all of that fun stuff so I have exactly what was on the slide here I created a file called helloworld.c again if this doesn't look familiar to you it will after you set up lab zero so also during the lectures I will have a bunch of code examples so please feel free to tell me we don't know anything yet because the course hasn't started but in general after the course starts part of the lecture is dedicated to just seeing what happens telling me what would happen if I did this we can do live coding examples we can see the result and the best thing about me being here is I can explain what is actually going on because some of the stuff will be mystifying to you and some of the stuff if you ask me the right thing to do might be even mystifying to me because well I have been programming for a long time so I am used to seeing the same stuff so if you just tell me to do something random I might not even know what it can do so here is our program as you go through lab zero you will figure out how to actually invoke or run your compiler so you actually get the machine code for something to run I believe the default in lab zero is to use a compiler something called GCC which is short for the GNU C compiler that is just the name of the people that wrote the software and then they did not have a creative name for it so CC is short for C compiler so if we use GCC we can save the name of the file to actually compile and then the resulting name of the the resulting name of the executable that we can actually run so now after this point I have executed a program or I have created a program called hello-world and if I type dot slash and then hello world as a name and when I press enter my computer will start executing that program that I have written so if I hit enter you can see the output so the output here is hello world which is what I have written and that dash n does not show up so that dash n doesn't show up because well it is why it's something called a new line that we'll get into in the next lecture and just typically you end each line with a new line whenever you're done a line of text so if I wanted to change it to output something different I would go ahead and change what comes after hello so I could just say hello class and then the part of this course is I have to after I make a change and save it you should be sure to save it in order to see your change when you actually run the program you have to compile it again because that hello world file that's still the result of when I compiled it the first time if I run it again it will still say hello world so if I run the compiler again so I'm just telling it hey compiler again compile the C file and create this program for me again and then it goes okay sure I've done that now if I execute it again it will say hello class so that's our first lesson so any questions of anything we have done so far sorry? yeah so anything for for tests or anything like that don't worry about it right now we'll get to it later we'll have problems that come up and we'll answer that don't worry about tests for now this is the first lecture this is stress free for now alright any other questions anything else fun you want me to do so typically I will built in some extra minutes for the lecture just in case there are questions I like taking questions and in this case I built in that time and there are no questions so we get to go early so just remember I'm pulling for you we're all in this together