Lecture 4 (Pseudocode-internet-processor) PDF
Document Details
Uploaded by ReasonedDulcimer
Tags
Summary
This lecture discusses pseudocode, an informal way to represent algorithms using a mixture of English, mathematical notations, and programming keywords. It also explores the use of flowcharts for graphically representing algorithm steps, with examples of flowchart symbols and common flowchart diagrams. The examples emphasize finding whether a number is prime.
Full Transcript
We can write pseudocode, an artificial informal syntax that is just a more specific version of English (or other human language) that represents our algorithm. Pseudocode It is a mixture of: English statements (or any other language). Some mathem...
We can write pseudocode, an artificial informal syntax that is just a more specific version of English (or other human language) that represents our algorithm. Pseudocode It is a mixture of: English statements (or any other language). Some mathematical notations. Selected keywords from programming language. And there is no standard convention for writing pseudo- codes, as long as clarity is ensured. 36 Understanding Computers: Today and Tomorrow, 13th Edition Pseudocode for our algorithm: 1 Pick up phone book 2 Open to middle of phone book 3 Look at page 4 If Smith is on page 5 Call Mike 6 Else if Smith is earlier in book 7 Open to middle of left half of book 8 Go back to line 3 9 Else if Smith is later in book 10 Open to middle of right half of book 11 Go back to line 3 12 Else 13 Quit 2 Understanding Computers: Today and Tomorrow, 13th Edition Some of these lines start with verbs, or actions. We’ll start calling these functions: 1 Pick up phone book 2 Open to middle of phone book 3 Look at page 4 If Smith is on page 5 Call Mike 6 Else if Smith is earlier in book 7 Open to middle of left half of book 8 Go back to line 3 9 Else if Smith is later in book 10 Open to middle of right half of book 11 Go back to line 3 12 Else 13 Quit 3 Understanding Computers: Today and Tomorrow, 13th Edition We also have branches that lead to different paths, like forks in the road, which we’ll call conditions: 1 Pick up phone book 2 Open to middle of phone book 3 Look at page 4 If Smith is on page 5 Call Mike 6 Else if Smith is earlier in book 7 Open to middle of left half of book 8 Go back to line 3 9 Else if Smith is later in book 10 Open to middle of right half of book 11 Go back to line 3 12 Else 13 Quit 4 Understanding Computers: Today and Tomorrow, 13th Edition Questions that decide where we go are called Boolean expressions, which eventually result to a value of true or false: 1 Pick up phone book 2 Open to middle of phone book 3 Look at page 4 If Smith is on page 5 Call Mike 6 Else if Smith is earlier in book 7 Open to middle of left half of book 8 Go back to line 3 9 Else if Smith is later in book 10 Open to middle of right half of book 11 Go back to line 3 12 Else 13 Quit 5 Understanding Computers: Today and Tomorrow, 13th Edition Finally, we have words that lead to cycles, where we can repeat parts of our program, called loops: 1 Pick up phone book 2 Open to middle of phone book 3 Look at page 4 If Smith is on page 5 Call Mike 6 Else if Smith is earlier in book 7 Open to middle of left half of book 8 Go back to line 3 9 Else if Smith is later in book 10 Open to middle of right half of book 11 Go back to line 3 12 Else 13 Quit 6 Understanding Computers: Today and Tomorrow, 13th Edition Flowchart Diagrams Flow charts are one of most famous diagrams used to show programs and processes. Sometimes they’re partitioned into 4 groups : Document flowcharts : showing controls over a document-flow through a system Data flowcharts : showing controls over a data flows in a system System flowcharts : showing controls at a physical or resource level Program flowchart : showing the controls in a program within a system. Understanding Computers: Today and Tomorrow, 13th Edition 7 Computer Program Flowcharts Computer program flowcharts are used to show control flow in a computer program. It is sometimes used to show an algorithm without writing the code. Sometimes they are used for training purposes for beginner programmers who don't know programming codes but can understand graphical symbols in flowcharts. Understanding Computers: Today and Tomorrow, 13th Edition 8 Flowcharts Symbols Understanding Computers: Today and Tomorrow, 13th Edition 9 Flowcharts Symbols Meaning Start : represents the start of program. Arrows : represent flow of control in a program. Rectangles : Used to show computation or a specific process. Parallelogram : Used for getting input from user or sending output to user. Rhombus : Used for conditional flow control where program has to decide which way to go. End : represents the end of program. Understanding Computers: Today and Tomorrow, 13th Edition We can also draw a Flowchart; a diagram that uses standard symbols to solve the problem (to represent our Algorithm graphically). Flowcharts consist basically of the following 6 symbols: Flowcharts 1 2 3 4 5 6 42 Understanding Computers: Today and Tomorrow, 13th Edition Start Pick up phone book Open to middle of phone book Open to middle of Look at page B left half of book YES NO Smith on YES NO earlier in Call Smith Page? Book? C A End Understanding Computers: Today and Tomorrow, 13th Edition 45 A later in NO Book? C YES Open to middle of right half of book B 13 Understanding Computers: Today and Tomorrow, 13th Edition Flowchart Examples Understanding Computers: Today and Tomorrow, 13th Edition 14 Flowchart Examples Understanding Computers: Today and Tomorrow, 13th Edition 15 This This example example checks if N calculates is prime. sum of numbers from 1 to N. Understanding Computers: Today and Tomorrow, 13th Edition 16 This example This example checks if 3 calculates all numbers can divisors of N. be length of sides of a triangle. Understanding Computers: Today and Tomorrow, 13th Edition 17