lecture1 - COMP 250 Winter 2010 lecture 1 addition and...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP 250 Winter 2010 lecture 1 addition and multiplication January 6, 2010 Algorithms for addition and multiplication Lets try to remember your first experience with numbers, way back when you were a child in grade school. In grade 1, you learned how to count up to ten and to do basic arithmetic using your fingers. When doing so, you memorized sums of single digit numbers (4 + 7 = 11, 3 + 6 = 9, etc). Later on in about grade 4, you learned a method for adding multiple digit numbers, which was based on the single digit additions that you had memorized. For example, you were asked to compute things like: 2343 + 4519 ? The method that you learned was a sequence of computational steps, commonly called an algorithm . What was the algorithm ? Lets call the two numbers a1 and a2 and lets say they have N digits each. Then the two numbers can be represented as an array of single digit numbers a 1[ ] and a 2[ ]. We can define a variable carry and compute the result in an array r [ ]. You know how this works. You go column by column, adding the pair of single digit numbers in that column and adding the carry (0 or 1) from the previous column. We can write the algorithm in pseudocode 1 as follows: Algorithm 1 Addition (base 10): Add two N digit numbers a 1 and a 2 which are represented as arrays of digits carry = 0 for i = 0 to N 1 do r [ i ] ( a 1[ i ] + a 2[ i ] + carry ) % 10 carry ( a 1[ i ] + a 2[ i ] + carry ) / 10 end for r [ N ] carry The operator % is the mod operator. It computes the remainder of the division. The operator / ignores the remainder, i.e. it rounds down (often called the floor)....
View Full Document

Page1 / 3

lecture1 - COMP 250 Winter 2010 lecture 1 addition and...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online