2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park

Slides:



Advertisements
Similar presentations
CS101: Introduction to Computer programming
Advertisements

ITEC113 Algorithms and Programming Techniques
Computer Software & Software Development H&K Chapter 1 Instructor – Gokcen Cilingir Cpt S 121 (June 20, 2011) Washington State University.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Computer Science 1620 Programming & Problem Solving.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms and Pseudocode
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Quiz1 Question  Add Binary Numbers a) b) c) d) e) none
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CMSC 104, L041 Algorithms, Part 1 of 3 Topics Definition of an Algorithm Example: The Euclidean Algorithm Syntax versus Semantics Reading Sections 3.1.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
CMSC 104, Version 8/061L05Algorithms2.ppt Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode Control Structures Reading Section 3.1.
Introduction to Algorithms
CS1010 Programming Methodology
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Algorithms, Part 1 of 3 The First step in the programming process
Algorithms, Part 1 of 3 Topics Definition of an Algorithm
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
GC211Data Structure Lecture2 Sara Alhajjam.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms and Flowcharts
CHAPTER 5A Loop Structure
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Lecturer CS & IT Department UOS MBDIN
UMBC CMSC 104 – Section 01, Fall 2016
Programming Fundamentals
Computer Science 101 While Statement.
CS 240 – Lecture 11 Pseudocode.
2008/09/17: Lecture 4 CMSC 104, Section 0101 John Y. Park
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
The while Looping Structure
Relational and Logical Operators
2008/09/22: Lecture 5 CMSC 104, Section 0101 John Y. Park
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
3 Control Statements:.
The while Looping Structure
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Introduction to Algorithms and Programming
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithm and Ambiguity
Computing Fundamentals
Understanding Problems and how to Solve them by using Computers
Computer Science Core Concepts
Relational and Logical Operators
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Lecture 7 Algorithm Design & Implementation. All problems can be solved by employing any one of the following building blocks or their combinations 1.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Chapter 3: Selection Structures: Making Decisions
Basic Concepts of Algorithm
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
UMBC CMSC 104 – Section 01, Fall 2016
The while Looping Structure
Algorithms, Part 1 of 3 Topics Definition of an Algorithm
REPETITION Why Repetition?
Presentation transcript:

2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park More Algorithms 2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park [New lecture, some reused slides]

More Algorithms Topics Algorithms: Elements in Depth In-Class Project: Euclid’s Algorithm In-Class Project: Student Voluteerizer Reading None

Pseudocode: Control Structures Any problem can be solved using only three logical control structures: Sequence Selection Repetition

Sequence A series of steps or statements that are executed in the order they are written. Example: Display “Enter two numbers: “ Read <number1> Read <number2> <sum> = <number1> + <number2> Display “sum = “, <sum>

Sequence Some languages… Have line numbers Allow a set of sequenced steps to be grouped as a “statement block” Explicitly bracketed (e.g., with “begin…end” or {…} Allow “goto”s Evil-evil-evil!!!!!

Sequence Goto e.g.: <a> = 0 goto Label3: Label1: DISPLAY “Hello” DISPLAY “bye” Label2: <a> = <a> + 1 DISPLAY <a> goto Label1: DISPLAY “Never get here…” Label3: DISPLAY “Starting up” goto Label2:

Sequence You should try to avoid the “goto” like the plague! In this class, you will never need to use a “goto” DON’T USE GOTOs!!! Why did I even mention it? It is a vehicle for explaining what other control structures are doing, in a more logical manner

Selection Defines one or more courses of action depending on the evaluation of a condition. Synonyms: conditional, branching, decision Examples: If (condition is true) If (condition is true) do this do this End_if Else do that End_if

Selection More complex examples: DISPLAY “Enter number to invert” READ <my_num> If (<my_num> < 0) DISPLAY “Don’t like negative numbers” if (<my_num> < -999) DISPLAY “… but I guess you really do!” End_if Else_if (<my_num> == 0) <result> = 0 Else <result> = 1 / <my_num> End_if

Repetition Allows one or more statements to be repeated as long as a given condition is true. Synonyms: looping, iteration Example: While (condition is true) do this End_while

Repetition More complex example (with mistakes) DISPLAY “Enter number to compute factorial for” READ <my_num> While (<my_num> > 0) <factorial> = <factorial> * <my_num> <my_num> = <my_num> - 1 End_while DISPLAY “The factorial of”, <my_num>, “ is “, <factorial>

Repetition More complex example (with mistakes) DISPLAY “Enter number to compute factorial for” READ <my_num> <factorial> = 0 While (<my_num> > 0) <factorial> = <factorial> * <my_num> <my_num> = <my_num> - 1 End_while DISPLAY “The factorial of”, <my_num>, “ is “, <factorial>

Repetition More complex example (with mistakes) DISPLAY “Enter number to compute factorial for” READ <my_num> <factorial> = 0 <saved_my_num> = <my_num> While (<my_num> > 0) <factorial> = <factorial> * <my_num> <my_num> = <my_num> - 1 End_while <my_num> = <saved_my_num> DISPLAY “The factorial of”, <my_num>, “ is “, <factorial>

Pseudocode Style Any user prompts should appear exactly as you wish the programmer to code them. The destination of any output data should be stated, such as in “Display”, which implies the screen. Make the data items clear (e.g., surround them by < and > ) and give them descriptive names. Use formulas wherever possible for clarity and brevity. Use keywords (such as Read and While) and use them consistently. Accent them in some manner.

Pseudocode (con’t) [Review] Use indentation for clarity of logic. Avoid using code. Pseudocode should not be programming language-specific. Always keep in mind that you may not be the person translating your pseudocode into programming language code. It must, therefore, be unambiguous. You may make up your own pseudocode guidelines, but you MUST be consistent.

Euclid’s Algorithm Problem: Find the largest positive integer that divides evenly into two given positive integers (i.e., the greatest common divisor). Algorithm: Assign M and N the values of the larger and smaller of the two positive integers, respectively. Divide M by N and call the remainder R. If R is not 0, then assign M the value of N, assign N the value of R, and return to Step 2. Otherwise, the greatest common divisor is the value currently assigned to N.

Finding the GCD of 24 and 9 So, 3 is the GCD of 24 and 9. M N R 24 9 6 So, 3 is the GCD of 24 and 9.

Euclid’s Algorithm Tips about problem # 1 : The user should specify the two numbers to factor. She may enter them in any order (i.e., don't assume first is greater). You will need a new arithmetic operation for computing remainders: the '%' operator. E.g.: "24 % 7" equals "3" “-5 % 2” equals “-1”, and "18 % -8" equals "+2" "7 % 0" equals "the end of the world as we know it." The user may input one or both values as negative numbers. You must either: explicitly test for and reject negative numbers make sure your algorithm computes the correct answer with them

Student Voluteerizer Problem: Write a generic algorithm for helping call on student “volunteers” in a “fair” manner <obviously underspecified…>