programming fundamentals lecture 3
TRANSCRIPT
![Page 1: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/1.jpg)
pseudocode and Flowchart
MCS 1Tariq Ali
![Page 2: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/2.jpg)
What is pseudocode?List of steps written in EnglishLike the instructions for a recipeMust be in the right sequence
Imagine saying “bake the cake” and then “mix it up”
![Page 3: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/3.jpg)
Sample PseudocodeTask: add two numbersPseudocode:
Start Get two numbers
Get first number Get second number
Add themPrint the answerEnd
![Page 4: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/4.jpg)
Invitation to Computer Science, Java Version, Third Edition 4
PseudocodeEnglish language constructs modeled to
look like statements available in most programming languages
Steps presented in a structured manner (numbered, indented, and so on)
No fixed syntax for most operations is required
![Page 5: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/5.jpg)
Invitation to Computer Science, Java Version, Third Edition 5
Pseudocode (continued)Less ambiguous and more readable than
natural language
Emphasis is on process, not notation
Well-understood forms allow logical reasoning about algorithm behavior
Can be easily translated into a programming language
![Page 6: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/6.jpg)
Sample PseudocodeTask: add two numbersPseudocode:
Start Get two numbers
Get first number Get second number
Add themPrint the answerEnd
![Page 7: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/7.jpg)
What does a flowchart look like?The pseudocode from the previous slide
would look like this as a flowchart:
Start
Get 2 numbers
Add them
Print answer
End
![Page 8: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/8.jpg)
What are those funny symbols?
START/END
INPUT/OUTPUT
PROCESS
DECISION
![Page 9: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/9.jpg)
What are those funny symbols?START/ENDUsed at the
beginning and end of each flowchart.
![Page 10: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/10.jpg)
INPUT/OUTPUTShows when
information/data comes into a program or is printed out.
What are those funny symbols?
![Page 11: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/11.jpg)
What are those funny symbols?PROCESSUsed to show
calculations, storing of data in variables, and other “processes” that take place within a program.
What are those funny symbols?
![Page 12: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/12.jpg)
What are those funny symbols?DECISIONUsed to show that
the program must decide whether something (usually a comparison between numbers) is true or false. YES and NO (or T/F) branches are usually shown.
What are those funny symbols?
Y
N
X>7?
![Page 13: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/13.jpg)
Another Sample: Calculating AgePseudocode:
Start Get year DOBCalculate age = (sysdate-DOB)Print ageIf age > 50 print OLDEnd
![Page 14: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/14.jpg)
Another Sample: Calculating Age
FlowchartStart Get year bornCalculate agePrint ageIf age > 50 print OLDEnd
Get yr
Calc age
Print age
Age>50?OLD Y
N
Start
End
![Page 15: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/15.jpg)
Self-CheckLook at the flowchart section below. If the
variable X is 5, what will print (K or 1st)?
X > 5?YN Print “1st”Print “K”
![Page 16: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/16.jpg)
Self-CheckLook at the flowchart section below. If the
variable X is 5, what will print (K or 1st)?
X > 5?YN Print “1st”Print “K”
K will be printed. The answer to the question “Is X greater than 5?” is NO, since X is equal to (not greater than) 5.
![Page 17: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/17.jpg)
Self-CheckChoose the correct
flowchart symbol for each of these statements.
AGE>65?
Calc. Tax
START
Print NAME
![Page 18: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/18.jpg)
Self-CheckChoose the correct
flowchart symbol for each of these statements.
AGE>65?
Calc. Tax
START
Print NAME
![Page 19: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/19.jpg)
ChallengeTry to write pseudocode and create a
flowchart for a program that calculates the average of three grades and prints the average.
The word GOOD should be printed only if the average is more than 80.
![Page 20: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/20.jpg)
ChallengePossible pseudocode
StartGet three gradesAverage them (add all of them / number of
grads taken)Print AverageAverage>80?
If Yes, print GOODEnd
![Page 21: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/21.jpg)
ChallengePossible
flowchart StartGet three gradesAverage themPrint AverageAverage>80?
If Yes, print GOODEnd
START
END
Get 3 grades
Calc avg
Print avg
Avg>80?GOODY
N
![Page 22: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/22.jpg)
ChallengeTry to write pseudocode and create a
flowchart for a program that calculates the average of three grades and prints the average.
The word GOOD should be printed only if the average is more than 80.
![Page 23: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/23.jpg)
ChallengePossible pseudocode
StartGet three gradesAverage themPrint AverageAverage>80?
If Yes, print GOODEnd
![Page 24: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/24.jpg)
ChallengePossible
flowchart StartGet three gradesAverage themPrint AverageAverage>80?
If Yes, print GOODEnd
START
END
Get 3 grades
Calc avg
Print avg
Avg>80?GOODY
N
![Page 25: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/25.jpg)
![Page 26: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/26.jpg)
Algorithm for Computing Average Miles per Gallon
![Page 27: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/27.jpg)
![Page 28: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/28.jpg)
Write a program to do the task: Print a list of the numbers from 4 to 9, next to each number, print the square of the number.
![Page 29: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/29.jpg)
![Page 30: Programming fundamentals lecture 3](https://reader035.vdocument.in/reader035/viewer/2022081508/55632b52d8b42a57348b4f82/html5/thumbnails/30.jpg)