Lecture+02 - Engineering 101 Engineering 101 Lecture 2...

This is the end of the preview. Sign up
to
access the rest of the document.

Unformatted text preview: Engineering 101 Engineering 101
Lecture 2 9/6/07
Algorithms and Programs Quote of the Day Quote of the Day he people may be made to follow a path of action ut they may not be made to understand it.
­ Confucius How are algorithms invoked? How are algorithms invoked? Every algorithm has a caller. An algorithm is usually called by another algorithm.
Caller Algorithm A How is data handled? How is data handled? Data is exchanged between the algorithm and its caller. Since our definition of an algorithm is that it transforms input to output there must be a way to accept input and provide output to the caller. Caller Data Data Data Algorithm A Example: Remainder Algorithm Example: Remainder Algorithm
1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x ≥ y do Assign x­y to x Return x End Caller x y Algorithm A Example: Remainder Algorithm Example: Remainder Algorithm
1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x ≥ y do Assign x­y to x Return x End Caller 20 x y Algorithm A Example: Remainder Algorithm Example: Remainder Algorithm
1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x ≥ y do Assign x­y to x Return x End Caller 20 7 x y Algorithm A Example: Remainder Algorithm Example: Remainder Algorithm
1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x ≥ y do Assign x­y to x Return x End Caller 13 7 x y Algorithm A Example: Remainder Algorithm Example: Remainder Algorithm
1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x ≥ y do Assign x­y to x Return x End Caller 6 7 x y Algorithm A Example: Remainder Algorithm Example: Remainder Algorithm
1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x ≥ y do Assign x­y to x Return x End Caller x y Algorithm A Example: Remainder Algorithm Example: Remainder Algorithm
1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x ≥ y do Assign x­y to x Return x End Caller x output 6 y Algorithm A How is data sent to/from the caller? How is data sent to/from the caller? Pass­by­value copies the data from the caller to a new location in the algorithm. When pass­by­value is used to send data back to the caller, this is called a return. The caller has the option to ignore this kind of data. Pass­by­reference gives direct access to the data in the caller’s environment. The caller cannot ignore pass­by­reference data send to it since the data was directly changed. Consider the Consider the Remainder Algorithm
Remainder
1: Request a value for x 2: Request a value for y 3: while x ≥ y do 4: assign x-y to x 5: return x 6: end Call-by-value Caller a 20 b 7 x Algorithm A y Call-by-reference Caller a 20 b 7 x Algorithm A y Consider the Consider the Remainder Algorithm
Remainder
1: Request a value for x 2: Request a value for y 3: while x ≥ y do 4: assign x-y to x 5: return x 6: end Call-by-value Caller a 20 b 7 20 x Algorithm A y Call-by-reference Caller a 20 b 7 x Algorithm A y Consider the Consider the Remainder Algorithm
Remainder
1: Request a value for x 2: Request a value for y 3: while x ≥ y do 4: assign x-y to x 5: return x 6: end Call-by-value Caller a 20 b 7 20 7 x y Algorithm A Call-by-reference Caller a 20 b 7 x Algorithm A y Consider the Consider the Remainder Algorithm
Remainder
1: Request a value for x 2: Request a value for y 3: while x ≥ y do 4: assign x-y to x 5: return x 6: end Call-by-value Caller a 20 b 7 13 7 x Algorithm A y Call-by-reference Caller a 13 b 7 x y Algorithm A Consider the Consider the Remainder Algorithm
Remainder
1: Request a value for x 2: Request a value for y 3: while x ≥ y do 4: assign x-y to x 5: return x 6: end Call-by-value Caller a 20 b 7 6 7 x Algorithm A y Call-by-reference Caller a b 6 7 x Algorithm A y Consider the Consider the Remainder Algorithm
Remainder
1: Request a value for x 2: Request a value for y 3: while x ≥ y do 4: assign x-y to x 5: return x 6: end Call-by-value Caller a 20 b 7 output 6 6 7 x Algorithm A y Call-by-reference Caller a b 6 7 output 6 x Algorithm A y Call-by-value Caller This output could be ignored. Call-by-reference can cause changes in the caller aside from returning output explicitly. a 20 b 7 6 6 7 x Algorithm A y Call-by-reference Caller a b 6 7 6 x Algorithm A y This output cannot be ignored. Mystery Algorithm What does the algorithm do? What does the algorithm do?
remainder(x, y) 1. Request a value for x 2. Request a value for y 3. while x ≥ y do 4. assign x ­ y to x 5. return x 6. end Request a value for a Request a value for b if a ≥ b then assign b to c if a < b then assign a to c while (remainder(a, c)≠0 OR remainder(b, c) ≠ 0) subtract one from c return c end a) Computes the Least Common Denominator of 2 fractions b) Computes the Largest Common Prime of 2 numbers c) Computes the Greatest Common Factor of 2 numbers Mystery Algorithm What does the algorithm do? What does the algorithm do?
remainder(x, y) 1. Request a value for x 2. Request a value for y 3. while x ≥ y do 4. assign x ­ y to x 5. return x 6. end Request a value for a Request a value for b if a ≥ b then assign b to c if a < b then assign a to c while (remainder(a, c)≠0 OR remainder(b, c) ≠ 0) subtract one from c return c end a) Computes the Least Common Denominator of 2 fractions b) Computes the Largest Common Prime of 2 numbers c) Computes the Greatest Common Factor of 2 numbers We can now build from our We can now build from our Remainder algorithm
Greatest Common Factor
Note: Writing remainder(a,b) means a is provided as the first value to the remainder algorithm and b as the second value. 1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 12 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 12 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 12 remainder(a, c) remainder(b, c) Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 12 remainder(16, 12) remainder(12, 12) Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 12 remainder(16, 12) 4
remainder(12, 12) 0 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 12 remainder(16, 12) 4≠ 0
remainder(12, 12) 0 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 11 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 11 remainder(16, 11) 5≠ 0
remainder(12, 11) 1≠ 0 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 10 remainder(16, 10) 6≠ 0
remainder(12, 10) 2≠ 0 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 9 remainder(16, 9) 7≠ 0
remainder(12, 9) 3≠ 0 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 8 remainder(16, 8) 0
remainder(12, 8) 4≠ 0 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 7 remainder(16, 7) 2≠ 0
remainder(12, 7) 5≠ 0 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 6 remainder(16, 6) 4≠ 0
remainder(12, 6) 0 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 5 remainder(16, 5) 1≠ 0
remainder(12, 5) 2≠ 0 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 4 remainder(16, 4) 0
remainder(12, 4) 0 Demo of Demo of “Greatest Common Factor”
1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end a b c 16 12 4 remainder(16, 4) 0
remainder(12, 4) output 4 0 News Flash! News Flash! Meet Apple’s new holiday line­up News Flash! News Flash! News Flash! News Flash! $79 $149 4GB $249 80GB $199 8GB $349 160GB $299 8GB $399 16GB What?!? $399 8GB News Flash! News Flash! Multi­touch interface 3.5 inch widescreen 802.11 b/g Wi­Fi with Safari 8GB or 16GB Flash Storage YouTube Wi­Fi iTunes Starbucks integration (lame?) No email or camera News Flash! News Flash! From the Associated Press on the iPhone price cut: “Apple stock dropped more than 5 percent after the price drop was announced, losing $7.40 to close at $136.76.” “People who bought the iPhone weeks or months ago must really be annoyed.” “This smacks a little of desperation, and it's very unlike Apple.” News Flash! News Flash!
Doh! News Flash! News Flash!
iPhone launched After iPhone announced oops? Hype/Excitement Builds The good news: Apple stock still doubled in one year… Sequence Selection Iteration All Algorithms Utilize All Algorithms Utilize 3 Primary Tools Sequence Selection Iteration All Algorithms Utilize All Algorithms Utilize 3 Primary Tools
Greatest Common Factor 1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end Sequence Selection Iteration All Algorithms Utilize All Algorithms Utilize 3 Primary Tools
Greatest Common Factor 1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end Commands follow in a specific linear order. Often this order is implicit. Algorithms invoke other algorithms. Sequence Selection Iteration All Algorithms Utilize All Algorithms Utilize 3 Primary Tools
Greatest Common Factor 1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end Selective execution of instructions based on a particular condition. Sequence Selection Iteration All Algorithms Utilize All Algorithms Utilize 3 Primary Tools
Greatest Common Factor 1: Request a value for a 2: Request a value for b 3: if a ≥ b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)≠0 OR remainder(b, c)≠0) 6: subtract one from c 7: return c 8: end Repetitive execution of a group of instructions as long as some condition is met. What Tool(s) Does this Algorithm What Tool(s) Does this Algorithm Use?
1. 2. 3. 4. 5. Request a value for A Request a value for B If A≥B Return B If A<B Return A End This algorithm utilizes: a) Only Sequence b) Sequence and Selection c) Sequence and Iteration d) Sequence, Selection and Iteration What Tool(s) Does this Algorithm What Tool(s) Does this Algorithm Use?
1. 2. 3. 4. 5. Request a value for A Request a value for B If A≥B Return B If A<B Return A End Finds the Min of 2 numbers This algorithm utilizes: a) Only Sequence a) Sequence and Selection a) Sequence and Iteration b) Sequence, Selection and Iteration What Tool(s) Does this Algorithm What Tool(s) Does this Algorithm Use?
1. 2. 3. 4. 5. 6. 7. Request a value for A Assign 1 to B While A > 1 Assign A*B to B Assign A­1 to A Return B End This algorithm utilizes: a) Only Sequence b) Sequence and Selection c) Sequence and Iteration d) Sequence, Selection and Iteration What Tool(s) Does this Algorithm What Tool(s) Does this Algorithm Use?
1. 2. 3. 4. 5. 6. 7. Request a value for A Assign 1 to B While A > 1 Assign A*B to B Assign A­1 to A Return B End Computes the factorial of A This algorithm utilizes: a) Only Sequence b) Sequence and Selection a) Sequence and Iteration a) Sequence, Selection and Iteration From Algorithms To Programs From Algorithms To Programs So far we have described a few algorithms in pseudo­code. Pseudo­code is fine for communicating algorithms between people but it is not precise enough to be used by a computer. Our Algorithms Must Be Written Our Algorithms Must Be Written in a Programming Language A programming language has precise syntax (grammar) and semantics (meaning). An algorithm in a programming language is called a program. To be used by the machine it must be translated into a native language specific to the computer’s CPU (central processing unit) called machine language. This translation is done by a compiler. Writing directly in machine language is taxing for humans because it is so rudimentary. Reading A C++ Program Reading A C++ Program
#include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program Much of this program is scaffolding. int main ( ) Scaffolding is only { important insomuch as it double x, y; holds the program together cin >> x; and provides context for y = x * 7; the C++ compiler. cout << y; We will consider this part return 0; of the program later. } #include <iostream> using namespace std; Reading A C++ Program The remaining part of the code consists of only 4 lines. The code reads in a number and then writes the result of multiplying the number by 7. #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } The first line of the body #include <iostream> declares two containers to hold using namespace std; numbers. The identifiers of these int main ( ) containers will be x and y. { It is important to declare every double x, y; cin >> x; identifier before it is used.
y = x * 7; cout << y; return 0; Reading A C++ Program: Declaration } Simple declarations are of the #include <iostream> form: using namespace std; simple_type identifiers;
int main ( ) { In this case the identifiers x double x, y; and y are declared to be of the cin >> x; type double. y = x * 7; For now we will consider two cout << y; simple types: double and int. return 0; } Reading A C++ Program: Declaration Reading A C++ Program: Declaration #include <iostream> A double is a number with a using namespace std; fractional part that can take on a very large range of magnitudes. An int is an integer number between about ­2 billion to 2 billion. Always choose the one closest to your meaning. int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Expressions The next 3 lines of the program are expressions. Expressions change and exchange data. The basic form of an expression is: expression; #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading AC++ Program: Expressions The first expression, cin >> x; will obtain input from the caller. int main ( ) The second expression, y { double x, y; = x * 7; multiples x by 7 cin >> x; and assigns the result to y. y = x * 7; The third expression, cout cout << y; << y; will output y to the return 0; caller. } #include <iostream> using namespace std; Reading A C++ Program: Semicolons
int main ( ) { Denote the end of a double x, y; declaration or expression. cin >> x; Critical for determining y = x * 7; order of execution. cout << y; return 0; The most common errors are } omitted semicolons. ; #include <iostream> using namespace std; Next Lecture Next Lecture C++ Basics – fun! ...
View Full
Document