Hi, I'm Greg Teichert. I'm a PhD student who's working on finite elements with Krishna Garikipati. And for the next few segments, I will be introducing you to programming in C++. Now, this isn't meant to be a comprehensive overview of C++. It's really just to give you the basics, the basic understanding, so that you're able to do the coding assignments that are associated with this course, all right? And so let's begin, I'll write down an overview of what we'll be talking about in this first segment. First, I will talk about how to run, Your code, your C++ code, using CMake. Then we'll move on to talking about the basic structure of a C++ program. And from there, we will move on to talk about the basic number types, or a few of the basic number types, In C++. And then we'll move on and talk about C++ vector, standard vector. All right, so when we're running the code using the deal.II finite element library, we'll be using a building system, a build system called CMake. And CMake takes your C++ code and it creates a makefile, which will then be used to run the code, all right? So we'll move over here to the desktop screen and you can see here I have two files, I have this CMakeLists.txt and main.cc. .cc is the file extension that specifies that this is a C++ source code file. The CMakeLists.txt is what CMake uses to identify the source code, all right? So this is a file that we'll include with the coding templates, with your assignments. There's also a CMakeLists.txt file included with all of the deal.II examples, all right? So here on line number six, it says set target, in parentheses, main. So that specifies the name of the file of the source code that we'll be looking at. That's main, of course, main.cc. The .cc is specified here on line ten as the file extension, all right? If your source code had another file name, you'd simply change that here, okay? For example, for the examples in deal.II, they're all labeled step one, or step two, and so on, all right? But I'll leave it as main for now. And from there, in my terminal, I'll go to the directory where those files are held. So they're on my Desktop in a file called Example, okay? If I list the contents of that file, you can see there are CMakeLists.txt as well as main.cc, okay? Now, the very first time we want to run the code, we need to run CMake and CMake will create the Make file that we need. So I type in CMake and then CMakeLists.txt. And there, if I open up the folder again, you can see it's created several different files, one of which is this Makefile, okay? Now, we only have to do this once. The only time we'd need to run CMake again is if you needed a new Makefile, all right? So from here on, we will refer to the Makefile and the Makefile will link to the source code. And we type in make, make builds, the executable. If we want to run it as well, we type in make run, okay? And there, you can see it, Built the target and run the target, okay? So that's the way that we are going to be writing our C++ code. Both in these tutorials, in the beginning, and then also for the templates, for your assignments, all right? Now, we move on to discussing the basic structure of the C++ program. So I'll open up this main.cc file and you can see it's pretty sparse, there's not much in it. These are the basics of what you actually need in you C++ program. And this actually is in the same structure as a C++ function. And we'll talk more about functions later on in one of the following segments. But you can see here each function has a name. In this case, the name is main, it has or can have inputs. The inputs would come here between the parentheses. As you can see, this function, main, doesn't have any inputs. The outputs are described right before the function name. In this case it's int, int stands for integer. So that means this function, main, doesn't need any inputs, but it has one output, an integer. Now, down at the bottom, it says return 0. Here, we're specifying what specific integer we'll be returning. And we return 0 to let the computer know that the program ran correctly. For example, if you had come across an error, you might do return one. And so the compiler or the operating system would know that you had come across an error and that’s why you're returning the program, okay? So that’s the very basic structure of the C++ program. Of course, it's not doing anything. And in order to add some more functionality, we add header files. Header files include additional functions and data types that we can use. So let's add one of those, and we add header files using #include. And I'm going to add a file called iostream. And iostream allows you to print statements to the terminal, to the screen. It also allows you to input statements from the terminal. So for now, I will just be printing the statement to the screen. One of the functions included in iostream is cout. I do this standard std::cout to specify that cout is a standard C++ function that we're using, okay? And so we want to print out to the screen, I'll print out the canonical example Hello, world!, okay? The quotes specify that it's a string that we're printing out words and std::endl just ends a line on the screen, like a character turn, okay? So we save that. Again, we don't need to run CMake again, because We're using the same make file even though the source code itself has changed. So, I'll type in make run. And there you can see on the screen, we have hello, world. Now most likely, we want our program to do more than just print words to the screen. We will be using numbers of various types. And so, let me talk about what some of those types are. I won't be discussing all of them, but just a few. So, there are actually mostly just three types of numbers that we'll be using in the templates. The first is in and we saw that already in the function. We were returning an int. An int is an integer. So, that 012 minus 1 minus 2 and so on. The second number type that we use quite a bit is an unsigned int and an unsigned int is just what you'd expect. It's an integer without a sign. So, it's non-negative. We can't store a sign with that. The third number type that we use quite frequently is called a double. A double stores real numbers and I'm not going to get into the specifics of how it's storing these numbers. I will explain though, that it's called a double. Because there is actually another number type called the float, which also stores real numbers. A float uses 32 bits per real number and can you guess how much a double uses? 64 bits. Double is short for double precision. We use more memory to store the number. And so, we can get greater precision with those numbers. So, let me show you here in the source code how we would declare these integers and real numbers. So for int, I type int. We have to let the program know how much memory to store for each of these variables that we're declaring here. So I can declare int a, I'll name it a and you'll notice I put a semicolon at the end there. I can also declare multiple ints at the same time, separated by a comma. I can specify the value while I'm declaring it like I do d = 1, e = 3 and maybe f without actually specifying anything there. So, I can do all of that. Somewhere with an unsigned int. Specify a number there and same with the doubles. 1.3, for example, i. So all of those are valid declarations of an integer, unsigned int or a double. Now, there's one thing to be careful of and this is a bug that can show up in, they will probably shopping at code at one point or another. It certainly shown up in my code and that's the fact that, okay, we understand that an integer plus an integer equals an integer or an int times and int equals int into minus int equals an integer. But in C++, an integer divided by an integer is also an integer. And so for example, if we print to the screen 3/4. What would you expect it to print to the screen? We would or I would've expected it to print, 0.75. But if you look here, it printed to zero. That's because three an integer, four is an integer. When you divide it, C++ wants an integer. And so, it truncate any decimal values and takes only the integer in front of the decimal. So if you want C++ to recognize that 3/4 4 is 0.75, you need to put a decimal on either the 3 or the 4 or both. So now if we run this. You can see it prints to screen 0.75. So, that is something to watch out for. So if you want your quotient to give you a double, make sure that the numbers involved are also doubles. So for the last topic in this segment, I want to talk about a standard C-++ vector and these are very useful objects in C++. A vector as you might expect from mathematics is an array or a range of elements. However, as opposed to mathematics where we talked about vectors and a vector would be an array of numbers. In C++, you can have a vector of any other C++ objects. So, you can have a vector of doubles. You can have a vector of integers. A vector of unsigned ints. You can have a vector of characters, of letters or you could even do a vector of vectors. And so, they're really quite handy. It's not just a mathematical object, but you can also think of it as a storage device. Now over here on the inner code, we'll look that to use vectors actually have to include another header file. So, I pound include vector. And when we declare vector, we use standard vector. Again, because it is a standard C++ object. We have to declare what this vector will be storing in it. It can only store one data type. So for example here, I'll store a vector of doubles And again, as with integers and doubles and so on. There are more than one way to initialize a vector. So, we can declare its name. Without saying anything about it. When we declare simply the name, then the vector has size of zero. There are no entries. There are no spots for any numbers. It has a length of zero. Let's declare another vector here. Make a vector double, as well. And here, I want to specify the size. And so in parentheses, I'll put 3, for example. So now we've declared a vector of size 3, of length 3 However, the doubles that are stored inside of it have a default value of zero. Now if I want to make a vector of length three where all the entries are one, for example. I would again, put in a three for the first entry. And separate it by a comma, I would put one. And so, that would make every entry initialize as one. And as before, we can initialize more than one vector at a time just like with doubles or integers. Now, a vector differs from the integers and the doubles in one way in that a vector has several functions associated with it. And now I'm saying, function. But again, functions in C++ doesn't mean the same thing as a function in mathematics. We're not necessarily taking a number or range of numbers. Performing some mathematical operation to produce another number. A function in C++ performs some operation, but it's not necessarily a numerical operation. So, one common function in for vectors is this resize function. So when I declared vec1, it was empty. I want to resize it, so that it now can contain five elements. So now, let me demonstrate another function. If you want to see what is the length of the vector, you can do vec1.size and I'll print that to screen and we'll see that. It should print out five, which is the length of vec1. Now, we can access any component of the vector using square brackets. So, let's look at the first component. Notice indices in C++ start at zero. So vec1 square bracket 0, square bracket should print out the first element of the vector vec1. So here on our screen, we printed out the size which is five. And the first element, which is zero which is what we expect. Because when we resized the vector, we didn't specify the values. And so, the default value was zero. We can also resize vec2 and vec3 even though they've already been initialized. We can resize those. Another function that is this handy is the pushback function. So right now, vec1 has size of 5 and all the elements in vec1 are 0. Now let's say, I want to add another component to the end of vec1 and I don't want it to be 0. I want it to be, for example 3, maybe 3.2. So, this adds another element on to the end of vec1. And so, what that does. Is it increases the size by one and we'll be able to see that here as I print this to screen. So, the original size was 5. The first component is zero. The size after calling pushback is now 6. We've added another element on to that factor and that element contains the value 3.2. Now, there are many other functions related to vector and one valuable resource for looking at these functions. Looking at other header files in C++ and even a very informative tutorial series is found on the website c++.com. I'll write that here plus is spelled out. And so if you have further questions on something we've talked about in this segment or any of the future segments, you ca refer to c++.com. And in their search, you can search, for example, vector or double or any of these other data functions or objects that we're taking about. In the next segment, we'll move on to talking about conditional statements and for-loops and what the scope in C++ means.