Arithmetic Operators Arithmetic Operations with Variables CMSC 104
Arithmetic Operators Arithmetic Operations with Variables CMSC 104 Fall 1999: Peter Olsen Lecture 13: 1
Arithmetic Operators Name Operator Addition num 2 Subtraction Multiplication Division CMSC 104 Modulus Fall 1999: Peter Olsen Example + num 1 + * / % initial - spent fathoms * 6 sum / count m%n Lecture 13: 2
Arithmetic Operators Infix Notation h Operator is between the operands y = a + b * (c + d) h Resembles standard mathematical notation h Requires parentheses to show order. CMSC 104 Fall 1999: Peter Olsen Lecture 13: 3
Modulus % The expression m % n yields the remainder after m is divided by n. l Modulus is an integer operation. - Both operands MUST be integers. l Examples : 17 % 5 = 2 6%3 = 0 9%2 = 1 5%8 = 5 CMSC 104 l Fall 1999: Peter Olsen Lecture 13: 4
Modulus % 11 % 4: 11 = 2 * 4 + 3 11 % 4 = 3 31% 27 = ? 16 % 9 = ? 32 % 5 = ? 64 % 21 = ? 128 % 31 = ? CMSC 104 Fall 1999: Peter Olsen Lecture 13: 5
Uses of Modulus l Even and Odd 5 % 2 = 1 odd 4 % 2 = 0 even Take the modulus by 2 of any integer, a result of 1 means the number was odd, a result of 0 means the number was even. l The Euclidean GCD algorithm CMSC 104 Fall 1999: Peter Olsen Lecture 13: 6
Integer Division If both operands of a division expression are integers, you get an integer answer. The fractional portion is thrown away, not rounded! l Examples : 17 / 5 = 3 4 / 3 = 1 35 / 9 = 8 3 / 4 = 0 (!) l CMSC 104 Fall 1999: Peter Olsen Lecture 13: 7
Mixed Division l Division where one operand is a floating point number will produce a floating point result. o Integer is “promoted” to a float l Division where one operand is a double will produce a double result. o Integer is “promoted” to a double. l General rule: promotion to “highest” type CMSC 104 Fall 1999: Peter Olsen Lecture 13: 8
Division By Zero Division by zero is mathematically undefined. l Division by zero will cause a fatal error. --- your program will terminate immediately and give an error message. l Often caused by forgetting to initialize variables (or using zero result of integer division). l CMSC 104 Fall 1999: Peter Olsen Lecture 13: 9
Non-Fatal Errors l Non-fatal errors will not cause program termination, but they may produce incorrect results. CMSC 104 Fall 1999: Peter Olsen Lecture 13: 10
Precedence and Associativity Many ways to interpret arithmetic statements 5 * 7 + 3 * 2 = 41 ? 76 ? 65 ? 100 l Infix notation is inherently ambiguous, need rules of precedence and associativity l CMSC 104 Fall 1999: Peter Olsen Lecture 13: 11
Arithmetic Operators Rules of Operator Precedence Operator(s) () Precedence & Associativity Evaluated first. If nested innermost first. If on same level - left to right. * / % Evaluated second. If several, evaluated left to right + - Evaluated third. If there are several, evaluated left to right. = CMSC 104 Fall 1999: Peter Olsen Evaluated last, right to left. Lecture 13: 12
Evaluation of expressions a*b+c Would multiply a * b first, then add c to the result. If we really want the sum of b and c to be multiplied by a, use parenthesis to force the evaluation to be done in the order we want. a*(b+c) CMSC 104 Fall 1999: Peter Olsen Lecture 13: 13
Practice With Evaluating Expressions Given integer variables a, b, c, d, & e, where a = 1, b = 2, c = 3, d = 4 Evaluate the following expressions : a+b-c+d a*b/c 1+a*b%c a+d%b-c e=d+c/b-a CMSC 104 Fall 1999: Peter Olsen Lecture 13: 14
Good Programming Practices You will be given a handout of suggested C coding standards and Indentation Styles l You will be expected to conform to these standards for all programming projects in this class. l Pick one of the two indentation styles shown and use it consistently. l CMSC 104 Fall 1999: Peter Olsen Lecture 13: 15
A Sample Project We are going to write a program in class that finds the volume and surface area of a box. l Write the Algorithm l Use the algorithm as a guide for writing the code. l Project : CMSC 104 Fall 1999: Peter Olsen Lecture 13: 16
An algorithm for writing code Write the algorithm Write the code using emacs Try to compile the code While there are still syntax errors Fix errors Try to compile the code Test the program Fix any semantic errors Compile the code CMSC 104 Test Fall 1999: Peter Olsenthe program Lecture 13: 17
- Slides: 17