420 likes | 472 Views
Algorithms for Integer Arithmetic. Addition Algorithm. Three Steps to Adding Two Integers. STEP 1. Do they have the same sign ? (both positive or both negative) or Do they have opposite signs ? (one positive and the other negative). Same Signs. STEP 2. Enter the common sign:.
E N D
Algorithms for Integer Arithmetic Addition Algorithm
Three Steps to Adding Two Integers STEP 1. Do they have the samesign? (both positive or both negative) or Do they have oppositesigns? (one positive and the other negative)
Same Signs STEP 2. Enter the common sign: STEP 3. Now enter the sum of the absolute values: (add the magnitudes)
Opposite Signs STEP 2. Enter the sign of the number having the larger magnitude: STEP 3. Enter the difference of the larger magnitude and the smaller:
Step 1 Same or opposite signs? They have opposite signs.
Step 2 Enter the sign of the number having the larger magnitude: |-34| = 34 > 12 = |12|
Step 3 Subtract the smaller magnitude from the larger and enter the result.
Step 1 Same or opposite signs? They have opposite signs.
Step 2 Enter the sign of the number having the larger magnitude: |29| = 29 > 14 = |-14|
Step 3 Subtract the smaller magnitude from the larger and enter the result.
Step 1 Same or opposite signs? They are both positive.
Step 2 The sign is positive.
Step 3 Add the magnitudes and enter the result.
Step 1 Same or opposite signs? They are both negative.
Step 2 The sign is negative.
Step 3 Add the magnitudes and enter the result.
Adding Many Integers 1. Add all the positive numbers and add all the negative numbers: 2. Add the two signed numbers:
Work it out! Rigid use of an algorithm is not always best.
Moral Look before you leap! Many problems can be worked in your head if you do things in the right order.