CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February...
-
date post
22-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February...
![Page 1: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/1.jpg)
CSI 101 Elements of ComputingSpring 2009
Lecture #5
Designing with Pseudocode
Wednesday, February 4th, 2009
![Page 2: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/2.jpg)
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
![Page 3: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/3.jpg)
3
Design Needs
• Inexperienced programmers needed more guidance
• Insufficient to review– Lack of detail made it too difficult to gauge
correctness and efficiency
![Page 4: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/4.jpg)
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
![Page 5: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/5.jpg)
5
Pseudocode constructs
• Execution block
• Input and Output
• Operations
• Decision
• Looping
• Branching
![Page 6: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/6.jpg)
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
![Page 7: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/7.jpg)
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
![Page 8: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/8.jpg)
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]
![Page 9: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/9.jpg)
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
![Page 10: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/10.jpg)
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)
![Page 11: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/11.jpg)
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
![Page 12: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/12.jpg)
12
IF statement forms
• Minimum: – IF condition THEN action
• Provide alternative– IF condition THEN action ELSE alternative
• Multiple checksIF condition THEN actionELSEIF new-condition THEN new-actionELSE alternativeENDIF
![Page 13: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/13.jpg)
13
Decision Examples
• Check for higher value:IF A >= B THEN High = AELSE High = BENDIF
• Equal special case:IF A > B THEN PRINT “A Higher”ELSEIF A < B THEN PRINT “B Higher”ELSE PRINT “Equal”ENDIF
![Page 14: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/14.jpg)
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
![Page 15: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/15.jpg)
15
DO WHILE Example
• Add first ten values:SET Count = 0SET Sum = 0DO WHILE(Count< 10)
READ Value FROM fileSum = Sum + ValueCount = Count + 1
ENDWHILE
![Page 16: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/16.jpg)
16
DO UNTIL Example
• Read items from file:DO
READ Element FROM file
CALL ProcessElement(Element)
UNTIL(EOF)
ENDDO
![Page 17: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/17.jpg)
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
![Page 18: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/18.jpg)
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
![Page 19: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/19.jpg)
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
![Page 20: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/20.jpg)
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
![Page 21: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/21.jpg)
21
Solution, cont
Count = Count + 1
IF valid = False THEN PRINT “Invalid login information. Try again”
ENDIF
UNTIL(valid OR Count = 3)
ENDDO
![Page 22: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/22.jpg)
22
Solution, concluded
IF valid = False THEN
BEGIN Terminate
PRINT “Invalid login attempt”
Terminate user
END Terminate
ENDIF
END login
![Page 23: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/23.jpg)
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
![Page 24: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/24.jpg)
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
![Page 25: CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d785503460f94a5ad6a/html5/thumbnails/25.jpg)
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