Join Isac Artzi for an in-depth discussion in this video Implementing mathematical expressions, part of C Essential Training.
- We'll now continue our discussion of expressions and statements in C. And focus a little bit more on mathematical ones. Math is the root of computer science, so it is no wonder that a lot of the material covered in a computer language class is about math. Let's take for example the area of a circle. It's a simple formula which you probably learned sometime in geometry in middle school. Pi times the radius of the circle square. Or the root of a quadratic equation.
The quadratic equation is typically noted as a times x square plus bx plus c equals zero, and then there are two positive roots given by minus b plus or minus the discriminant, which is the expression under the square root, divided by 2a. This will give you two values based on whether you're using plus or minus. And then the volume of a cylinder, which is the area of the base, which is a circle, pi r square times the height.
Let's implement those formulas in C and see how we can compute that. In order to do that, we're going to introduce a new library, math.h, which we're not fully taking advantage of in this particular one, but as we move along, you will see different uses of math.h. For now we're going to explicitly define pi as 3.14159, and we will use it throughout.
We're also going to define the radius of a circle and the variable area, which obviously does not have an initial value. In a comment that's a little bit of a creative way to write radius square, we are going to calculate the area of a circle. Rather than using a function for radius square, we simply multiply radius times itself. Then we'll displaying it. Then for the quadratic equation, I am arbitrarily defining a, b, and c as two, seven, and four.
I am then calculating the discriminant using the square root in line 40 which is a use of the math.h library. That's b square minus four times a times c. And now I'm calculating the two real roots of the quadratic equation. One root is negatively plus the discriminant, divide by 4a, the other one is negative b, minus the discriminant, divide by 4a.
I'm printing the two roots as floats and now we're on to our last calculation which is the volume of a cylinder. Pi times r square times h. Calculation here is a little bit simpler, we'll simply multiplying the value of pi times r times r times h in line 56. Let's see how all this works. We're going to run the code, and as we can see, a circle with radius 5 has an area of 78.539 and so on.
The solutions of the quadratic equations are, the first root is -0.35 and so on, and the second root is -1.39 and so on, and the volume of a cylinder. We have just created a calculator in C. You can imagine that should you have a need to use very specific calculation in any area of science, you could simply create a C program that has as many formulas built in, and all you have to do is plug in those numbers.
You would note, however, that this particular example is not very useful in the sense that it has hard coded values, meaning it only calculates the areas and the volumes and the roots for those particular equations whose values are included into that file. We will now see a better approach to this. We will allow the user to enter those values. In this particular one, we'll allow the user to enter the values for a, b, and c, and calculate values of the roots for any quadratic equation.
First, we're going to ask the user to enter those values, so if you look at lines 24 through 31, we are using a combination of printf and scanf to ask the user to enter a, b, and c, respectively, and capture those as floats at their respective value positions, a, b, and c. Notice the ampersand sign preceding "a"s, "b"s, and "c"s in scanf, because scanf requires a memory address to place the value of the variable.
This is a syntax that makes scanf a little bit different than the way you use to treat variables with other functions. Further down, we are calculating the discriminant, just like before, which is the square root of b square minus 4ac, and the root1 and 2, just like the previous example. So the only thing that's different now is that we have not hard coded anymore the numbers inside the code, but we are asking the user to enter his or her own value.
Let's put this to work. I first enter the number for a, let's pick a one. I'm going to enter now a number for b, let's say 10. And a number for c. And here are my two roots of this quadratic equation. If you want to test that, you are welcome to plug those values back in to the quadratic equation for x, using the "a", "b"s, and "c"s you defined, and you should get a zero. Let's try something else.
I'm going to use a different value for a, a small value for b, and a large value for c. Notice that we did not get this time a numeric result. Nan is C's way to say, "not a number." In other words, something erroneous happened, and this was expected. If you like at line 33, there is a square root. As you recall from algebra, the square root can only be applied to a positive number, therefore if the expression b square minus negative 4ac is a negative number, you will get an error.
You will get an error simply because in the real of real numbers, we do not handle square root of negatives. That's another area of mathematics called complex numbers, which are handled separately and with different libraries, sometimes custom libraries to handle that. This is beyond of the scope of this video.
C is a great first step for new programmers, and a way to broaden and deepen your knowledge if you've already programmed for a while. Press Play to start learning.
- Reviewing the C language and the C11 standard
- Understanding the development cycle
- Setting values
- Writing statements and expressions
- Adding comments to code for clarity
- Declaring data types
- Manipulating strings
- Declaring variables
- Using operators and expressions
- Working with functions
- Controlling flow with if-else statements and loops
- Initializing arrays
- Working with files
- Including files and executing macros with the C preprocessor
- Understanding best coding practices