Title: Computer Science 1620
1Computer Science 1620
2- C Math
- we have seen how to use numbers in our program to
represent data - however, we can also manipulate this data
- we will look at some basic mathematical operators
3- Operators
- there are five arithmetic operators
Operator Symbol
Addition
Subtraction -
Multiplication
Division /
Modulus
4- Arithmetic Expression
- to use an arithmetic operator, use the following
syntax
numeric expression
operator
numeric expression
A numeric expression is any expression whose
value is a number. This is also called an
operand.
Operator in this case refers to one of the 5
arithmetic operators.
5- Arithmetic Expression
- Examples
- add 3 and 4
- subtract 40 from 75
- multiply 36 to 97
- divide 49 by 7
numeric expression
operator
numeric expression
3
4
75
-
40
36
97
49
/
7
In short, exactly the infix notation that you are
probably used to (just different symbols).
6- The statement
- in C is an expression
- more specifically, an arithmetic expression
- the value of the expression is
- whatever the formula evaluates to
- in the above example, the value of the expression
is 7
3 4
7Expression Value
2 5 7
13 89 102
34 20 14
45 90 -45
2 7 14
8- Where are arithmetic expressions used?
- anywhere an expression is valid
- we have seen two places so far where an
expression is valid - in a cout statement
- in an arithmetic expression
9include ltiostreamgt using namespace std int
main() cout ltlt "14 27" ltlt endl
cout ltlt 14 27 ltlt endl return 0
What will the output of this program be?
10(No Transcript)
11- Why did we get this output?
include ltiostreamgt using namespace std int
main() cout ltlt "14 27" ltlt endl
cout ltlt 14 27 ltlt endl return 0
12- Why did we get this output?
include ltiostreamgt using namespace std int
main() cout ltlt "14 27" ltlt endl
cout ltlt 14 27 ltlt endl return 0
- The first expression is a string literal,
because of the quotation marks. - Remember that the value of a string literal is
the text between its quotation marks
13- Why did we get this output?
include ltiostreamgt using namespace std int
main() cout ltlt "14 27" ltlt endl
cout ltlt 14 27 ltlt endl return 0
- The second expression is an arithmetic
expression. - Remember that the value of an arithmetic
expression is the value of the formula.
14- Quick Note More than one type of expression
can be sent in the same cout command - must be separated by ltlt operator
include ltiostreamgt using namespace std int
main() cout ltlt 3 ltlt "" ltlt 4 ltlt "" ltlt 34
ltlt endl return 0
Integer
String
Integer
String
Arith. Exp.
15(No Transcript)
16- Integer Division
- what is the output of the following program?
include ltiostreamgt using namespace std int
main() cout ltlt 5.0 / 2.0 ltlt endl
cout ltlt 5 / 2 ltlt endl return 0
17- Integer Division
- 5 / 2 2 (the quotient)
- when two integers are divided, the result is an
integer - when a fraction (or remainder) occurs, it is
simply ignored - note that this ONLY APPLIES TO INTEGERS
- doubles are computed as expected
18Equation Result
14 / 7
-5 / 2
-7 / -3
19 / 20
19.0 / 20.0
2
-2
2
0
0.95
19- Modulus
- the remainder of the division operation
- in mathematics, if we divide 34 by 5, we get
Remainder
Quotient
r 4
- in C
- integer division gives us the quotient
- modulus gives us the remainder
20include ltiostreamgt using namespace std int
main() cout ltlt 34 / 5 ltlt endl
cout ltlt 34 5 ltlt endl return 0
21- Modulus
- has many uses in programming
- extracting digits
- determining a prime number
- hashing
- etc
- we will see modulus again when we reach
conditionals
22- Embedded Arithmetic Expressions
- recall the format of an arithmetic expression
- recall that a numeric expression is an expression
whose value is a number - since an arithmetic expression's value is a
number, we can use it in another arithmetic
expression
numeric expression
operator
numeric expression
3 4
23- Embedded Arithmetic Expressions
- recall the format of an arithmetic expression
- recall that a numeric expression is an expression
whose value is a number - since an arithmetic expression's value is a
number, we can use it in another arithmetic
expression
numeric expression
operator
numeric expression
3 4
numeric expression
operator
numeric expression
1 2 4
24- Embedded Arithmetic Expressions
- previous slide formally demonstrates that C can
handle expressions with more than one operator - important this is nothing special, just one
binary expression embedded in another - each binary expression is executed individually
cout ltlt 1 2 4 ltlt endl
1 2 4
- This expression is evaluated first
- Its value becomes the operand for
- the second expression.
25- Order and Precedence
- there are two possible orders of computation in
the previous expression
Order 1
Order 2
1 2 4
1 2 4
1 6
3 4
7
7
Which one does C use?
26- Order and Precedence
- there are two possible orders of computation in
the previous equation
Order 1
- Rule
- arithmetic expressions are evaluated left to
right when their operand is the same
1 2 4
3 4
7
27- Order and Precedence
- the order of evaluation becomes important for the
and / operators
Order 1
Order 2
1 - 2 - 4
1 - 2 - 4
1 - -2
-1 - 4
-5
3
28- Order and Precedence
- what about when the operators are not the same?
Order 1
Order 2
1 2 4
1 2 4
1 8
3 4
12
9
Which one does C use?
29- Order and Precedence
- what about when the operators are not the same?
Order 2
RULE When different operands are
used, precedence rules take over. , /, are
evaluated before any , - operations
1 2 4
1 8
9
30- Order and Precedence
- what about when the operators have the same
precedence?
Order 1
Order 2
1 - 2 4
1 - 2 4
1 - 6
-1 4
3
-5
Which one does C use?
31- Order and Precedence
- what about when the operators have the same
precedence?
Order 1
RULE When two arithmetic operands have the same
precendence, they are evaluated left to right.
1 - 2 4
-1 4
3
32- Order and Precedence
- what happens if I want the addition performed
first? - that is, parentheses override any precedence rule
1 2 4
(1 2) 4
33Equation Result
3 4 5
(3 4) 5
8 / 2 4
8 / (2 4)
19 / 20 200
23
35
16
1
0
34- Mixed Mode Expressions
- when two integers are used in an arith. exp., the
value of the operation is an integer as well - 5 2 7
- when two doubles are used in an arith. exp., the
value of the operation is a double as well - 5.0 2.0 7.0
- what happens when an integer and a double are
used in an operation? - 5.0 2 ????? 7 or 7.0?
35- Rules for Mixed Mode Arithmetic (text)
- if the operator has the same types, then the
value of the expression has the same type - if one of the operands is a floating point number
and the other an integer, then the integer is
promoted to a floating point number. The value
of the expression is a floating point number - the precedence rules from before still apply
36- Evaluating mixed-mode expressions
- the rule is applied for each operation
individually - the type of the value of each expression must be
remembered - order of operations usual precedence and left to
right rules
37Equation Result
3 / 2 5.5
15.6 / 2 5
4 5 / 2.0
4 3 7 / 5 25.5
4 3 / 5 3
6.5
12.8
6.5
-12.5
7