Join Isac Artzi for an indepth 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.
Author
Isac ArtziReleased
6/30/2014C 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 ifelse statements and loops
 Initializing arrays
 Working with files
 Including files and executing macros with the C preprocessor
 Understanding best coding practices
Skill Level Beginner
Duration
Views
Related Courses

Introduction

Welcome1m 5s

Using the exercise files2m 34s


1. Why Program in C?

Running your first C program3m 45s

2. Overview of the Structure of a C Program

Making code more manageable4m 11s

Setting values to use later4m 15s

3. Data Types

Understanding data types1m 28s

Using the void data type2m 15s


4. Strings

Understanding strings3m 24s

Manipulating strings5m 27s


5. Variables

6. Basic Programming Constructs

7. Functions

Working with recursion7m 26s

8. Decisions and Branching

9. Loops

Using for loops3m 32s

Using while loops5m 50s

Breaking out of loops5m 35s

Challenge: Make a bar chart1m 10s

10. Arrays

Introducing arrays3m 23s

Declaring arrays4m 23s

Initializing arrays3m 7s

Rules for using an array3m 57s


11. Pointers and Memory Management

Understanding pointers1m 56s

Using a pointer variable4m 29s


12. Structures

Understanding structures1m 53s


13. Files

Exploring file access modes4m 24s

14. UserDefined Functions

Using recursive functions6m 58s

15. The C Preprocessor

Defining and using macros6m 10s

Using conditional directives5m 23s


16. Good Programming Practices

Testing code correctness8m 35s

Developing portable code5m 49s

17. Course Challenges

Conclusion

Goodbye3m

 Mark as unwatched
 Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Implementing mathematical expressions