CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
CS107 Introduction to Computer Science Lecture 2.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
PHYS 2020 Pseudocode. Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear.
Repetition Control Structures
Repetition control structures
ITEC113 Algorithms and Programming Techniques
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CSI 101 Elements of Computing Fall 2009 Lecture #4 Using Flowcharts Monday February 2nd, 2009.
Program Design and Development
Chapter 5: Loops and Files.
CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009.
Computer Science 1620 Programming & Problem Solving.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Pseudocode.
Chapter 2: Algorithm Discovery and Design
Pseudocode.
Adapted from slides by Marie desJardins
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
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.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ITEC113 Algorithms and Programming Techniques
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Loops and Files. 5.1 The Increment and Decrement Operators.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Algorithms and Pseudocode
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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
REPETITION CONTROL STRUCTURE
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Python: Control Structures
PROGRAM CONTROL STRUCTURE
Introduction To Flowcharting
Scripts & Functions Scripts and functions are contained in .m-files
Lecture 07 More Repetition Richard Gesick.
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
Starter Write a program that asks the user if it is raining today.
Lecture 4B More Repetition Richard Gesick
Iteration: Beyond the Basic PERFORM
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009

2 Design Deficiencies Many design methods missing key constructs: –No inherent looping or recursion ability Looping is the process of repeating the same instructions multiple times in a row –Limited ability to define where data comes from

3 Design Needs Inexperienced programmers needed more guidance Insufficient to review –Lack of detail made it too difficult to gauge correctness and efficiency

4 New Design Called “pseudocode” –Literally means “Fake code” Uses verbal descriptions and code-like structures –Code used is based on Basic, which evolved into Visual Basic Pseudocode often used in addition to other design methods –Pseudocode is an example of Low-level design

5 Pseudocode constructs Execution block Input and Output Operations Decision Looping Branching

6 Execution Block BEGIN to start END to conclude Used to start a pseudocode application, as well as separate repeated or recursion blocks Each BEGIN must have a corresponding END

7 Execution Block Example BEGIN MyProgram : (These are ellipses. They denote that : statements exist, but I am not listing them) BEGIN RepeatBlock : END RepeatBlock : END MyProgram

8 Input and Output READ … [FROM source] –Can read directly into variables –If you need to specify source, use the optional FROM keyword WRITE … [TO location]

9 I/O Examples READ Num1, Num2 FROM keyboard WRITE Total TO File “Out.txt” WRITE Pay as Format Currency –This last version starts blurring the line between design and code, as you will see later in the course –Best to keep pseudocode DESCRIPTIVE, but not just like code

10 Operations SET – Used to put a value into a variable –Not used so much anymore –Mostly use equation sign as equation CALL – Invoke a stored routine –A stored routine is one that has been previously created and stored in the system Examples: –SET Ave = Sum/Count –CALL 3Sort(High, Med, Low)

11 Decision IF statement Condition must be stated to have only TRUE or FALSE as the answer Must have statement or statements to run if condition is TRUE Must close with ENDIF

12 IF statement forms Minimum: –IF condition THEN action Provide alternative –IF condition THEN action ELSE alternative Multiple checks IF condition THEN action ELSEIF new-condition THEN new-action ELSE alternative ENDIF

13 Decision Examples Check for higher value: IF A >= B THEN High = A ELSE High = B ENDIF Equal special case: IF A > B THEN PRINT “A Higher” ELSEIF A < B THEN PRINT “B Higher” ELSE PRINT “Equal” ENDIF

14 Looping DO Statement Two types: –DO WHILE(condition) … ENDWHILE –DO … UNTIL(condition) ENDDO WHILE tests condition before performing action(s) between DO and ENDWHILE UNTIL performs actions then tests –Guarantees that statements run at least once

15 DO WHILE Example Add first ten values: SET Count = 0 SET Sum = 0 DO WHILE(Count< 10) READ Value FROM file Sum = Sum + Value Count = Count + 1 ENDWHILE

16 DO UNTIL Example Read items from file: DO READ Element FROM file CALL ProcessElement(Element) UNTIL(EOF) ENDDO

17 Branching ON condition GOTO label Most often used for Error Handling: ON Error GOTO Cleanup : Cleanup: READ Error type from Error structure Note Cleanup: is a label. Labels are immediately followed by a colon

18 Pseudocode Example Let’s revisit the user login process This time, since we have a loop structure, we can give the user 3 chances We’ll use functions to check for valid userid and password

19 Algorithm Initialize count and validity flag Loop 3 times or until valid: –Get userid and password –Check for userid existence –Check for valid password –Both OK, set flag –Otherwise, ask user to try again

20 Solution BEGIN login SET Count = 0, valid = False DO READ userid, password FROM user exists = UserExists(userid) IF exists THEN valid = CheckPwd(userid, password) ENDIF

21 Solution, cont Count = Count + 1 IF valid = False THEN PRINT “Invalid login information. Try again” ENDIF UNTIL(valid OR Count = 3) ENDDO

22 Solution, concluded IF valid = False THEN BEGIN Terminate PRINT “Invalid login attempt” Terminate user END Terminate ENDIF END login

23 Another Example Let’s design the Bubble Sort algorithm discussed in Lecture #3 Takes a list of numbers, compares two at a time, and switches their positions if necessary Loops through list again if a switch was made

24 Bubble Sort design BEGIN BubbleSort SET switched to False Loop: Item = 1 DO If Item > Item(+1) Then switch Items SET switched to True End If

25 Bubble Sort Design, cont Increase Item by 1 UNTIL all items have been compared to its neighbor ENDDO ON (switched set to True) GOTO Loop END BubbleSort