icp - lecture 6
TRANSCRIPT
![Page 1: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/1.jpg)
CSC 103
Lecture 6
Introduction to Computers and Programming
![Page 2: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/2.jpg)
Pseudo-code and Flow Charts
There are two commonly used tools to help to document program logic (the algorithm)
These are:
Flowcharts Graphical representation
used for small problems
Pseudo-code English and programming combination
used for large problems
2
![Page 3: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/3.jpg)
Pseudo-code
Pseudo-Code is a numbered list of instructions to perform some task
Statements are written in simple English without regard to the final programming language
Each instruction is written on a separate line
The pseudo-code is the program-like statements written for human readers, not for computers
Implementation is to translate the pseudo-code into programs, such as “C” language programs
3
![Page 4: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/4.jpg)
Writing Pseudo-code
Number each instruction
This is to enforce the notion of an ordered sequence of
operations
Furthermore we introduce a dot notation (e.g. 3.1 come after 3 but before 4) to number subordinate operations for conditional and iterative operations
Each instruction should be unambiguous and effective
Completeness: Nothing is left out
4
![Page 5: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/5.jpg)
Basic Elements of Pseudo-code
A Variable Having name and value
There are two operations performed on a variable
Assignment Operation is the one in which we associate
a value to a variable.
The other operation is the one in which at any given time we intend to retrieve the value previously assigned to that variable (Read Operation)
5
![Page 6: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/6.jpg)
Basic Operations of Pseudo-code
Assignment Operation
This operation associates a value to a variable.
While writing Pseudo-code you may follow your own syntax.
Some of the possible syntaxes are:
Assign 3 to x
Set x equal to 3
x=3
6
![Page 7: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/7.jpg)
Basic Operations of Pseudo-code
Read Operation
In this operation we intend to retrieve the value previously assigned to that variable. For example:
Set Value of x equal to y
Read the input from user
This operation causes the algorithm to get the value of a variable from the user.
Get x
Get a, b, c
7
![Page 8: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/8.jpg)
Basic Operations of Pseudo-code
Print the output to the user
Print x (This will print value of variable x)
Print “Your mileage is” x
Carry out basic arithmetic computations
Set x to 10
Set y to x*x/3
8
![Page 9: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/9.jpg)
Example: Pseudo-code of calculating area of circle
1. Begin
2. Input value for radius
3. Calculate area (pi x radius2)
4. Output radius and area
5. Quit
THEN PROGRAM
9
![Page 10: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/10.jpg)
Flow Chart
Graphical representation of an algorithm
Some of the common symbols used in flowcharts are shown:
Start/Stop
Preparation
Input/Output
Process
Decision
Connector
10
![Page 11: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/11.jpg)
With flowcharting, essential steps of an algorithm are shown using the shapes above.
The flow of data between steps is indicated by arrows, or flowlines. For example, a flowchart (and equivalent Pseudocode) to compute the interest on a loan is shown below:
11
![Page 12: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/12.jpg)
Pseudo-code Flow chart
1. Read NAME, BALANCE, RATE 2. Compute INTEREST as BALANCE x RATE 3. Write (Display) NAME and INTEREST
Stop
Start
Read NAME, BALANCE, RATE
INTEREST=BALANCE x RATE
Write NAME, INTEREST
12
![Page 13: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/13.jpg)
Note that the Pseudo-code also describes the essential
steps to be taken, but without the graphical enhancements.
Another example of a flowchart and the equivalent pseudo-code is shown next.
13
![Page 14: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/14.jpg)
Pseudo-code Flow chart
1. Read X,Y,Z 2. Compute Sum(S) as X+Y+Z 3. Compute Average(A) as S/3 4. Compute Product(P) as X x Y x Z 5. Write (Display) Sum, Average and Product
Stop
Start
Read X, Y and Z
S= X+Y+Z A = S/3
P = X x Y x Z
Write S, A and P
14
![Page 15: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/15.jpg)
Some Examples
Write pseudo-code of a program that asks the user to enter two numbers and prints the sum, product, difference, and division of the two numbers.
Write pseudo-code of a program that solves a quadratic equation ax2+bx+c by taking a, b and c as input from user.
15
![Page 16: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/16.jpg)
Decision Making and Pseudo-code
16
![Page 17: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/17.jpg)
Stop
Start
Read A, B
BIG = A SMALL = B
Write BIG, SMALL
BIG = B SMALL = A
A < B ? Yes No
1. Read A, B 2. If A is less than B
2.1 BIG = B 2.2 SMALL = A
3. Else 3.1 BIG = A 3.2 SMALL = B
4. Write BIG, SMALL
Example
17
![Page 18: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/18.jpg)
Loops and Pseudo-code
18
![Page 19: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/19.jpg)
Start
K=1
K=K+1
K > 10?
Procedure
Stop
Yes
No
19
![Page 20: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/20.jpg)
Draw the Flowchart
1. get hours worked
2. get pay rate
3. if hours worked ≤ 40 then
3.1 gross pay = pay rate times hours worked
4. else
4.1 gross pay = pay rate times 40 plus 1.5 times pay rate times (hours worked minus 40)
5. display gross pay
6. End
20
![Page 21: ICP - Lecture 6](https://reader034.vdocument.in/reader034/viewer/2022042518/555e3624d8b42a63048b494e/html5/thumbnails/21.jpg)
Draw the Flowchart 1. get number of quizzes 2. sum = 0 3. count = 0 4. while count < number of quizzes 4.1 get quiz grade 4.2 sum = sum + quiz grade 4.3 count = count + 1 5. average = sum / number of quizzes 6. display average 7. End
21