Agenda 1 1 1 1 2 2 2 2 • Non DP stuff: – dUFLP/dHUNG Naming Conventions (xBase) – Binary Arithmetic Review Dynamic Programming II CPSC490 Lecture 11.3 • Today we’ll be looking at these DP topics... – Memoization Friday 2006 March 17 – The Coin Changer Problem David Freedman and Igor Ostrovsky – Hamiltonian Paths and Cycles – The Travelling Salesman Problem CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 dUFLP/dHUNG Variable Conventions 3 3 3 3 4 4 4 4 • Standard for “xBase” family of languages (dBase, Clipper, Paradox, Fox Pro, etc.) – dUFLP = dBase Users Functional Library Project, the dBase (later xBase standards group) dUFLP/dHUNG – dHUNG = “Simplified Hungarian,” also known as “Short Hungarian” and “Hungarian without the Arian” • Hallmark feature: The first letter of each variable denotes it’s type. Naming Conventions • Key differences between dHUNG and Hungarian notation: – type codes correspond to dBase standard type names – only 1 letter is used for each type – types don’t chain (an a rray of i n tegers is just “a” not “an”) • The original goal was that this would be a cross-language standard. • bastardizations of the standard (less standard standards, like the one you’re about to see), sometimes find their way into functional/OOP languages. CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II
Scope Prefix xBase Types Applied to C/C++/Java/JavaScript/Basic... 5 5 5 5 6 6 6 6 a a rray (any type/dimensions) l l ogical (boolean) Another prefix, separated by an underscore, denotes scope for all int a theArray[]; int l theFlag = 0; //pre C99 ! boolean[][] a theArray; bool l theFlag = false; non-local variables (variables declared inside a function or method) var a theArray = { false, 12, { 0, 0 }, "abc" }; boolean l theFlag = false; Dim l theFlag as Integer b code b lock n i n teger /* no Basic, C/C++ or Java equivalent! m, g: private/public Arguably used in Java for Runnable */ byte n int = 123; int n int = 123; long n int = 123; var b theFunc = function() { return "OK"; } • used for both object fields and “file level” variables (top level in an include/header file) /*use only 1 of these within a single program!*/ class TheClass { private int m_ nvar ; public int g_nvar = ... Integer n int = new Integer(123); c string ( c haracter field) Integer N int = new Integer(123); char[80] c theString = "abc"; //pre C99 ! var m_sVERSION = "1.2"; string c theString = "abc"; o o bject String c theString = "abc"; function TheClass() { this. m_ nvar = 0; this. g_nvar = ... char c theChar = 'a'; Scanner o scanner = new Scanner(System. in ); p: parameter d d ate v v ariant (where the type matters) /*use only 1 of these within a single program!*/ • parameter passed to function. /* no C/C++ or Java equivalent! */ Date d now = new Date(); switch( v theVar) { case "string": case "number":... public static void main(String[] p_aargs) { ... String d now = "19991231"; Dim v theVar As Variant; long d now = System. currentTimeMillis (); function doSomething( p_ nint, p_ cstr) { ... x variant (where the type is irrelevant) f f loating point /* no Java equivalent! */ • in JavaScript, P_ is used for constructor parameters to avoid name conflicts with method float f theFloat = 12.34; double f theFloat = 12.34; #define IFF(lcond, x a, x b) ( (lcond) ? ( x a) : ( x b) ) var f amt = 12.34; Dim f amt As Single Sub Iff(p_lcond As Integer, p_ x a As Any, p_ x b As Any) parameters. function TheClass( P_ nparam, P_ cparam) { /*use only 1 of these within a single program!*/ y currenc y (integer cents) Double f theFloat = new Double(12.34); this. p_nvar = P_nparam; Double F theFloat = new Double(12.34); this. method = function(p_nparam, p_cparam) { ... long y balance = 1234; //$12.34 CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 Scope Prefix (cont’d) Why would we ever want to do this? 7 7 7 7 8 8 8 8 • Makes it easier to understand code in implicitly typed languages (esp. scripting i, o: input/output parameters languages) like JavaScript or VBScript • used instead of “p” for languages where functions return values by changing parameters (usually database languages like ABAP or SQL). • Simplifies translating code between • also used for recordset fields for stored procedures which take table parameters languages • i = input parameter, which will be read and not be modified. Can pass a variable or a literal. • o = output parameter, which will not be read but will be modified. Must pass a variable, • Simplifies collaboration on multilingual cannot pass a literal. projects (and reduces the amount of documentation required !) CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II
Positive Number Systems in Decimal 9 9 9 9 10 10 10 10 How are numbers in other systems decoded to decimal? binary (base 2) hexadecimal (base 16) decimal (base 10) Binary Arithmetic Review non-decimal “digits”: ������ ������ ������ ������ ������ ������ Notice: binary 10 is decimal 2. hexadecimal 10 is decimal 16. Why is this important? (Look over at the decimal chart) CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 Positive Number Systems in Binary Negative Binary Integers 11 11 11 11 12 12 12 12 • For signed integers (all integers in Java), the leftmost bit is called the How are numbers in encoded in binary? sign bit. It works exactly like all the other bits, except it is subtracted instead of added. This system is called complement numbers . decimal (base 01010) binary (base 010) • To change the sign of a number, flip every bit, then add 1 to the total • The first “computer” which worked with complement numbers was developed in 1640 by Blaise Pascal (1623-1662). non-binary digits: ��������� ��������� ��������� ��������� ��������� ��������� ��������� ��������� CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II David Freedman and Igor Ostrovsky Friday 2006 March 17 CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II CPSC490 11.3: Dynamic Programming II
Recommend
More recommend