Last time, we learned an important lesson as we're implementing stem computations, and that important lesson in case you've forgotten already is that if there's a function or more than one function available in the C standard library, we should use that function rather than implementing our own algorithm for solving the same problem. In this lecture, we're going to solve another stem computation problem. The problem is, given the x and y locations for two user-specified points, calculate the distance between them. Now, we certainly can do this in three dimensions as well. We know how to do that math, but we're going to keep it simple, and we're going to stay in two-dimensions for this particular lecture. Let's go implement the C program we need to implement to solve this problem. I'm going to start by pound including math.h because I know we're going to use functions in math.h. The next thing I need to do is to declare variables for the four pieces of user input. So, the user is going to input the x and the y for the first point and the x and the y for the second point. So, I'm going to call this pointOneX for my first variable. Now, you could certainly use the number one rather than the word one. My preference is to usually not include numbers within my variable names only because the number one can look like a lowercase L, but that is totally personal preference. That's not a rule in C or anything like that. So, we'll do pointOneY, and we'll do the second point as well. Now, I am going to prompt for and get user input. We're going to do that the standard way with printf and scanf. So, I won't make you watch me type that in there. I'll be back in a moment. So, here we go. Here are the prompts into the reading in for each of those four pieces of data. Remember, in scanf we're providing the address of the variable that we want the user input to go into. So, let's control F5, and make sure that all works. So, I'll enter 1,1, 2, 2, and the prompts all work fine our code doesn't crash. So, we can continue. The next thing we need to do is calculate and print the distance between the two points. To do that, you actually need to know the Pythagorean theorem. So, let's go take a look at what that is. Here it is. So, basically this says a squared plus b squared is equal to c squared. So, we are actually trying to calculate c here, given this point and this point. So, what we have to do is we have to calculate the difference in x and square it. We have to calculate the difference in y and square it. We add those two together, and that will give us c squared, then we need to take the square root of that to actually get the distance between the two points. Now, I'm going to declare a variable called distance. I know that I need to get the difference between the X's. The order of subtraction doesn't matter here because I know I'm going to square this, right? I'm calculating a squared at this point from the Pythagorean theorem. So, it doesn't matter if I do pointTwoX minus pointOneX or vice versa. We're going to get a positive number when we square it. Now, there are multiple ways to square this value. One would be to multiply by pointOneX minus pointTwoX. But, there's got to be something in the C standard library to let us square numbers. So, let's go take a look. We should go to the C standard library, and look in math.h and see if there's anything in there that's useful to us. In fact, there is. You shouldn't be really surprised. If we look down here in the power functions area, we see that the power function raises a number to the given power. As usual, there's a powf, a pow and a powl. We'll use powf, and we'll give it a base and an exponent. So, we know that what we need is to use powf, and we provide two values, the number we want to raise to a power and the power we want to raise it to. So, that's how we square the difference in X. So, this is a squared. Now, we want to add b squared to it, the difference in Y. So, I'm going to copy and paste. I could paste right here, but that line is going to get pretty long. So, I'm going to move it down here and paste it, carefully changing the X's to Y's. I need to get rid of my compilation error. I just can't live with it anymore. So, I finally put the semi-colon. Now, on the right-hand side of this assignment statement, we have a squared plus b squared. I know I need the square root of that. So, let's go look at the C standard library to find a square root function. Luckily, math.h contains square root as well, as usual with f's, naught, and l. So, I'll use square root f here to calculate the square root of a squared plus b squared. So, now, I'll have c. I'll have the distance that I've been trying to calculate between those two points. Now, I'll print it out. Of course, I'm printing this out as a float. I'm going to actually print it out to two decimal places. Let's see if we get the right answer. So, I'll control F5. 1, 1, 2, 2, and I get 1.41. I'm actually going to add a blank line between the user input and the results I print out. You could even add that blank line here if you wanted to. You've seen me put this \n at the end of a line, but I can put it at the beginning of a line. I tend to actually put it in its own printf, but that is again personal preference. You can do that where you want. I'm going to run it again just to make sure I didn't break anything. I didn't. Now, you'll notice that I claimed that I'm getting the right answer. The moral of that story is you actually have to know what the right answer is to make sure that your program is doing the math correctly. I picked easy points so that's delta x was one and delta y was one. So, I knew that the distance was going to be square root of two, and square root of two is 1.41 to two decimal points. So, you have to know what the right answer is before you run your program to confirm that your program is working properly. To recap, in this lecture we got more practice developing C programs to implement stem computations, and we got some more practice using the functions in math.h.