100 likes | 208 Views
Applied Algorithms. Lecture #6 Arithmetic and Algebra. Quiz. We have a 10 minute quiz today on the reading – Chapter 5. We will continue to have quizzes until the class gets an average score high enough to convince me that people are doing the reading. a. b. c. d. e. f. g. h. i. j.
E N D
Applied Algorithms Lecture #6 Arithmetic and Algebra
Quiz • We have a 10 minute quiz today on the reading – Chapter 5. • We will continue to have quizzes until the class gets an average score high enough to convince me that people are doing the reading.
a b c d e f g h i j k l m n o p q r s t u v w x y z n t x Crypt Kicker the quick brown fox … xnm cecob lrtzv ita … length table plain line
Make a substitution Bool test(int length, char line[]) { int i; char c; char target; if (strlen(line)!=length) return False; for (i=0; i<length; i++) { c = line[i]; ; target = plain[i]; ; if (table[c]=='?') table[c]=target; ; if (table[c]!=target) return False; } return True; } Exits as soon as we can’t extend the subsitution
Vitos Family 5 9 20 21 44 68 80 • |x-5| + |x-9| + |x-20| + |x-21| + |x-44| + |x-68| + |x-80| • Suppose x is between 21 and 44 • (x-5)+ (x-9) + (x-20) + (x-21) + (44-x) + (68-x) + (80-x) • 4x – (5+9+20+21) + (44+68+80) -3x
Partial sums from left and right 5 9 20 21 44 68 80 5 14 34 55 99 167 247 247 242 233 213 192 148 80
In class Assignment • Using this idea • Goto the class web page and upload • VitosFamilySkeleton.c • Modify this program to use these facts • Use the sorting algorithm in <stdlib.h> #include <stdlib.h> void qsort(void *base, size_t nel, size_t width, int(*compare)(const void *,const void *));
Shoemaker don't think that a simple sort by duration within penalty descending works in all cases. For example duration penalty 30 9 30 9 1 8 is in the right order and gives, by my calculation, 30*9 + (30+30)*8 = 510, where putting the short duration first is: duration penalty 1 8 30 9 30 9 by my calculation, 1*9 + (1+30)*9 = 288.
In Class Problems • Primary Arithmetic 5.9.1 • Page 119 of the text • We will write this together as a class • Reverse and Add 5.92 • Page 120 of the text • Keeping in mind today’s lecture. • Pairs of two
Today’s Assignments Read for next time Chapter 6 of the text. pp 129-146 Be prepared to answer questions in class next Friday from the reading. Programming assignment • 5.9.8 Pairsumonius Numbers • Page 127 • Write a solution Submit your solution (until you get it right) Hand in both your program, and the judge output. Those who volunteer to discuss their program get class participation points. Email me solutions before noon on Friday, May 6.