Summary

This document is a study guide for a C++ course. It covers the basics of computer logic, memory representation, and binary numbers.

Full Transcript

**eLecture 1:** Welcome everyone to our first day of class. Let\'s go ahead and dive in right into our introduction into C++. So let\'s start off here with the logic view of a computer. All right. So with the logic view of a computer, let\'s go ahead and draw it out and see the multiple parts that...

**eLecture 1:** Welcome everyone to our first day of class. Let\'s go ahead and dive in right into our introduction into C++. So let\'s start off here with the logic view of a computer. All right. So with the logic view of a computer, let\'s go ahead and draw it out and see the multiple parts that it has. So suppose that this is the entire machine here that we\'re working with, and let\'s suppose that we are going to have two parts here. Here\'s for the CPU, and then we\'ll have one for the main memory. So main memory, it\'s essentially the RAM of your computer. So it\'s very much like your brain, where you have your short-term and long-term memory. So main memory is the RAM, which is equivalent to short-term memory. CPU, we\'ll discuss this a little bit later. But, the CPU is essentially where the brain of the entire machine is. And so, this is what handles everything in terms of the hardware, the software. So let\'s go ahead and look at the type of inputs that we can have. So the first input is- and again, this is talking about hardware- we\'ll have the keyboard, a mouse, and even a microphone, just like I\'m using here to record this particular video. We can also have some output. And of course, this is leaving the machine. So some examples of that is going to be a monitor, printer, speakers, or headphones. And such cases that you may have. We can also have some secondary storage. Some examples of this is going to be, for example, your flash drive. So we can again, have that flash drive or even a hard drive. That\'s, of course, external. And that\'s the idea here of what we\'re working with in terms of our memory. So let\'s now discuss precisely some cases now. The definition of what is a memory. And essentially, it is a huge collection of circuits with electricity flowing through them or not. We don\'t necessarily have to have electricity flowing through them. That\'s usually when the machine is off or no information is passing. So that\'s essentially what memory is. So let\'s go ahead and draw this idea, this connection, between one point to another point. Suppose that we have here our A location and we\'re going towards our B location. And we have some dots here that we can connect. Now, suppose that we have no connection between these two particular points. So we\'ll draw another one up there. And these two will be connected together. This is what we interpret as an open circuit. This is signified as zero because it is in the off position. And another form, suppose this is our same connection, from A to B. But this time, they\'re all connected here together. This is what we consider to be a close circuit. And this is symbolized by the value of one, which means that it\'s on. Together, these are what we call binary digits. And for short, we know them as bits. Let\'s go ahead and look now at the idea here of each particular bit. So what we know is that, eight bits is equivalent to one bytes. And so, if we draw out the memory. And again, we know that eight bits are associated to one byte. So let\'s go ahead and draw here seven lines. Just like that. We start counting from zero all the way to the total value of seven. In terms of computer science and computers in general, everything starts counting out the value of zero and progresses upwards from there. So it doesn\'t start counting at one, but we start counting at zero. So suppose here we have again eight bits and again this equivalence here, we can write it out. This is again to one byte, where all of this here is a total of eight bits. So suppose that we are going to have some of these on and off features, just like we were discussing here beforehand. So suppose we have this pattern of 0, 0, 0, 0, 1, 0, 1, 1. We want to know what this represents, what value is given from here? A way to know that is that we know that a bit could be either on or off. So that means we have two different choices, and so each one of these is raised, two to the value that we\'re on. So in other words, it\'s two to the zero power. This next one here will be two to the first, two to the second, two to the third, two to the fourth,two to the fifth, two to the sixth, and then two to the seventh. So let\'s use this idea here and figure out what that value represents internally. So we\'re going to take this value zero and multiply that to two to the seventh. So this is going to equal zero times two to the seventh. Then let\'s look at the next one here. So it\'s plus zero times two to the six. So again, zero times two to the six, plus zero times two to the fifth, again, this zero times this, and so forth. So zero times two to the fourth plus now, here\'s a different one. This is now a one. So it\'s going to be one times two to the third plus, and then the rest here, two squared plus one times two to the first, plus one times two to the zero. Mathematically, we know that anything times zero is going to go away. So this is just the value of zero, this all goes away and this one here. So we\'re just going to be left here with three terms. Those are one times two to the third, plus one times two to the first, plus one times two to the zero. This is saying two to the third is just going to be two times two times two. So that\'s the value of eight plus two to the first is just two plus two to the zero power. So anything raised to the 0 power has the value of one. Now we\'re going to add all these values together, and that\'s going to give us here the value of 11. So the value of 11 here is what represents not necessarily the number 11, but it\'s a representation in terms of an ascii value of what\'s going to be stored internally. So 11 represents some particular character on your keyboard. So 11 could be the letter \"L\" or could be the character, period or a question mark. Now, all of this are associated by a standard, and each of these values are going to be associated to one particular character. We\'ll later reference what that is but if you want to go ahead and check it out, you can quickly Google the ASCII table, it\'s also in the back of your book, and you can see again what each value represents in terms of a character. Now, some things in terms of understanding about bits and what they can represent, as we said, is that bit patterns can represent anything in terms of colors, objects, characters, numbers, or anything like that. Let\'s go ahead and write that down. So bit patterns can represent anything as colors, characters, objects (we will see later what this is), numbers and more. So let\'s go ahead and try here an example. See if you guys can figure it out. If we\'re given in bits, how many unique patterns are there? You more than welcome to pause the screen at this moment and see if you can figure this out. Let\'s go ahead and see some examples of this and see if we can try to figure out the different cases that we can work with. So as an example, say we only have one bit, and let this little box here represent it. We know that anything in there can only be either on or off, in other words, zero or one. So we have two choices. So that\'s how many unique forms we have. Let\'s go ahead and look at two bits. If we have two bits, let\'s draw out these two boxes here together. Let\'s make it a little bit more pretty here. What are the possible choices that we can have here? We can have when both of them are off, we can have when the first one off, but the second one is on. We can have the first one on and the second one off, or we can have both of them on. So as you can see here, we have a total of four different cases. Let\'s look at three bits. With three bits we can do the same format where we can say all of them are off, the last one here is on, or the last two here, or we can say all three of them are on, or the first tier are on and then the last one is not. The very first one here is on, the first, middle and last tier is on, or the middle one here is on, and so if we count these and we have here one, two, three, four, five, six, seven, eight, we have here a total of eight rows. In other words, total of eight different combinations that we\'re working with. So what does this mean? What is the general pattern here that we\'re seeing? Well, the answer here is that the number of patterns is given by two to the n. So if you notice when we have only one bit, two to the first is just two, when we had two bits, two to the second is four when we have three of them, it\'s two to the third, which is a total of eight. So we can figure this out based on how many bits we\'re given. We can plug it into this particular formula here, and we can figure out from there, the total amount of bits that we\'ll have in terms of combinations. Now, another thing here that we can address is, how is memory associated when we\'re working with all of these bits and bytes. So what we know is that every byte has a unique physical address. So suppose that in this location here we have the memory address. If I scoot a little bit over here, we just want to know that this here is considered to be base 16. What is considered to be base 16? Well, if we\'re looking at it in terms of numbers, if we look at the counts between zero, one, two, three, four, five, six, seven, eight, and nine, these are the total number different unique symbols, if you will, that we have for these values. So we count here a total of them, we have here a total of ten. So this here is considered for us to be base ten. Now, when we look at it in terms of base 16, so let me write this down that this here is considered to be base ten. So base 16 is the same format, so it\'s 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. But at this point, if we write the value of 10, notice that this is a combination between the first two, the 0 and the 1. So we don\'t have a weight really to establish what is the tenth value here. So instead is we\'re going out to the alphabet. So this is going to be now the letter A, B, C, D, E, F, until we have here a total of sixteen. So if you don\'t remember them, you can stop here at this particular letter and counts, you have to have your total of sixteen values. So here\'s, we know from 9 is 10, so here\'s 11, 12, 13, 14, 15 and 16. So this here\'s what we consider now to have a base 16. So the address is here for this, again, our memory are a combination of these values. So very much how we can make, as an example, we can make the value of, let\'s say, 1,215. If you notice, this is just a combination here of these particular values that we have. But in terms of base 16, We can do the same thing and represent the values here accordingly. So we can say something like, A, 1, F, as an example. So this will symbolize here a different value that we\'re working with, but all of this is just in terms of base 16. Now you can convert base 10 to base 16 and vice versa. But that\'s in different course where we\'ll work with it in terms of a discrete mathematics course or as you progress with your computer science courses as well. So let\'s go ahead and understand this in terms of a visual concept. So if we have over here the memory address, and again, we note that to be as base 16, we can draw that out as such. So we can say that the first address here that we have could be address 21. And we can simplify it as this particular block here. So again, we\'re talking about in terms of definition here is that every byte has a unique physical address. And so this here is considered to be only one bite, and it\'s located at address 21. We can have a consecutive address here, so 22, and that\'s located here. We can have another address, say this is address 23, and that\'s located here, and so forth. Even at one point, if we go down the listing, we can say this one down here is that address, let\'s say A to F if we would like. And this is again all in base 16, but of course, we can have it in terms of 2 and 1 because that exist here in our base 16 as we\'ve discussed already. Now, when we look at our projects, at our homework, at examples, we\'re going to associate some address of where everything is stored in memory. And we\'re going to keep it simple with just 21,22, we\'re not going to go into true base 16 of A to F or something of that format. We just want to keep it simple here as an introduction course. Now, if we were to display the address in memory from our computer, we will see there that this particular address does exist. We\'ll see it very lengthy. But again, just for the scope of our learning concept, we\'re just going to keep it as small values here. Now let\'s go ahead and continue with the next definition here of the CPU. The central processing unit, which we know as the CPU, this is what we consider as the brain of the computer. It coordinates system resources at the hardware level, and it also performs basic math and logic operations. So what\'s considered as basic math? Just addition, subtraction, division. We also have one special one called modulus. You may have learned that in discrete mathematics, where it\'s essentially just a remainder that it returns. Logic would be more like logical AND, logical OR, you\'ll learn those in discrete mathematics. But of course we\'ll go over all of this together. Alright, next one is the software. The software is essentially a compiled set of instructions for the CPU. So it\'s just simply going to give instructions to the brain of the computer to know what to execute at what time as you\'re running a particular software, much like how I\'m writing right now, one note, it\'s telling it again, we\'re typing this, it\'s displaying this on the screen. So it\'s giving all these lists of the commands that we want our computer to do. The most important software that we have is the operating system. And we\'ll know that as an OS. What this does is, this manages system resources at the software level. So you have the CPU that manages the hardware, and the software here in terms of operating system, manage the software level, all those resources that we have. Okay, so let\'s go ahead and look now at the evolution of programming languages. We\'ll start off here with our first one, which we denote as a low-level. The low-level is essentially machine language or machine languages, and this is the only language a processor understands. So as an example, this is simply just a bunch of 1s and 0s. So for example, here 011011. And let\'s make sure here we have a total, again of 8 bits that we\'re working with so that way we have 1 bite. So here\'s 1, 2, 3, 4, 5, 6, 7, 8. Oops. Put some spaces here for the next round here. So let\'s say,1, 2, 3, 4, 5, 6, 7, 8, something like that. And we\'ll do one more here, so 1, 2, 3. So here again, we have just some example here that we have again 8 bits, which makes 1 byte here. So here we just have a total of 4 bytes. So hopefully I counted correctly where we have here 8 bits in each one, numbers and are really important here. But that\'s just an example of, again, what a machine language is. Essentially, it\'s just a bunch of 1s and 0s here that we\'re working with. Alright, the next part here is in intermediate level, and we can say this is your assembly language. We do have a course here at Fullerton that teaches assembly language. I highly encouraged to all of you to take it. And what this is here is where rewrite mnemonic code in a text file which is translated into machine. Sorry, just translate it into machine code by an assembler. So let\'s go ahead and look at an example of this. I\'ll make a spacing there. So an example here is we can have move at this particular address five. So what this is saying is that you\'re going to store the value of five at this particular address location. And then from there we\'re also going to store the value of two at this particular address; and what we\'re going to do at the end is we\'re going to look at what is stored at these two memory locations and we\'re going to add the corresponding value. So just like that. So again, this is store the value of five to this address, store the value of two to this address add what\'s located in those two addresses together. So let\'s list here the pros and cons of assembly language. So pros here is that it\'s very fast and a con that we have with this is that it\'s very abstract. It\'s difficult to work with and it\'s not portable. What does this mean? Very abstract. Well, just look at it. mov add. We have to know what the addresses are as were working with it. Very abstract in that aspect, and it\'s difficult to work with because we have to keep in mind all the address locations that we\'re working with. It\'s not something that we can create an alias for to remember that this is located at such position. An easy analogy that we can use from this is; I have a name you have a name and imagine that we call each other by our DNA sequence because that\'s truly what we are. As opposed to whatever name our parent\'s gave us. And so trying to remember that\'s going to be very abstract. So the same way here it\'s trying to remember the address of where everything is located makes us again very abstract. Another thing is that it\'s not portable. Meaning if we\'re working in one machine, for example, we\'re working in a Linux environment, or working with a Windows, or a Mac. Then we\'re working between a 32-bit, and then 64-bit processor. So from there, it\'s not really portable because memory has different, again, sizes that we\'re working with. So this makes it very difficult to work with because each assembly language has to be pertained to the machine. In other words, the operating system that it\'s running behind as well as the size for. In other words, if we\'re working with a 32-bit or 64-bit processor. The last part here that we have is now the high-level. From this one, what\'s considered to be high level is language C, C++, which we are learning. Pascal, Java, Python, etcetera, many other ones that exist here. So let\'s look at an example of a high-level language and how it is that it can be easier; or potentially harder depending on what you\'re trying to do. So we can create here a variable. It\'s an integer that we\'re calling it here as sum. And so what this is doing internally, it\'s finding an address that can fit an integer. So an integer contains four bytes. So it\'s going to find the location of four bytes at address 22 as an example. And we\'re going to coin this as an alias as sum so that we don\'t have to remember that particular address. That address I just gave you. I already forgot what that value was. But it\'s very nice to work because we know we gave the word sum. It\'s very meaningful. This particular variable name that we gave it. In other words, this identifier which is the name sum. So what we can do now internally is we can say that sum, wherever that\'s located in memory is going to equal five plus two. And this is the syntax that we\'re going to look at for C++ as we again progress our course. So what are these pros and cons for this particular example and this high-level language that we are working with. So the pros is that it\'s easier to use and it\'s portable. So it\'s easier to use. Like I said, we don\'t have to remember the true address location; and it\'s portable because it\'s going to find a location of four bytes for us. It doesn\'t matter if we\'re working on a 64-bit, 32-bit processor. If we\'re working with Windows or Linux, you name it. It\'s going to find four bytes of what we\'d need exactly the size here for an integer. What are some cons here? Cons is that we have less low-level control such as memory. So we won\'t be able to necessarily access a particular location by knowing the actual location from it. So we\'re going to lose a little bit of that, but we\'re going to gain quite a bit more structure within an ease of usage. So depending on what you want to do with your programming language, or what software it is that you want to build; that\'s going to determine whether if you want to work in a high-level, or something that\'s in the middle level per se, or assembly language. But don\'t think of it as one or the other. You can also mesh these two languages together meaning you can work with something with C++ and then have, or in other words, built a program where that\'s going to call another function in assembly language to execute that particular thing. Whatever it is that you want to do at a low level. So don\'t think of it as just one or the other. You can definitely mesh that together. And of course this is going to progress your work.\ **\ Lecture 2:** Welcome everyone for the second lecture here. Let\'s go ahead and continue with the next definition here with the compiler. The compiler, it is a program that translates a high level language program, such as our C++ program into a machine language program that the computer can directly understand and execute. An example of what we mean by this. You can think of it very much as a translation. My family, they only speak Spanish. They always have me, especially when I was a little kid, is they have me to always translate anything that was done in English. It could be a bank transaction, something at a store. I would be their compiler. In other words, I would translate the language of English, of what was being said to them into Spanish.Then, they told me in Spanish and I would translate it back into English. You can think of this in terms of the compiler doing where you write the entire code in C++. As an example, it runs through the compiler and that\'s going to translate that particular language into machine language. We can write that down here as an example. You translate English to your parents native language. Let\'s talk about the next part which is now linking. With linking, it\'s the object code for your C++ program that must be combined with the objects for routines of our input and output. Sometimes you will see input and output written as IO for short. I\'ll just put it here in square brackets here for you to remember that. What is object code? The compiler, when you translate it from C++ into machine language. That new file, if you will, that got translated. That new machine language, that\'s already considered here to be object code. The linking takes after the compiler is done translating everything in terms of whatever language you\'re working on, in our case, C++ into machine language. So that\'s object code, and what this is, what the linker does is that it\'s going to combine with other objects for routines. What does that mean? Well, you\'re not going to necessarily write the input stream. In other words, you\'re not going to write how you going to read the characters that are typed into keyboard to your program. You\'re not going to write a program that whatever it is, you\'re going to output to the monitor, you\'re not going to write such a thing. Someone else wrote it. All you have to do is incorporate in your code. If you write one program, then you\'re going to link that with another program. In other words, an object code that someone else wrote already translated for you in machine language. It\'s going to combine those two codes for you, those two machine language codes. It\'s going to create one executable program. That\'s what this is here. The object code for your C++ must be combined with the objects for routines of input and output that your program uses. This process of combining object code is called linking. It\'s done by a program called a linker. Let\'s go ahead and see what is design here of how your program is going to look like. Everything begins here with C++ program. From here, the next step is that it goes to the compiler. This is again our translation that\'s done. Then from here, we obtain already the translated version. Remember this is our C++ program. The compiler then, translates our C++ code into machine language so that\'s what we get here after. Again, this is the object code for our C++ program. Then, from here this goes to the linker. The linker is going to obtain some extra code that you didn\'t write, but that you included, which is your input and output routines. After this step here, we\'re going to have a complete machine code ready to run, or in other words, execute. These are essentially the steps that we\'re going to look at. Let me go ahead and just box these, make it a little bit more obvious what each step we are performing. Again, the first part here is our C++ program, and that leads to the next one here, which is the compiler, which leads to our object program, and that leads to our linker. Lastly, the linker also includes that attaches there is also object code, for other routines. In fact, we have two things going into the linker, this object code of C++ that we created those in there, and then, everything else that we\'re including as part of our program that we didn\'t write necessarily, but we\'re including it so that goes into the linker. Then, this outputs from here a complete machine language code that it\'s ready to execute. One more time, if we designed this entire build process. Let\'s look here as an example of this build process. We\'ll start off here with one file. Let\'s say that our file here, it\'s called foo.cpp. As we noted beforehand, it\'s any file that it\'s going to be C++, is going to have the file extension of dot cpp. In here, we can write some simple code here to work with. I\'ll write this over by hand here. This program can simply be something like outputting a hello. Will have here a little Cout hello, followed by a new line. That\'s going to be our program roughly to illustrate what it is that we\'re trying to do. Let\'s box out there. This is going to lead to our C++ compiler. Then from here, it\'s going to translate this very same code that we just wrote here into machine language, in other words, what we call object code. This is just going to be filled in with a bunch of ones and zeros. Just writing here randomly some ones and zeros across. We can name this object here, this object code with the same name. This is still going to be called foo. But the extension is going to change because no longer going to be a.cpp file. This is going to be now a.o. Again, to signify here that we\'re working with an object code. From here, this goes now to the linker. It takes our original program of foo.o and any existing code that were incorporated into our program. That\'s what we\'re going to write it up here. It\'s existing codes and we can say these are from our standard libraries. That\'s going to go also into our linker. At the end, this is going to give us now a new one and zeros. In other words, a new machine language here. This is essentially going to be now our executable code, so box that. It should be very much different than our foo.o. By default, when we\'re working with our Linux environment, this particular file here is going to be called a.out. It will always be called that by default. If you\'re working on a Windows machine, that\'s going to be a.exe. Again, we\'re working on our server. If you\'re going to work with your own machine, if you\'re a Mac user, you\'ll have a.out, but if you\'re working in a Linux environment, but on a Windows machine, it will be a.exe. I\'ll just go ahead and erase that so it doesn\'t confuse anyone from that aspect but essentially that\'s the trail here of the steps that\'s going on at each particular case. We can rename this to be a different name than a dot out and we\'ll see how we can do that with our commands of how we can compile it. To be honest, for our beginning class here, we\'re just going to leave it as a dot out. There\'s really no need to change the naming. We just need to remember that, that\'s going to be the name of the executable file once we are done working with that. Let\'s go ahead and see now a sample program of how everything is going to look like and what is it that we need to execute the entirety of this particular program here. Let\'s start off here with our int main. This is going to have a void argument. This is where the entire program is going to begin. Main is the name of the function where our C++ will always begin. Think of it as we\'re starting in chapter one of a book, or at the very beginning of something. C++ is always going to start at the very beginning of this function called main. What does it take as an argument? It takes void. Void means nothing. It\'s going to return an integer, meaning it\'s going to return to the operating system some particular value to signify whether the program executed correctly or not. You can think of it in a way as where I may ask you, can you please go and do X and Y and Z for me? And then you return back to me to let me know whether you were able to do that or not. That\'s what this is as an int. It\'s basically telling the operating system, was a program successful, if so, great, if not, how can we take care of it if it wasn\'t successful? That\'s basically what that means. The next part here of our program is a Cout. This is the most famous program that release the setup that we can work with, which is, the hello world. Meaning it\'s going to display on the screen for us, hello world. We\'ll have here our int, main, void. Then we can say hello world, followed by a new line. What this does, it\'s telling it, I want to print this out to Cout. This is the name that we\'re using to say that this is the monitor that we want to display it to. Then here we have our insertion operator, meaning it\'s directing this string here that we have as hello world to be displayed to Cout again, that\'s our monitor. Again, we have another insertion operator, again going towards Cout, and it\'s going to display here a new line for us. At the end we\'ll have our return zero, and zero here means it\'s what we\'re returning because we wanted to an integer. Again, remember mathematics, what an integer means, it\'s any whole number or natural number, and we also include the negatives in there. Negative three, negative two, negative 10123, etc. Zero here is reserved to say that everything went smoothly, there\'s absolutely no problems. Let\'s go ahead and terminate this particular curly brace. One extra thing that we need at the very top here is because we didn\'t write this Cout, meaning we didn\'t write this particular connection to be displayed to the screen to connect that for us. That\'s going to be what we call here standard libraries or existing code that we are including it. What is that we need to include up at top? We need our pound, include, iostream. Again, this is saying we want to include our input and output stream, and then we\'re going to say using namespace, std. Here, we want to include everything that\'s in our standard libraries, so that\'s going to be our Cout and this ENDL, which is our new line. Let me go ahead and copy this particular code for us. Let\'s just make it a little bit pretty by having some syntax highlights for it. Give me one moment here, I\'ll set this up here. Looks something like this much prettier to look at. At least it highlights it for us here what the string is, our return, our integer values that we are working with. Again, let\'s go ahead and annotate this for you so you can remember what each part is. Again, integer here, this is our return type. This is our output. We have here our main, which is the name of the function. This void here is our input and again, void means nothing. Then we have our string, which is in these double quotes. These two less than symbols is what we call an insertion operator and this ENDL here at the end, this is what we call as a new line. So we\'ll see more of those type of programs and will again show you how to compile all of it and we\'ll see some demonstrations as well through our terminal as well, how this program works out. Some key things that we want to denote from here, is, if there is no error, then we\'re going to return zero. So again, zero here means everything went smoothly. Recall that int signifies that we\'re working with an integer. Cout means that it\'s an object. Remember how we talked about in the last video or in other words, the last class session about C++ being object-oriented and working with objects. So Cout is just a very fancy datatype and we\'ll discuss again more about this, but this is again a very fancy variable that we call it an object, and this is an object that represents the screen or the monitor. Now, we didn\'t see it here in this program that we made, but we can also say, well, what\'s going to represent the keyboard? If Cout represents the monitor, then Cin is going to represent the keyboard. So this here is an object that represents the keyboard. The next part here is the, again our extraction operator, that\'s our two greater than symbols here. That\'s our extraction operator. If you noticed, when we have it as less than, we note that as our insertion operator. Extraction is what goes with Cin where we can say we\'re extracting out that information that we are reading from the keyboard. So Cout goes hand in hand with the insertion operator, and Cin goes hand in hand with the extraction operator. You don\'t want to mix those as those will give you some errors when you\'re compiling it. The compiler does also two things; its translates it from C++ to your machine language code, but it also checks for the syntax, making sure everything is correctly formatted. Much like an essay, where you\'re writing your essay and you\'re capitalizing the first letter of every sentence, you\'re putting commas, periods where they are appropriate. One last bullet point here is our, two forward slashes. These are essentially your comments. So what this does here is that these cancel or cancels up to the end of the line. In other words, whenever we write these, again forward slashes, these are essentially comments that we aren\'t working with in our particular program that we don\'t want the compiler to see. We want those to be hidden from them, and that way it helps us to make our code much, much more readable since we\'re putting in little notations. I mean, it\'s not like we\'re going to write what I wrote over here in our black font to signify what each part of our code is, but essentially, the idea behind this is just to make the code much more readable for us. All right, let\'s go ahead and see an example with everything that we talked about here in these particular bullet points. In this program, let\'s ask the user now to enter a particular value and we\'re going to display it. We\'re going to need our include iostream, again for input and output. Meaning the keyboard and the monitor itself will be using namespace, std, and then our main function. Notice with this main function, we\'re not putting void as we did up here. In this reasoning here is that the compiler is going to know, it\'s going to be obvious for it that there\'s no arguments that we are supplying, so there\'s no need. If you log onto Stack Overflow or any other websites forums in other words, you\'ll see that many programmers are omitting when it\'s a void argument. All right, now let\'s go into the body here of the main function and let\'s create now a variable of where the user is going to again enter this particular value. I\'m going to put it as a comment, so create a variable, and again, the compiler is not going to see this at all. As soon as it starts here with these two forward slashes, it\'s going to know that this is just a comment. It\'s something for us, humans to make our code readable and so from here, let\'s create it. So int, let\'s call it myint. Now, let\'s prompt the user so I\'ll put here a little comment. Prompt the user for an int, and then the last thing I\'ll do is display what the user entered. Then last comment here is return. This just gives me an idea of the code that I want to do, and again, it\'s a structure, think of it as outlined as you\'d write in an essay, you would outline what it is that you want to talk some key points. The same way here, we can use comments to output an algorithm of what it is, the steps that you\'re going to take. We\'re going to prompt here for the user to enter a value. We\'ll do here a little Cout, enter a value. Notice again how I leave the space here, so it\'s a little bit obvious and easy for the user to, again, associate themselves with a program, readability and whatnot. Let\'s go ahead and read that in using our extraction operator and we\'ll have here myint, that\'s what everything is going to be stored. Now let\'s display what the value is that the user entered. Again, we\'ll say you entered colon a little space, and then myint, that\'s got updated with the value that the user enter so that\'s going to be in there now, followed by a new line so it starts at the bottom and then let\'s return. Everything went smoothly. So we\'re going to say here, return zero, and we\'re going to close. Let\'s go ahead and make the text look a little bit prettier using some syntax highlighting. Now there\'s nothing wrong with how this looks like. Many old-school programmers are used to seeing it this way, but not all want to see it in terms of a colored fashion. So either or, it doesn\'t really matter which way you want to look at it, but I just like to have it highlighted. It makes it a little bit easier for a beginner to look at it. All right, here we are. As you can see, we have here again everything highlighted, our comments, notice they are in green color. Not all of them are going to be in the same coloring format, that is just the program that I\'m using to translate this code for us. But if you go into Nano, Emacs, any of these other programs, some of them will display it in color, others will not until you mess around a little bit there with the settings and you can get it setup for this. But as you can see, this simply does what we intended where we created a variable here which is called myint. We asked the user to enter that particular value for us, and then we displayed it after the user entered it here, with using the Cin and then we extracted it out and now it\'s stored into myint. Then we just display a new line at the end. We like to end everything with a new line, that way it starts at the next step there in our terminal window. At the end we just return zero, meaning everything went smoothly. **Lecture 3:** Welcome everyone to our lecture three. This time we\'re going to talk about algorithms. So you can find all of that information in your book on page 12 for tenth edition. So what\'s an algorithm or what are algorithms in general? These are just essentially steps that lead to a particular solution. And so if you\'re trying to solve, let\'s say, a particular math problem, then you\'re going to do the opposite of PEMDAS in order to solve this particular solution. So these are the steps that you\'re going to take to come up with that answer. This algorithm could be anything that you want. Of course, we\'re in computer science. And so you can program something to take a certain amount of steps and come up with that solution. The assumption here is that computers are not well-grounded. And so you have to let them know, each and every single step that you\'re going to do in order to solve that particular solution. So for example, if you want to exit a particular room, you have to tell it to get up from the chair or wherever maybe, take X amount of steps, to reach the particular door, reach over, open the door, and then tell it to keep walking and exit out of it. So this algorithm, as you saw, it\'s very detailed oriented in terms of the steps that you want to do, just to simply exit out of the room. So what I explained is something more in terms of robotics. It can be engineering here. But in the same way is, a computer doesn\'t understand everything and you have to let it know in detail what it is that you want to do. And that\'s essentially what an algorithm is all about. It\'s describing the steps that you wanted to take to come up with a solution. So let\'s go ahead and define this as saying it\'s a sequence, steps that lead to a solution. All right, let\'s go ahead and do an example here where we\'re going to have a listing of variables, in other words, characters. And all of them are going to be in random order. And what we want to do is figure out if there\'s any repeated letters within our listing. So let\'s go ahead and write an algorithm for all of this. And let\'s test it out, how it works, if really are finding what is supposed to do. And so we\'re going to list all these particular steps and it\'s a very good way that you check it. Make sure it is doing what you want it to do. So these are the steps here. So our first step, it\'s we\'re going to set a current position to let us know where we are in the listing, as well as where the beginning of the list is. So we\'re going to set a current position and list top to zero. From here, we\'re also going to set up something else. We\'re going to set our variable that we call found to false, meaning we haven\'t found any repeated letters so far. So this is what our variables are and what we\'re going to initialize here to get us going in terms of our algorithm. Think of these as counter variables. All right. Step two is we\'re going to read the target letter from the position of list top. And we\'re going to check, if the target letter is empty, we\'re going to quit our program, meaning there really isn\'t anything in our listing. So we can just exit the program and say we found nothing. Our third step is we\'re going to increment our current position, and we\'re going to read what the current letter is there. Step four is we are going to check if the current letter is empty, we\'re going to increment list top. We\'re going to set the current position equal to list top. And then we\'re going to go to step number two. And what this means here is that if our current letter has reached an empty location, meaning we have nowhere else to compare, we\'re going to reset it to the second letter, in other words, second variable. And we\'re going to check that from every other that comes after that to make sure that within them there are or are not any repeated letters. Step five, is if the current letter is equal to the target letter, then we\'re going to set found to true. And we\'re going to quit the program. So at this point we\'re going to check that the current letter equals a target letter of what we\'re looking for. And we\'re going to set our variable found to true, that we found it. We don\'t need to check anything else because we found one letter already that repeats. So automatically we exit the program and we return as true. Step six, is that we went towards the end. And so we want to reset everything and we want to go to step number three. All right. Let\'s go ahead and test this algorithm out with a sample of lettering. So suppose that we are working with these particular characters. So let\'s say our first letter here is a, our next letter is b, followed by c, d, b, c. And then here is our empty location. So this is what we signify here, just temporarily visually that we\'ve run out of characters. This is our empty space, if you will. One thing to remember, when we look at this is in computer science, the very first element is considered to be zero. The next one is one, two, three, four, five and then followed here by the value of six. So let\'s follow this particular algorithm and see how we can work with this array of characters. So we\'re going to do the first step where it\'s set the current position and list top to zero and we\'re going to set found to false. So let\'s go ahead and create some variables of that here, so we will have here our current position. That the value of 0. We have listTop and that\'s currently also position zero, and we have our variable that we call found. We\'re going to set this one to false. We\'re done with our first step. The second step is we\'re going to read the target letter from listTop. We\'re going to create a target letter and we\'re going to read this from listTop. ListTop is in position number zero. We go to position zero here, and we see that we have here the letter a. That\'s what goes here, letter a. The next part here is checking, is a target letter empty? Empty here signifies for us this asterisk. We have the letter a here, so that doesn\'t mean that it\'s empty. Then we proceed to the next step, step three. We\'re going to increment our current position and we\'re going to read the current letter. That\'s what we\'ll do here. Increment the current position, current position is zero. Now it\'s going to be the value of 1. When we say increment, we always mean add one to it. We go to the next value, which is the value of 0 plus 1, which is 1, and we\'re going to read the current letter. At position number one, we have the letter b, and so that\'s what\'s going to be written here at our current letters. We\'ll go ahead and write that. Current letter and we have that as letter b. Now we go to the fourth step. If the current letter is empty, which it\'s not. Because it\'s not empty, in other words, we don\'t have the asterisk character, we don\'t need to do any of this other stuff that goes onto. Let\'s go now to step five. If the current letter is equal to the target letter. In other words, is a equal to b? It\'s not. So we don\'t have to do any of this extra steps here of set found to true, anything like that. It\'s only if this statement is true, do we do these next steps. Then we go to step six, which tells us to go back to step number three, at step three it tells us to increment the current position and read the current letter. We\'re going to increment the current position from one to the value of two, and we\'re going to read the current letters, so that\'s the letter c. We update our current letter to the value of c and we repeat the same steps of four and five. Is the current letter empty? No, it\'s not. We have a letter c. Is the current letter equal to the target letter? In other words, is a equal to c? It\'s not. We go to step six, which tells us to go again to step number three. We\'re going to increment again our current position. Incrementing that\'s going to be the value of three. Read the current letter. Current letter is the value here of character d. That gets updated and we do step four again, is the current letter empty? No, we have the letter d is the current letter equal to the target letter? Nope, a and d are not the same, go to step three. Step three again, is increment our current position, which is four, read our current letter at that point is the letter b. We do the same thing, current letter empty? No, we have a b. Is the current letter equal to the target letter? No, a and b are not the same, go to step three. We go to step three. Again, increments are position, which is number five, read the current letter, and that\'s the letter c. Then again, go to step four. Is current letter empty? No, it\'s not empty. Is a current letter equal to the target letter? A is equal to c? No, so step three, which tells us again to go to step three, increment the current position. Now we\'re at number six. Read the current letter, which is now an asterisk. Now we go to the next step. Step four. Is the current letter empty? Yes, this time it is empty. Now we\'re going to execute these next steps. We\'re going to increment our listTop. ListTop is currently at 0, so that\'s going to be now the value of 1. We\'re going to set the current position equal to listTops. So this is no longer at six, it\'s the same value as listTop. The last thing is we\'re going to go to step number two. Right above it. From here, we\'re going to read the target letter from listTop. The target letter from listTop, again, listTop is the value of 1. That\'s the letter b. That\'s going to be our target letter. It gets updated to the letter b. Then we check, is a target letter empty? No. It\'s the latter b so we don\'t need to quit. We keep moving forward. We were at step two. Now we\'re at step three. We\'re going to increment our current position. No longer at number one, but now at number two, and we\'re going to read the current letter. What\'s the current letter at position number two? That\'s the letter c. Crossover the asterisk and then put the next value there, the C, and then we check, is a current letter empty? No, it\'s the letter c, so we don\'t need to do any of those steps. Is the current letter equal to the target letter? Is b equal to c? No, it\'s not so we go to step number six here, which tells us to go to step number three. Going to step number three, again, increment our current position. That\'s the value of three. Read the current letter. That\'s a d, and then again check step four, if the current letter is empty? It\'s not. We have the letter d. Step five. If the current letter is equal to target letter? B is equal to d? Nope. Then we go to step six, which tells us to go to step number three. We\'re back there, increment our current position. Now we\'re at number four. Read the current target letter. I\'m sorry. Not the target letter but the current letter and that\'s going to be the letter b now. We\'re going to check step four. If current letter is empty? It\'s not. So we don\'t need to do any of that. The next step is, if current letter is equal to the target letter? It\'s target letter equal to the current letter? That\'s true here, so then we\'re going to set found to equal true. This is no longer false. This is now a true statement and we\'re going to quit the program and that\'s it. That\'s the algorithm that we need to do to figure out, within this particular listing, are any of the characters repeated? This is one way of doing it. There\'s different ways that you can approach this particular problem. It just depends on how you think about it. You\'re going to learn a little bit more about this in the next course, the Data Structures class, where one algorithm may be better than the other. We\'re going to define what is considered to be better. For our introduction course, is as long as you find some algorithm, whether you think about it, you find it online to help you, you can use it to solve a particular problem that you want to again solve for. The next steps after you found a particular algorithm, you want to execute those in your program. This could be any language that you want. Of course, our classes about C++, but an algorithm should theoretically work for any language that you\'re working with. Whether it\'s C++, C\#, R, MATLAB, Java, Python, it should work the same way because an algorithm is a generic of the steps that you need to take. You just need to focus those on those particular algorithm. Think of it as communication. If you want someone to do a favor for you, they can speak English, they can speak Spanish, Arabic, you name it, as long as you talk to them in their native tongue, if you will, then they will be able to do a favor for you or whatever it is that you would like them to do. In the same way here, this is going to lead us to what we call the implementation phase. This is when we\'re producing the final program from the algorithm that you design. Each algorithm that you design, in other words, each program that you make in C++ or in general, it\'s always going to be custom-made to whatever it is that you want to do. Most often than not, you\'re not really going to reuse code. You\'re just going to reuse simple functions, simple things that you\'ve probably done before, and always aggregate them together to get this new design that you like. It\'s always custom made of what it is you\'re designing. That\'s the implementation phase where we\'re produces final program from this algorithm that you designed after you tested it out to making sure that it is working properly. This leads us now to what is the stages here of a software. What is the lifecycle? So again, this is the software lifecycle. There\'s different steps for this here. This is one of the steps that is designed here in your book. There\'s different versions of them, but they\'re all more or less very similar. The first step is the analysis and specification of the task.In other words, this is your problem definition. What this means is this is where you\'re going to analyze what it is that your employer wants you to do or if you\'re making a program for yourself. It\'s analyzing and understanding what specific things this particular program needs to do. You want to make sure you understand all of those in grand detail. This is where you become very proactive and asking again your employer or whoever it is that you\'re designing this particular program for, making sure you ask all these questions and follow up with anything else, any new ideas that you may have to improve it right from the beginning, making sure you have a complete understanding of the particular task that you want. Second step is the design of the software. This is where we understand again, the objective for it and the algorithm design. In this particular cases, you want to make sure that you understand. Now that you understand the problem, it\'s how are you going to design it? What is it that we\'re going to use? Are we going to use a particular data structure? Again, we\'re going to learn that in another class. What are we going to use in terms of simplicity of variables? How\'s it all going to come together with what it is that we\'re working with our current programming language? Now, once we design this particular steps, again, this design that we wrote up before, and usually this is all done again by hand in grand detail. Once we\'re done with all of that, we\'re going to go to our step three, which is the implementation. In other words, this is the coding of the program. This is now again typing away in terms of C++ or whatever it may be and moving forward from there. A lot of times as a beginner, you are going to skip step two and combine it with step three. Now the issue with that is that you\'re going to be typing away C++ code, designing the algorithm at the same time as you\'re writing it all out and it\'s going to be very troublesome because you\'re going to have a lot of errors going on, syntax errors, understanding of the language before you go to actually understanding the program and designing it, either by paper or on a white board, you name it and making sure that it actually works. With step two it\'s quite important that you design it separately from actually coding it. In other words, it\'s sometimes you might hear it as pseudo code where you simply writing out each step that you\'re going to do. I\'m going to do this, I\'m going to do that. Think of it as a mental task of what it is that you want to do for the day. Again it\'s like today I\'m going to go to the grocery store because I\'m going to get some milk, some cereal, some hot Cheetos, something like that. Taki\'s, right? You can think of this as that particular design. Then later you\'re actually going to put it in practice, what it is that you\'re trying to design up here. Make sure you keep these two separate. It\'s going to save you a lot of headache in the long run saying, well, \'\'it should work my code, but it\'s not doing what I want it to do\'\' and that\'s because you never really tested your algorithm, making sure that you actually did design it appropriately, as opposed to just jumping in right away in terms of the implementation. Now that we implemented the code and we have it running, we want to test it. We want to make sure besides what we already tested in our design of the software, we want to make sure that it\'s also been tested thoroughly once it\'s completed, once it\'s compiled and ready to execute, making sure that when we throw everything at it in terms of little things that the user can do, making sure that it doesn\'t crash to program. It still executes as it\'s supposed to do. For example, if you\'re going to ask the user to divide two numbers, you want to make sure that the user is not going to divide by the value of zero in there because we can\'t. Those are some things here that you want to test out to making sure you cover your bases for those particular circumstances that may come up. Step five is the maintenance and evolution of the system. What does this mean? It means that sometimes we\'re going to be updating our program, whether it\'s an operating system update, if you\'re working with Windows or Mac or Linux. With Windows, for example, we started off with Windows seven, then we moved on to the next version and so forth. That\'s going to present some problems as we are working with our current software that depends on an older state system. We want to make sure that we maintain that in terms of the evolution of the operating system. Another type of maintenance that may come up it\'s, suppose that we\'re making a cash register as an example. The user only wants to have cash only. Sometimes the next step here, in other words, the next update of this particular program is, well now we\'re going to take some credit cards. We want to make sure that we maintained this particular software and keep up with the latest tasks, the latest trends, whatever it maybe and again, make sure that our software is still working with our current environment. Our step six here is when our program becomes obsolete. At this point, it\'s too much of a burden to actually try to fix the particular software that we made so far. It\'s just best to just throw it away and start from scratch. It starts with a brand new design. There might be a latest new version of C++ that may be out there that you want to use and take advantage of or not. I mean, just depends on what it is that you want to do, but sometimes it\'s just more trouble to fix it than it is to just start from scratch. Think of it more of an example like a car. If you\'re driving an 1980 car, it\'s going to cost you more money to just fix it as opposed to just buying a brand new car in the future at that point. You can think of it in that particular aspect. Let\'s go ahead and look at errors. Errors start after we compile. As a reminder, when we compile something is G++ dash wall. Again, this just means invoke here all warnings or display all warnings. Dash G is for the debugger. Then foo dot cpp. Foo is just a random name that we use to name a particular file. It can be anything that you want in terms of naming convention. Of course, keep that in mind that this is just a sample that we\'re working with, that exists somewhere here, randomly that we came up with. When it comes to your homework, we\'re going to let you know what particular filename we want to have, as that\'s what we\'re going to use to collect it based on this particular name. When you compile this, this may give you a bunch of warnings and errors that may be given. We want to understand what these particular errors are and again, how is it that we can fix them? So let\'s go ahead and discuss the different types of errors. The first one that we have is syntax error. This is the case when your compiler will detect a syntax error from your programming language, such as omitting a semicolon. Syntax error is you\'re forgetting to write something syntactically speaking on your program. Think of this as when you\'re typing an essay, you may forget to put a comma where you need to, you may have a misspelling somewhere in terms of words, you may forget a period, a question mark, an accent if you\'re talking about in language other than English. That\'s what a syntax error is here. Forgetting something that you need in order to make it in terms of true C Plus Plus. Now, when we get a syntax error, this leads to an error message. An error message is when the compiler will give you an error message when there is a direct violation of the syntax rules. Again, this is what will be displayed for you in the terminal because it\'s caused by this particular syntax error that you broke the rules on. The compiler, think of it as a spell check in terms of C Plus Plus. This is not the only case where we\'ll have a compiler. There\'s other languages that also have a compiler. But for now, since we\'re again learning about C Plus Plus, these again are the errors that you will get. Now, sometimes you will also get a warning message. A warning message simply means that the compiler will give you a warning message when there is something technically wrong with your syntax, but not enough to cause an error message. What is it that we mean by this? Well, very simply is we can design a quick little program here to give you this idea is suppose that we make a program here, so here\'s our int main and let\'s allocate space for a particular variable. Let\'s call it \"myint\". This is an integer type. What we\'ll do here is we\'ll just display hello followed by a new line. Then we\'ll just have our return zero and we\'ll end our main function there. The warning that we are going to get from this particular case is the fact that we never used this particular variable in our program. The compiler\'s going to see this as wasting memory for this integer that we allocate space for, and we never really ended up using it. That\'s the warning message that we get from this particular case and example. Another error that we can get is a run-time error. These are errors that the compiler, or should I say the computer now because this is no longer through the compiler, this is now working through the computer system. It\'s going to detect something as it\'s running as incorrect. Let\'s complete here the definition and we\'ll explain it. These are errors that the computer system can detect only when a program is running. An example of what I mean by this is the compiler is not going to catch any error here because everything worked fine. This is the point when you\'re actually running, or in other words, executing the program. In other words the./a.out, and an error of this can occur when, let\'s say you\'re dividing by zero. Everything is running smoothly until it comes to a point where you have a division by zero and the entire program crashes here. We can say here as an example, it\'s running until you have, say something or some number here divided by zero, and then it crashes. That\'s when we have a run-time error. Another one is a logic error. These are mistakes in the underlying algorithm, or in translating the algorithm into C Plus Plus language, or any other language. What this means here is that you may have some kind of logic error of how you were thinking about this. For example, is in this top program of saying dividing by zero, maybe you meant for it to multiply by zero and because of that, it gave you a logic error that the compiler is not going to catch. It\'s something that you have to figure out based on the results that you\'re getting saying. \"That doesn\'t really make sense, I shouldn\'t really be getting this answer or I shouldn\'t be really getting these particular results.\" More often than not, that\'s a logical error in terms of your thinking, logical thinking. You\'re going to have these kind of errors. These are not what comes up from the compiler, but more based on the results that you are obtaining. Of course this translating the algorithm into C Plus Plus. Well, sometimes you can translate something and have the general idea, but you\'re going to miss some steps there because not all languages are the same. You can think of this with a simple analogy where you can have a very nice joke in English but when you translate it to your native language, mine is Spanish, it\'s not really going to ring the same way. I\'ve done it many times with my family. I tell them a joke and they\'re not really laughing and I\'m cracking up and then it\'s just an awkward silence. In this case it\'s not one to one aspect of translating a particular algorithm. The last one here that we have is the human error. So what the human error is that everything runs, and it works out, but you\'re not getting the right answer. This is the case where you essentially accidentally do a typo. You have everything correct, the algorithm is designed, there\'s no really logic error because you intended it to do this particular case. It\'s just as you\'re typing away, I know we\'re typing like 200 words per minute as we\'re compiling all of this. That\'s where we\'re going to have some human error, just accidents, it\'s inevitable that we\'re going to work with, but things can happen. Again, as an example of this is you accidentally put the minus symbol, meaning subtraction instead of, let\'s say the multiplication symbol. That\'s the idea here. These are all the different types of errors. With experience, you\'re going to have understanding what each error is as you\'re seeing it through the compiler, as well as Googling it because there\'s some cases where I really don\'t understand what it\'s trying to tell me. It\'s very bizarre of how the compiler\'s communicating to me, what it\'s trying to tell me that it\'s incorrect. Sometimes you\'ll even find myself that I copy, paste the error that\'s being given by the compiler, put it on Google and see what other people have to say about it. Whether it\'s in Stack Overflow or any other type of form that you\'re working with. Don\'t be taken back saying, \"As a beginner, I have to do this a lot.\" Even at my level, is we still go back and discuss with other people that we don\'t know around the world and see what kind of errors that we\'re getting that we don\'t understand and how we can improve our program so we can actually compile it, or how can we actually make it work if it\'s some kind of run-time error or logic error that we are trying to design here. **Lecture 4:** >> Welcome everyone to our next lecture. Let\'s go ahead and talk about data types. We have different data types that we can work with. The first one is an int, which we already know that the stance for an integer and we know that an integer, just like in mathematics, it\'s simply includes the positive values, includes the negative values as well as the value of zero. Now when I say these negative and positive values, these are whole numbers that we are talking about. The next data type that we have is a float, also known as a double. So we can combine these two. These are exactly like integers but they can now include decimal values. So float and double we\'ll later discuss what the differences are. But just to give it a simple idea here is just simply a difference and memory of how much decimal values that they can store. The next case is a bool. This is for boolean. So this stores either true or false, in other words, zero or one. Then we have char, where this is a character and this just stores any character that we can have. So you can look at your keyboard and we have all of these characters that we can relate to in our program. The next topic that I want to discuss here is variables and you can find all of this information in your book on page 40. So a variable is a named location in memory. For example, let\'s look at this particular code. Suppose that we\'re in the function main and we allocate here a space for variable called myint. Again, this is known as the identifier, myint is the naming that we give it and we\'re storing there an integer. So what this does here, just in this small piece of code here, is that at runtime, your program is going to go and find space for an integer wherever it can find a particular space in memory. Think of this as you arriving to Fullerton College and you want to find a parking next to your class. So you\'re going to find a parking spot that can fit whatever vehicle you are driving and in the same way, the program is going to go find a space where we can store myint. An integer takes a certain amount of space. It takes four bytes so we\'re going to find exactly that particular size in our memory. So if we were to draw this here, let\'s say this is our memory and here\'s this block where we are going to store the identifier of what we call myint at some particular address. Wherever we found space, just like a parking spot, wherever we find a particular space location. Suppose that this is at address 32 and so if we continue with the rest of our program here, we allocated the space. Now one thing important to know is what is stored inside here. It looks like it\'s blank, but it\'s not. In fact, there\'s garbage inside, meaning there\'s still some leftover bits that are turned on and that has some value to the old program that was using this particular memory. But in our program, it\'s useless, it\'s junk in other words, we have no use for it, so we just call it garbage. I can give you an analogy of this. Say for example, you\'re at a parking lot at McDonald\'s and someone else was there parked before you and just left their trash of whatever they ate at McDonald\'s and you park there. Well, in your spot, you see that trash that they left behind where it was once useful for them, but for you is garbage. So in the same way, there\'s really no way to essentially clean up this particular space. We just know that whenever we allocate space for it, a garbage value remains inside. There are some languages other than C++ that whenever you allocate space like we did here, it\'s going to provide a zero value to it, but C++ is not like that. Unless the program before us that used this particular space was nice enough to set the value of zero before releasing the spot and so when we use it, it\'s initialized to zero. But we cannot always assume such a case. So we want to always assume that the value that\'s stored in here is a garbage value. So let\'s go ahead and get rid of this garbage value now by putting a value in there. So in the next step here, we\'re going to say myint is equal to some random value, let\'s say value 58 here. So that means we\'re going to go now to myint location, that\'s what\'s telling us here. Again, this is an alias to the address location of 32 and that\'s the beauty here of working with a high-level languages that we don\'t need to remember what address we\'re located with, we just need to remember the identifier that we gave it that represents that particular location. So we go to myints, which is right here, and we\'re going to store inside the value of 58 and that\'s what do we have in there. Afterwards we can do whatever we want that with this particular value. We can say maybe display it. So we\'ll do a little cout, myint followed by a new line and so that\'s it. That\'s it with this very simple program. So once we\'re done with this, then the memory will get released from here and this 58 that is meaningful to our program is essentially going to be garbage value for the next program that\'s run that uses the address 32 in here. So what did we learn here is that declaring a variable only allocates space. It does not set it to a known value, meaning this known value is not essentially zero. Let\'s go ahead and talk about our next topic, which is identifier\'s. Identifier\'s, they\'re used as names for variables and other items in a C++ program and that\'s what we just discussed here with my int. Let\'s go ahead and write that definition. It\'s used as names for variables and other items in a C++ program. Another thing is that we have some rules. Lec plus plus has some rules in the naming convention of the identifier\'s. The identifier must start with either a letter or the underscore symbol. The underscore symbol is again the hyphen, but again in lowercase here and all the rest of the characters must be letters, digits, or the underscore symbol. We need to have this particular combination for the compiler to see the identifier as a valid syntax form. If it\'s not, it\'s going to give you an error and you will have to fix that throughout the program where you use this particular identifier. Let\'s go ahead and look at some examples of this. We can have x as an identifier. We can have x1, we can have x\_1, we can have \_abc or capital ABC or ABC with a combination of some random values like this all together and etc. Now the only problem here with this is that these identifiers that we\'re using here are not really meaningful. They\'re X, XY and it seems like we\'re doing here some math where we have Y equals MX plus B. It\'s a bit abstract in that particular case and so as you\'re programming it, it may not make sense when you refer to x and you\'d be thinking, \"Well, what does x represent? I forgot what did I put it with? X1. What does that mean? Is that a rate, is that a height, what is it?\" You want to create here some meaningful identifiers to work with. As an example of this case is you want to create something like a sum or a rate, a count, data. Whatever it is that is meaningful to the program that we\'re using at hand and so as you can see, we have identifier as sum, you\'ll know that that\'s going to have the total of sum particular value, whatever it is that you\'re making the program of or a rate. We know that\'s going to be something in terms of miles per hour, meters per second, a count, so a count are here, data, the data set that we\'re working with. All of these here are very meaningful identifiers that we\'re working with and so when we look at our coding style guidelines, we\'ll later make a separate lecture particularly for that, you\'re going to make sure that you make every single of your program have meaningful identifiers. If you don\'t, then that\'s going to cost you a little bit on your homework assignments, your projects, and whatnot. This is a very good programming practice to have some meaningful identifiers as you\'re working with, if you go on Stack Overflow or any of these other websites, you\'ll see some programmers who definitely know what they\'re doing. But the thing is that they\'re making their code very hard to read and understand and you want to make it easier for yourself and easier for others as you\'re working in a team potentially where you\'re a software engineer, you\'re working as a group, just like Google. For example, I was talking to a friend of mine today and he works at Google but he just told me that he switched over to still working with Google, but a different team now in Tokyo and in India. Working with a group of people like this, you want to make sure your code is very readable to everyone and in the same format, that\'s what you\'re going to do. You\'re going to practice for yourself so others can\'t read your code or even yourself 10 years down the line as well. There\'s code that I wrote when I first took this class more than 10 years ago and coming back to it, I am thankful that I was able to program with these particular styles so that it makes sense what I\'m doing. Those are some examples. Now, let me give you another case here. Suppose that we have three identifiers, say rate, all lowercase. Rate that starts with a capital R. Then RATE that is all capital letters. My question for you all here is, looking at these three variables, are these the same or are they different in terms of C++? You probably guessed it here, but C++ sees this as different variables, or in other words, different identifier\'s. C++ can compile all three because they\'re all unique, since C++ is case sensitive. The next part I wanted to discuss here is defining keywords. Keywords are reserved words that we\'re working with, meaning they already exist in C++ at that particular name and we cannot change it for any reason. As we define these, these are defined keywords. In other words, reserved words. These are predefined, predefined meaning in C++, that you cannot use as names for variables or anything else. If you try to use it, then the compiler will also give you an error saying, can\'t change this name or any of those readings. Some examples of these particular cases are int, char, float, return, etc. All of these are keywords and there\'s some other ones as well. Another example here is that cin and cout are not reserved words. The reason for this is that we\'re including a library when we say using namespace std and someone else wrote cin and cout to have that represent the communication between the keyboard as well as a communication to the monitor from your program. Since they created that, it\'s not in the fundamentals of C++, then we can reuse those. Now I\'m not saying you should do something different because if you do, it\'s going to look a little weird, a little bit abstract to anyone else who looks at your code but it\'s just an example that these two can be renamed, where it\'s not so obvious that it was possible. Let\'s go ahead and look now at the general syntax for identifiers. In general, it\'s going to be the data type, followed by the identifier that we\'re using and then a semicolon. So these angle brackets are just to show you what would go in there, but it\'s not something that you would have to write. Let\'s look at an example of this case, and we already saw this at the above example. In this case, what\'s the data type that you want to create? It\'s going to be an integer. What do you want to call it? You can call it whatever you want as long as you follow the naming convention that we just described up here under the identifier section. Here we can say it\'s an integer, let\'s call it myInt. Another example here that we can use as well, maybe we want to include some decimal values in here. Let\'s create a double and again, we can call it whatever we want, let\'s say myDouble. These are just some examples here that we can work with. All right, let\'s go ahead and look at assignment statements next. You can find all of this on page 48 in your book. What are assignment statements? These are used to store values in variables. In other words, meaning, we\'re going to go to a particular location in memory and assign a particular value and we already saw this right here. We did the value of 58 and we assigned that to myInt, which is at location number 32 in terms of address. In the same format, that\'s what an assignment statement is. Let\'s go ahead and see some examples of this. The first one here, myInt is equal to 12. What this means, I\'ll put here a little comment, is we are assigning 12 into myInt location wherever that may be. More importantly, is that the equal sign that we have there, this one here. That\'s what it means here of an assignment operator. As we progress with our course, we\'re later going to see how we can overload this assignment operator, but that\'s going to be towards maybe three weeks before the semester ends, somewhere around there. So be sure to watch out for those. Another example here, let\'s say example number two, is we have myInt is equal to 12 plus ten. What\'s going to happen here is 12 is not going to get assigned to myInt. Instead, we are assigning first the evaluation. In other words, what we\'re doing here is we\'re first going to evaluate what\'s on the right-hand side. We\'re going to evaluate what 12 plus ten is. That\'s the first thing and we know that this is the value of 22. What we\'re going to do after the step is we\'re going to overwrite what exists already for myInt here. If we take it from the next step from above to follow into this next step, myInt at the moment has a value of 12, and let\'s go ahead and draw it in memory so it makes a little bit more sense to us all here. Suppose this here is myInt and let\'s say we have this address, let\'s say 48. In the first part and the first step here, when we said myInt is equal to 12, that means we had a garbage value that\'s located in myInt and now we\'re going to override it and assign the value of 12 inside. In the second step, what we\'re going to do is we\'re going to take the value of 12, that\'s hard coded right here, the value of 12 and what we\'re going to do is, we\'re going to evaluate what 12 plus ten is, which is 22. Then we\'re going to overwrite the existing 12 value that we have inside the memory with the value of 22. In other words, this is no longer 12. It\'s going to be now the value of 22 that\'s stored inside. Let\'s go ahead and finish this definition here. Evaluate 12 plus ten first, which is 22, and overwrite and reassign 22 into what we call myInt in that particular location. Let\'s look at a third case. In the third case here we can say myInt is equal to myInt plus ten. Looking at this and continuing from the second point here that we have, what this is going to do, is it\'s going to use myInt on the right hand side first. Everything goes from assignment from right to left and what we\'re going to do is, we\'re going to see what myInt is at the moment. If we look over at our memory, we currently have there the value of 22, as we can see and we\'re going to use that temporary value that\'s stored in there. This is on the right-hand side, is going to be 22 plus the value of ten. This here is going to give us the value of 32. From this 32, it\'s now going to assigned all of this over to myInt location over here on the left-hand side. So this is going to update once again the value to 32 and what happened again is we use the current value that was located at myInt, which was 22 add ten to it, which gives us the value of 32 and then assign this over to the left-hand side here of myInt. And that\'s again where we get the new value of 32 that we see there in the memory and that\'s the assignment statements here, how we can work. Just remember that it\'s always going to be assignment from the right-hand side, going to the left-hand side. It\'s not going to be from left to right so that\'s a very common mistake that I see with beginners where if you\'re trying to assign something as an example, you\'re trying to assign 12 to myInt. This is not the correct way because everything is from right to left and here we\'re doing it from left to right so make sure that you remember that as you\'re working with it. That way you can not get any errors here as we\'re working with our programs, let\'s talk about integers now, integers, we have different types, let\'s label that here, n types here are, the first one that we have is what we call a short int. And we\'re going to start here going from small then we\'re going to go little by little to a large state, so short int contains a total of two bytes. We already know that one byte contains a bits so two bytes is going to contain twice as much so it\'s going to be 16 bits, but we\'re going to talk about bytes. The next one that we have is just the regular int and we have here a total of four bytes of memory for that one. The last one that we have is a long int and this is what we classify as our large size and this one has a total of eight bytes, we can draw here little diagram that we\'re going from small to large. The cool thing about working with integers is that we can use the usual math operators on ints for example, we can add two integers together. That\'s what we saw in our previous case here with the assignment where we have the 12 plus ten, 12 is an integer, ten is an integer as we have no decimals there so we can use addition, we can use the subtraction symbol here, we can use the multiplication. Multiplication is with the asterisk located with your number eight and then our division symbol. Again, it\'s just this forward slash here that we\'re working with so we can work with all of those particular cases. In fact, we have another one that we can even use is the modulus operator. We\'ll talk a little bit more about this shortly but the modulus operator is figuring out the remainder of two integers. One fact that you must remember is that it int divided by an int always produces an int. In other words, the remainder is tossed away, let\'s go ahead and see some examples of this particular case here. If we have eight divided by four and we know that\'s just going to be an even number, which is the value of two. If we have here as an example of seven divided by two, That\'s just going to equal. Well, I didn\'t know one notebook here does the math for you. That\'s very cool, but this is the true answer, so this is good. That came out. This is the true answer. Seven divided by two is 3.5 but since we\'re dividing an integer divided by another integer, meaning seven is an integer, and two is an integer, this 0.5 is basically lost so in reality, we just get the value here of three that\'s the crucial thing here to remember and you might be thinking, well, does it, does the three go? Does it round up, In other words, or does it round down? There is no such thing as rounding and for example, hypothetically, if seven divided by two was 3.9, it will still be three. If it was 3.5, it will still be three, 3.1, still it is three so you\'re always going to just literally just take that whole number every single time and you\'re literally not going to look at the decimal value. Some more cases here is, suppose that we have the value of 13 divided by five. What is that going to equal? We know that 2.6, but that\'s just going to be the value of two only, how about another one? How about we have three divided by seven? What is that going to equal? We have this fancy decimal value, 0.4286, but in reality it\'s just a value of 0 only. Last one. Suppose we have three divided by 0. What does this equal to? Well, look at that one note that it didn\'t even bother to figure that out for me but of course, anything in terms of division by 0, we cannot do so at this point, your program will crash and it\'s going to give you what we call a segmentation fault. It\'s very popular error that comes up at runtime and so your program as it\'s running or as it\'s executing, it\'s going to abruptly stop, goes back to the terminal and it\'s going to say seg fault or segmentation fault to produce that entire wording there and that just means something happened, that shouldn\'t happen in this case it\'s a division by 0. Let\'s look at some other cases. I talked about the molecules operator to get the remainder let\'s go ahead and see that to get the remainder, we\'re going to use the modulus operator, which is this percent symbol. Let\'s see now some examples here using the modulus operator. Suppose we\'re going to use the same numbers as before; eight modulus four. That\'s going to equal the value of zero. Meaning four goes into eight evenly and it\'s going to be evaluated to 2.0 in this case and this zero that we have at the end is the evaluation that we have here from this expression. Which is the value of zero. We can take this to the side and demonstrate for you what it is that we\'re trying to do. With this one is, again we\'re trying to divide 4 into 8. We know that goes in two times, that\'s going to give us here the value of eight. We subtract this, we get the value of zero. That means it\'s going to be remainder zero and that\'s the zero value that we see there. Let\'s look at another example. Suppose that we were working with 13 modulus five. This equals here the value of three. I am not sure why it\'s coming up here as five for this, but it\'s going to be the value of three. Let us take this one here to the side and see why it\'s that. We\'re dividing 5 into 13, 5 goes into 13 2 times. That gives us the value of 10. We subtract, gives us the value of three that\'s left over and this is what we see as the remainder of three, because five can no longer go into the value of three, it\'s just a remainder. We can also see this as a fraction where this is the same as saying this is two and three fifths.This will be important here to see when we work with the next example here that I want to show you. This next one, suppose that we have three modulus seven. This here gives us the value of three. How do we get such a case here? Let\'s look. We have here 7 and we want to divide that into 3 here. We know that\'s going to be zero times that it goes in. There\'s really nothing that we can do here at this part. We can just do some decimal arithmetic. But in reality what does this mean? This just means three over seven. There\'s no number that\'s in front. In other words there\'s no number that\'s there and what we\'re going to look at here is that, this is already the remainder here. That value of three. That\'s our R and that\'s what we see here in this expression. This value of three. Let\'s look at some other examples. Suppose we have now three modulus zero. We know here we\'re dividing by zero and again we\'re dividing by zero because division and modulus is the same thing. It\'s just getting the remainder. This one here is the one that\'s going to crash here for us. There\'s nothing here that we can possibly do with this particular one besides again getting a segmentation fault. Okay? Let\'s go ahead and move to another topic. Let\'s talk about floated point numbers or floating point numbers as well. This is going to be on Page 60 in your book. In the same concept as we described with our integers, there\'s different versions that we can work with. Just like we have the short end, regular ends, and a long end that we were looking at over here. We\'re going to discuss the same way here with our again floated point values. The first one is we have as a float. This is what we consider here to be the small size and this float here has a total of four bytes. The next one is a double and this one here has a total of eight bytes and the last one is a long double and this is what we consider to be our large size and this one has a total of 16 bytes. With a floating point numbers, as we\'ve discussed, these can hold decimal values. Is just depends on how much it is that you want it to hold. If you know, you\'re going to be working with large numbers or a large quantity of decimal values that you want to have in terms of significant figures, then I would go with something like a long double. But if you\'re crunch with the size because potentially working with something small like a memory chip in your like a watch for example, or something that\'s minute, then you might want to go with the float because that\'s only going to cost you four bytes as opposed to just a double with eight bytes. But for our programming here, especially in our intro class and using the server, you don\'t have to worry about the size. You can work with a double and an integer. You don\'t have to work with the floats which are long double, but you\'re more than welcome to start thinking in that particular mentality of what are the possible ranges of numbers that I can work with and what\'s going to be the appropriate size that I should have. Nothing wrong with getting into that mindset already. But again you don\'t have to worry about that. Let\'s go ahead and built now an example, let\'s create a program here, and in this program, let\'s test out the idea of how we can make a simple program where we can ask the user the number of hours as they work this particular week and what is their expected wage that they should get at the end of the week or the month, whatever may be that they are working. That way, when they run our program, they just enter the number of hours, their wage and we will display what they will have made in that particular time-frame. We\'re not going to worry about deductions in terms of taxes and one one not. We are here just assuming that they\'re going to keep a 100 percent of what they made. Let\'s look here at our program. Let\'s start building it. We have here our pound include Iostream. Then we\'ll have our using namespace, std. After this, we\'ll have our main function, and in our main function we want to think about here, is what variables do we want to use. I like to create these variables at the very beginning of our program. That way, it\'s all located at one central location as opposed to randomly allocated in our program at different spaces. That way, in case we need to add one more variable or remove one that we never used, then we can just go back to that same location and just either add it or remove it. One thing that we\'re going to require here is the number of hours that the user is going to enter, and the number of hours you can think, all right I worked five hours, but they can also work in terms of fractions. For example, 5.5, 5.1, especially if you have some time clock that you are punching into. So it\'s best to create this as a double. We\'ll have here a double, and let\'s call it number of hours. Then we also have to know the hourly pay that they are being paid. Whether it\'s \$13 an hour, \$15, \$20 an hour. But it can also be in decimal. It could be, 15.5 or 16.7, something like that. So again, we\'ll have here double for all of this. So let\'s call this hourly pay. Now notice I\'m using a double as opposed to a float. Floats will be fine here to work with. But like I said, we can easily work with a double. Now, do we need any other variables here? Well, we can use one more. How about the total. The total pay that they will get. Again, it\'s going to be a double since we\'re multiplying the number of hours, which could be a double hourly pay, which can be double, that should also be a double but for the total. So let\'s put here our total, and that\'s it. That\'s everything that we need here. Now the next step now as we mentioned, is we want to prompt the user to enter the number of hours. So we can use the cout statement followed by the insertion operator, and then we prompter the user. The number of hours, little colon, and our quotes here. So this will display up for us. Then we need to get the information here from the user as they enter it from the keyboard. We\'re going to use cin that reads from the keyboard, then our extraction operator, and then where is it that we\'re going to store it. So we\'re going to store it in our Num hours that we have here. That\'s what we\'ll write down here. So number of hours, that goes in there. The next thing we\'ll ask here is, what is their wage? Again, we will read that in and store it in the appropriate location, which is the hourly pay. After this, we are done. We don\'t need to prompt Any other stuff. We have all the information that we need. Now we calculate what the total will be. We\'ll say here total is equal to number of hours times the hourly pay, and that\'s going to update it for us. Now, one thing that I\'ve seen many times that beginner programmers do is, they think that if I copy this and if I put it down here, then it\'s going to already calculate what the total is based on the number of hours and the hourly pay that they enter afterwards. But remember, at this point of creation for these variables that we have here, these are all garbage values. So this total that\'s given here, it\'s going to be a garbage value, and so when we go down here to display it down the lines, we\'re going to display some garbage value where we never really used the information that the user gave us in these particular steps. So make sure that each step that you do have the precise order, that\'s how things work here in C++, and in any program that you\'re working with. After the number of hours and after the hourly pay has been updated, then we calculate the total to update that for us. Now that that\'s been updated, now we can print out the total for the user, where we can say that the total is and then let\'s make it a little fancy and put a little dollar sign for them, and then a space, and then we want to display what that total is, what it\'s located at that memory address, and then we\'ll have our new line at the end, and then finally, our return 0. That\'s our program here. Let\'s go ahead and make this a little bit pretty, and then we\'re going to run it together through one note, we won\'t go to the terminal. I want you guys to start practicing running these things here. In another words, this code on piece of paper. Ideally that\'s how it\'s going to be for your exam, you\'re not going to be working in the terminal. You\'re going to be working on Canvas. You\'re going to make this entire program, and as you\'re looking at your code, you kinda wanna run it in your head. You\'re more than welcome to take a small piece of paper, put it on the side, jot it down what the steps will do at each case, and from there you can figure out whether your program works. Let\'s go ahead and see how this program is going to run. The first thing that we see, is we\'re going to allocate space for three variables here; our number of hours, hourly pay, and then the total. We can create this memory, and again, we\'re going to have here three variables. Let\'s say this is number of hours, this is our hourly pay, and then the total. Let\'s go ahead now and give these some hypothetical addresses, and because we\'re working with doubles, let\'s say this one is located at address 20. This one\'s located at address 28, and then the next one here is located at address 36. These so happen to be next to each other. But it doesn\'t always happen this particular way. For example, if you attend class at seven in the morning at Fullerton College, with a group of your friends, you guys can all park right next to each other. But say you go together at, say, 9:00 AM or 10:00 AM in the morning, it\'s a little bit more crowded. It doesn\'t mean you\'re going to be parked in the same location. One will be parked in one lot and other one will maybe in another lot, but not next to each other. The same way it\'s going to happen here with our memory. They can be located together or not. The next thing to notice here is the first value is address 20, the next values address 28. Why is that? Well, if we go back to our description here of a double being eight bytes as we see there, then that\'s the spacing that goes in between them to allocate enough room to store them. That\'s why we have these eight difference each time. So 20 plus 8, 28, 28 plus 8 gives us 36. That\'s if we want them to be sequential. Now, because all of this here is essentially hypothetical in terms of the addresses. Just to get the understanding of what goes behind the scenes, we just set it up in this particular format. Now that we have our memory setup, and one thing that we want to mention here is that, everything that\'s inside of the number of hours, hourly pay, and total, there is some value in there. It\'s just a garbage value. But that\'s okay. We don\'t need to zero it out in a step in between in our program because it\'s going to get overwritten in two steps. It\'s going to get overwritten here when we read it in, and it\'s going to get rewritten for the hourly pay at this particular point. It\'s okay that we\'re working here with garbage values because we know that they\'re going to get updated as our program here progresses before we actually use it meaningfully. Let\'s make a window here that is going to represent our monitor. We call this here standard out. This is again our monitor that we\'re working with. The first thing that we\'re going to see is our first cout statement, which says number of hours followed by this colon and a little space. The next thing here that we want to provide is the number of hours that we have worked, or whatever hours that you think that we may have worked here. We will go ahead and put some hypothetical value in there. Let\'s say it\'s 34.5. That means, we\'re going to go to the number of hours location in our memory, and that\'s going to extract for us, as we can see here, this is going to

Use Quizgecko on...
Browser
Browser