The important thing to remember when it comes
to math and programming is that the computer
does the work.
You just have to copy the equation down properly.
To do so, you need to know a few math operators
plus some other C Language rules and procedures.
In this movie, I review the basic math operators
and how they're used.
Also explored are the increment and decrement operators,
plus, a few notes on the order of precedence,
which determines which part of an equation
is evaluated first.
The C language uses four basic symbols
for the four basic math operators.
Addition, subtraction, multiplication, and division.
These should be familiar to you, especially
if you've used a spreadsheet.
The asterisk is used for multiplication,
that's because the multiplication symbol
isn't available on the keyboard,
and please don't use the letter "x".
Similarly, the division symbol isn't found on the standard
computer keyboard so a slash is used instead.
Two additional operators are increment
and decrement, these are single operators despite
using two characters, these operators serve
a useful purpose when dealing with loops
which is a topic covered in a later movie.
And in C, the calculation goes on the right.
The result is then assigned to a variable on the left.
Or, the result can be used immediately
as in the "printf" function shown here.
Two variables are declared, "a" and "b."
In line 6, I've preset the value of variable "b" to "5."
The "scanf" function in line 9, reads in an integer value
storing it in variable "a," then the "printf" function
displays both values "a" and "b" as well as their sum.
The math equation is the third argument
in the "printf" function, build and run.
Type in a value to see the sum of that value and "5."
To change addition to subtraction, you need to make
only one modification, look at the code for a second,
and see if you can spot the place where
that modification would take place.
Here's where it would, right here,
this is the equation to change addition to subtraction
simply change the operator and change it here
in the display string just so that it makes sense
when you run the code.
Save, build and run.
You'll notice that negative values are calculated as well.
If you wanted to try multiplication, again you need
to only change the operator, look at the code
and figure out on your own where that change
would take place.
Again, it takes place in the "printf" statement at line 10,
change the minus operator to multiplication,
and then the display string as well.
Save, build and run.
The final modification as you've probably guessed,
is division.
But division can be tricky especially with integer values.
Let me show you, I'll change the print statement
to reflect division, it's the only change I'm going to make.
And I type in a value such as "9."
The result displayed is "1," which is dubious.
To do division properly, you need to use
"floating point values."
Open exercise file "math2."
This is basically a modification of the existing code
switching into values to float, as well
as the associated placeholders.
Also the value "5.0" is assigned to variable "b"
in line 6, remember, floating points have
a decimal part so that when you're writing
a whole number, you need to specify ".0",
build and run.
Try the value "9" again.
The result is more palatable but ugly.
A later movie describes how to clean up the output
of floating point values.
Open exercise file "math3."
Math is present at line eight, it adds one to the value
of variable "x," this equation may look funky to you,
but it's how it works.
The compiler calculates the right side
of the equation first.
So "1" is added to the value of variable "x."
That result is then stored back into variable "x."
To prove how it works, build and run the code.
The original value is "10," and the modified value is 11.
You can manipulate any single variable in this manner,
the most common manipulation is to add
or subtract "1" from a variable's value.
For example, if you wanted to increment
the value of variable "x" by "1", you use the increment
operator, which is two plus signs in a row.
Change line 8 in the code to read simply, "x++".
That's a statement by itself, it's a mathematical operation.
Save, build and run.
The decrement operator is two minus signs.
Edit the code again to replace the increment operator
with the decrement operator.
Minus, minus.
Save, build and run.
Now the output is "10," "9."
The final math goodie I want to show you
deals with something called "the order of precedence."
It involves math equations that have multiple operators.
Load up exercise "math4."
Can you guess the value of variable "a"
by looking at line seven?
Run the code and see what the computer thinks.
The computer thinks the answer is "13."
That's because multiplication and division
have precedence over addition and subtraction.
The equation is also read left to right.
So "25" divided by "5" is "5",
times "2" is "10" plus "3" is "13."
You can change that order by using parentheses.
For example, I'm going to enclose, "2+3"
in parentheses, which means that part
of the equation is evaluated first.
Save, build and run.
Now the answer is "25."
"2" plus "3" is "5,"
"25" divided by "5" is "5" times "5" is "25."
Now I'll add more parentheses, remember to match
them up, save, build and run.
Now the answer is "1."
That's because "2" plus "3" is "5"
"5" times "5" is "25" and "25" divided by "25" is "1."
Remember in C, equations are evaluated,
multiplication and division first,
then addition and subtraction, unless,
you use parentheses to modify the order.