Arithmetic Expressions
- how to write arithmetic expressions on type double and int data
- arithmetic expression consists of operators and operands
- arithmetic operators: +, -, *, /, % (remainder, used only with int type)
- operands can be constants, variables, or other arithmetic expressions
- examples of arithmetic expressions:
3 + 6
6 - 7 * 2
x + 3 * y
- result of arithmetic expression is often stored in a variable of int or double type through an assignment statement:
x = 3 * y;
Division Operator (x / y)
- there is an important difference whether operands are int or double types
o for int type, / computes the integral part of the division
7 / 2 equals 3, not 3.5
o for double type, / represents the standard division
7.0 / 2.0 equals 3.5
o Note: division with 0 is undefined (causing run-time error)
Remainder Operator (x % y)
- only applicable on int types
- produces remainder of a division
o 7 % 2 equals 1
o 23 / 6 equals 5
- useful equation for int variables m and n:
o m = (m/n)*n + m%n
Data Type of an Expression
- x + y is int if both x and y are int
- x + y is double is either x or y are double (this includes mixed type expressions)
- expressions that have operands of both type int and double are allowed and are called mixed-type expressions
Mixed-Type Assignment Statements
- how assignment works with mixed-type expressions:
o expression on right is evaluated according to the mixed-type rules
o the result is stored in the variable on the left
e.g. y = m/n;
if y is int type, only integral part of division m/n is saved
e.g. int k, m, n;
double p, x ;
m = 3;
n = 2 * 1.2;
p = 2;
x = m / n;
x = m / p;
k = m / n;
k = x / p;
Type Conversion Using Casts
o if m and n are int type (e.g. m = 5, n = 2), the value of y after the statement y = m/n is 2, even if y is of double type!
o To prevent such behavior, type casts are used:
y = (double) m / (double) n; /* y will be 2.5 */
y = (double) m / n; /* y will be 2 since division has precedence over type cast */
Unary Operators
- + and - can be unary or binary operators
o x = -y; /* unary */
o z = y - x; /* binary */
Rules for Evaluating Complex Expressions:
- Parenthesis rule (executed from the inside out)
- Operator precedence:
o unary +, -
o *, /, %
o binary +, -
- Associativity: evaluate operators of the same precedence from left to right
o x * y * z + a / b – c * d EQUALS (x*y*z) + (a/b) – (c*d)
Writing mathematical formulas in C
- look at examples from Table 2.10 in the textbook
Formatting Numbers in Program Output
- printf provides capability to print neat outputs to the screen
- it can be achieved by adding numbers after placeholdes,
o %5d means that at least 5 spaces will be taken to display an integer value
o %6.2f means that two digits are reserved for the fractional part, and at least 6 total spaces will be used to print a real-vallued variable
- read section 2.6 from the textbook
- pay attention to Tables 2.11, 2.12, 2.13
Common Programing Errors
- Syntax errors – compiler cannot translate it. Typical errors:
o missing semicolon
o undeclared variable
o comment not closed
- Run-time errors – program directs computer to perform illegal operation. Example:
o dividing by zero
- Undetected errors – program gives incorrect result
- Logic errors – caused by faulty algorithms. Example:
o kms = 1.235*miles;