100 likes | 246 Views
CMPUT 101 Lab #3. September 24, 2007 14:00 – 17:00. Guido: Adding numbers. Today work: A program that lets GvR add two 3-digit numbers using 10 based representation. Follow the step in the lab exercise 3. Step 1. Load worlds: lab_3_one_a.wld -- single digit without carry
E N D
CMPUT 101Lab #3 September 24, 2007 14:00 – 17:00
Guido: Adding numbers • Today work: • A program that lets GvR add two 3-digit numbers using 10 based representation. • Follow the step in the lab exercise 3
Step 1 • Load worlds: • lab_3_one_a.wld -- single digit without carry • lab_3_one_b.wld – single digit with carry
Step 2 & 3 • Load provided program: • lab_3a.gvr • Make sure the program works • Copy/paste may cause spacing problem • By executing it, the robot will be just turned off • Add your code in the space provided
Step 4 • Program a robot to add two single digit numbers. • Consider to use move_south, move_north, move_east, move_west, and add_digit_to_bag provided in the initial program • Define a new sub routine as: add_two_digits (Suggestion) • Don’t worry about the carry yet – GvR just keeps all beepers in his bag. • Test on the worlds in Step 1. Demo -- lab_3a_4.gvr
Step 5 • Think about the carry • Adding two numbers always give either 0 or 1 carry --- why ? • Consider in the largest possible • 999 + 999 = 1998 • the largest sum: 9 + 9 + 1 = 19 -- also 1 carry out
Step 6 • After adding two digits, GvR will have some number in the range 0…19 of beepers • Check for carry • try 9 times to drop a beeper • If there is still a beeper in a bag, then there is a carry out. • Otherwise, there is no carry out.
Step 7 • no_carry_out • The sum result should be the right place • No need to do extra steps • So, just move the robot to the next column (1E, 3N, 1E) • carry_out • Pick up all beepers. • Move south to drop 9 beepers in the garbage space. • Move 2 west and 1 north to drop 1 beeper (carry). • Move 2 east back to drop the all beepers in the bag. • Demo lab_3a_7.gvr
Step 8 • Repeat 2 more times to add 3 digits • Be sure to pick up the carry if there is one before computing the next sums. • Demo lab_3a_complete.gvr
Demo • Demo your final program on: • lab_3a.wld: 372 + 863 = 1235 • lab_3b.wld: 30 + 13 = 43 • lab_3c.wld: 823 + 192 = 1015