01 - Introduction to Algorithms and Programs

01 - Introduction to Algorithms and Programs - Engineering...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Engineering 101 What will you get from this course? You will learn how to "think like an engineer" You will learn about algorithms and how to design/implement them You will learn programming in C++ and MATLAB You will have fun! Introduction to Algorithms and Programs Quote of the Day I hear and I forget, I see and I remember, I do and I understand. - Confucius What is an algorithm? A list of instructions for accomplishing a task that may be executed by a mechanism. Algorithm From our text Examples of "famous" algorithms Web search: (Page Rank) Recommendations: (you want to buy...) Mp3 encoding: (and decoding) A list of instructions that, when executed, transform information from input to output. The instructions are a finite set of steps that can be executed, in a definite order, by a deterministic mechanism. When these steps are actually executed, the execution must terminate after a finite time. time. Key Point 1 Other uses of algorithms Air Traffic Control Other uses of algorithms Circuit Design Magnetic Resonance Imaging Materials Analysis Computer Numerical Control Air Pollution Modeling Other uses of algorithms 1. Question 1 Is it an algorithm? 2. 3. 4. 5. Chemical Analysis Request a value for A Request a value for B Request a value for C Assign BB-4AC to D B Return D No, it is not an algorithm Yes, it returns the solution to a quadratic equation Ax2+Bx+C=0. Yes, it returns the discriminant for a quadratic equation Ax2+Bx+C=0. a) b) Process Control Simulation c) Question 2 Is it an algorithm? 1. 2. 3. 4. Question 3 Is it an algorithm? 1. 2. 3. 4. 5. 6. Request a value for A While A>0 Assign A/2 to A Return A No, it is not an algorithm Yes, it returns a value of one-half A. oneYes, it returns the smallest even divisor of A. Request a value for A Request a value for B While B>A Request a new value for B Assign B/A to C Return C a) b) c) a) b) c) d) No, it is not an algorithm Yes, it returns a value <= 1. 1. Yes, it returns a value >= 1. 1. Yes, it returns 1 2 Skills you will develop when mastering algorithms Logical thinking Ability to analyze a process Capability to identify and troubleshoot problems Patience and persistence Administrative Business Teaching Staff Textbooks Intro to Engineering Programming James Holloway, 2004 ISBN: 0471202150 MATLAB: An Introduction with Applications, 3rd Ed Amos Gilat, 2007 Gilat, ISBN: 0470108770 http://hecda.wiley.com/WileyCDA/Section/id http://hecda.wiley.com/WileyCDA/Section/id324363.html The most useful tool for this class... CTools! http://ctools.umich.edu/ Contains useful information and discussions where you can ask questions of the instructor, GSI's, and other students. Important notes about CTools More details in the syllabus You are responsible for checking CTools regularly and obtaining assignments Lectures will be posted on Ctools along with a video capture. Questions regarding projects, exams and technical issues should be posted to the CTools Forum, Forum, not sent by email 3 Grading More details in the syllabus Project Submission More details in the syllabus Turn in to your Engin101 AFS directory Due at 9pm on the due date. Shortly after 9pm your account will be altered to prevent revisions. Altering your code after 9pm is considered an honor code violation. No late submissions unless there is a valid and documented problem. Work must be submitted to the proper directory with the correct name. Instructions will accompany each assignment. Improperly submitted work will not be counted. Collaboration / Cheating More details in the syllabus Re-grade requests ReRe-grade requests must be submitted within one week Reafter the project or exam is returned in lab. DO NOT write anything on the original graded project or exam. A more detailed description of how regrade requests re pertain to projects will be discussed in your individual lab sections. All projects and exams are to be done individually Form groups to work on concepts. However, actual coding must be done by yourself. Beware the auto-grader-cheat-detecting-magic-machine! auto-grader-cheat-detecting-magic- Exams More info on CTools How are algorithms invoked? Every algorithm has a caller. An algorithm is usually called by another algorithm. Caller Algorithm A 4 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 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 Algorithm A y Example: Remainder Algorithm 1. 2. 3. 4. 5. 6. 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 Algorithm A y 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 Algorithm A y Example: Remainder Algorithm 1. 2. 3. 4. 5. 6. 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 Algorithm A y 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 Algorithm A y 5 Example: Remainder Algorithm 1. 2. 3. 4. 5. 6. 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 Algorithm A y Request a value for x Request a value for y While x y do Assign x-y to x Return x End Caller x Algorithm A output 6 y How is data sent to/from the caller? Pass-byPass-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, pass-bythis is called a return. The caller has the option to ignore this kind of data. 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 Pass-byPass-by-reference gives direct access to the data in the caller's environment. The caller cannot ignore pass-by-reference data send to it pass-bysince the data was directly changed. Call-by-reference Caller a 20 b 7 x Algorithm A y 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 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 Call-by-reference Caller a 20 b 7 x Algorithm A y 6 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 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 13 b 7 x y Algorithm A Call-by-reference Caller a b 6 7 x Algorithm A y 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-value Caller a 20 6 7 x Algorithm A y This output could be ignored. Call-by-reference can cause changes in the caller aside from returning output explicitly. b 7 6 Call-by-reference Caller a b 6 7 output 6 x Algorithm A y Call-by-reference Caller a b 6 7 6 x Algorithm A y This output cannot be ignored. What does the algorithm do? Mystery Algorithm 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 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 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. 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 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 7 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 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 a b c 16 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 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 a b c 16 12 12 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 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 a b c 16 12 12 remainder(a, c) remainder(b, c) 8 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 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) a b c 16 12 12 remainder(16, 12) 4 remainder(12, 12) remainder(12, 12) 0 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 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) a b c 16 12 11 4 0 remainder(12, 12) 0 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 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) a b c 16 12 10 remainder(16, 10) 5 0 remainder(12, 11) 6 0 remainder(12, 10) 1 0 2 0 9 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 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) a b c 16 12 8 remainder(16, 8) 7 0 remainder(12, 9) 0 remainder(12, 8) 3 0 4 0 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 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) a b c 16 12 6 remainder(16, 6) 2 0 remainder(12, 7) 4 0 remainder(12, 6) 5 0 0 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 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) a b c 16 12 4 remainder(16, 4) 1 0 remainder(12, 5) 0 remainder(12, 4) 2 0 0 10 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 All Algorithms Utilize 3 Primary Tools Sequence Selection Iteration a b c 16 12 4 remainder(16, 4) 0 remainder(12, 4) output 4 0 All Algorithms Utilize 3 Primary Tools Sequence Selection Iteration 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 All Algorithms Utilize 3 Primary Tools Sequence Selection Iteration 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. All Algorithms Utilize 3 Primary Tools Sequence Selection Iteration 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 All Algorithms Utilize 3 Primary Tools Sequence Selection Iteration 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. Repetitive execution of a group of instructions as long as some condition is met. 11 Reading A C++ Program: ColorColor-Coded! Sequence Selection Iteration 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 What Tool(s) Does this Algorithm Use? 1. 2. 3. 4. 5. Request a value for A Request a value for B If AB Return B A 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 Use? 1. 2. 3. 4. 5. 6. 7. Tips for success Attend lecture and lab regularly Get help when you need it Make use of office hours Sleep/eat regularly Be patient and persistent Request a value for A Assign 1 to B While A > 1 Assign A*B to B Assign A-1 to A AReturn B End This algorithm utilizes: a) Only Sequence b) Sequence and Selection c) Sequence and Iteration d) Sequence, Selection and Iteration Next Lecture C++ Basics and Input/Output 12 ...
View Full Document

Ask a homework question - tutors are online