Lect3 Algorithms and Flowchart Ppt
Lect3 Algorithms and Flowchart Ppt
FLOWCHARTS
1. INPUT x, y, z
2. sum = x+y+z
3. WRITE sum
4. STOP
EXERCISE 1
Write a computer solution using pseudocode to read two
numbers and multiply them together and print out their
product.
Write a computer solution using pseudocode to accept
four marks from a person, and find the average of the
marks. If the average is less than 40, the computer
should output “FAIL” else the computer should output
“PASS”
Write a computer solution using pseudocode to accept a
temperature reading in Fahrenheit, convert it to degrees
Celsius and display the results to the computer screen
Write pseudo code that performs the following: Ask a
EXERCISE 2
Write a computer solution using pseudocode to accept
four marks from a person, and find the average of the
marks. If the average is less than 40, the computer
should output “FAIL” else the computer should output
“PASS”
EXERCISE 3
Write a computer solution using pseudocode to accept a
temperature reading in Fahrenheit, convert it to degrees
Celsius and display the results to the computer screen
EXERCISE 4
Write pseudo code that performs the following: Ask a
user to enter a number. If the number is between 0 and
10, write the word blue. If the number is between 10 and
20, write the word red. if the number is between 20 and
30, write the word green. If it is any other number, write
that it is not a correct color option.
Pseudocode to Find the Average
of N Numbers
Write a computer solution in pseudocode to find the
average of N numbers.
Pseudocode to Find the Average
of N Numbers
The entire pseudocode for finding the average of N
numbers is shown below
1. SET a counter COUNTER to 0
2. Set an accumulator SUM to 0
3. Read number of students, N
4. While COUNTER <=N
5. READ a student’s age, AGE
6. ADD age to SUM
7. Increase COUNTER by 1
8. END OF WHILE
9. Computer average, AVERAGE = SUM divided by N
10. PRINT AVERAGE
11. STOP
FLOWCHARTS
The Flowchart
(Dictionary) A schematic representation of a sequence of
operations, as in a manufacturing process or computer
program.
(Technical) A graphical representation of the sequence of
operations in an information system or program.
Information system flowcharts show how data flows from
source documents through the computer to final
distribution to users.
Program flowcharts show the sequence of instructions in a
single program or subroutine. Different symbols are used
to draw each type of flowchart.
The Flowchart
A Flowchart
shows logic of an algorithm
emphasizes individual steps and their
interconnections
e.g. control flow from one action to the next
Flowchart Symbols
Basic
Name Symbol Use in Flowchart
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by
multiplying LFT with 30
Print length in cm (LCM)
Example 2
Flowchart
Algorithm START
Step 1: Input Lft
Input
Step 2: Lcm Lft x 30 Lft
Print
Lcm
STOP
Example 3
Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Pseudocode
Input the width (W) and Length (L) of a rectangle
Calculate the area (A) by multiplying L with W
Print A
Example 3
Algorithm START
Print
A
STOP
Flowcharts
Flowcharts is a graph used to depict or
show a step by step solution using
symbols which represent a task.
The symbols used consist of geometrical
shapes that are connected by flow lines.
It is an alternative to pseudocoding;
whereas a pseudocode description is
verbal, a flowchart is graphical in nature.
Principles of Programming - NI July
2005 38
Flowchart Symbols
Terminal symbol - indicates the beginning and
end points of an algorithm.
Statement 1
Statement 2
Statement 3
No Yes
Condition
else- then-
statement(s) statement(s)
43
Flowchart – repetition control structure
yes Loop
Condition
Statement(s)
no
45
Flowchart – example 1.1
Write an algorithm
using flowchart to
find the average of
three numbers
46
Flowchart – example 2
Write an algorithm, using pseudocode and a flowchart to
calculate age.
Begin
Calculate
Age = current year – birth date
Display
age
End
47
Flowchart – example 3
Write an algorithm, using pseudocode and a flowchart to
check age and display retired or young.
Begin
Read age
YES NO
Age > 60?
End
48
Flowchart – example 4
Begin
sum = 0
current_number = 1
NO
current_number <= 10? print sum
YES
End
sum = sum + current_number
current_number = current_number + 1
49
Example 5
Program: Determine the direction of a numbered
NYC street, even is eastbound other westbound
Get street
If street is even Then
Display Eastbound
Else
Display Westbound
End If
Example 6
Write an algorithm in pseudocode and draw a
flowchart for a program that will read the two
sides of a rectangle and calculate its area.
Example 7
Write an algorithm that reads two values,
determines the largest value and prints the
largest value with an identifying message.
Example 8
Write an algorithm in pseudocode and draw a
flowchart to convert the length in feet to
centimeter.