3 Flowcharts
3 Flowcharts
1
FLOWCHARTS
▪ Graphically depicts the logical steps to carry
out a task and shows how the steps relate to
each other.
▪ Used to document how an algorithm was
designed
▪ Easy to understand and visualize
2
FLOWCHART SYMBOLS
3
FLOWCHART SYMBOLS CONTINUED
4
START/END (TERMINAL )
▪ Indicates the start and end of an algorithm
▪ Represented by a rectangle with rounded
sides
▪ There are typically two:
▪ one to start the flowchart
▪ one to end the flowchart
5
INPUT/OUTPUT
▪ Indicates data being:
▪ inputted into the computer
▪ outputted to the user
▪ Represented by a parallelogram
▪ Flowcharts can have many of this shape
6
PROCESS
▪ Indicates
▪ Data being processed
▪ Calculations being processed
▪ Represented by a rectangle
▪ The most common shape in a flowchart
7
DECISION
▪ Indicates a conditional branch
▪ describe a condition or a question
▪ has more than one outgoing branch, depending on
the outcomes to the condition/question
▪ Represented by a diamond
8
PROBLEM SOLVING EXAMPLE
▪ How many stamps do you use when mailing a letter?
▪ One rule of thumb is to use one stamp for every five sheets of
paper or fraction thereof.
9
PSEUDOCODE EXAMPLE
Determine the proper number of stamps for a
letter
Read Sheets (input)
Set the number of stamps to Sheets / 5
(processing)
Round the number of stamps up to the next
whole number (processing)
Display the number of stamps (output)
10
FLOWCHART EXAMPLE
11
DECISION FLOW CHART STRUCTURE
12
SAMPLE NESTED IF STRUCTURE
13
LOOPING FLOW CHART STRUCTURE
14
DIRECTION OF NUMBERED NYC STREETS
ALGORITHM
▪ Problem: Given a street number of a one-way street in New
York City, decide the direction of the street, either eastbound or
westbound
▪ Discussion: in New York City even numbered streets are
Eastbound, odd numbered streets are Westbound
15
PSEUDOCODE
Program: Determine the direction of a
numbered NYC street
Get street
If street is even Then
Display Eastbound
Else
Display Westbound
End If
16
FLOWCHART
17
EXAMPLE-1
▪ Draw a flowchart to find the average of two
numbers.
18
EXAMPLE-1 SOLUTION
▪ Draw a flowchart to find the
average of two numbers.
1. Start
2. Read two numbers in x and y
3. Set w = x + y
4. Set z = w / 2
5. Print z
6. End
19
EXAMPLE-2
▪ Draw a flowchart to take the sides of a
rectangle and calculate its perimeter and
area.
20
EXAMPLE-2 SOLUTION
▪ Draw a flowchart to take the
sides of a rectangle and
calculate its perimeter and area.
1. Start
2. Read width and length in w and h
3. Set a = w ⨯ h
4. Set s = 2 ⨯ (w + h)
5. Print “perimeter is s and area is a”
6. End
21
EXAMPLE-3
▪ Draw a flowchart to take a number and prints
if it is odd or even.
22
EXAMPLE-3 SOLUTION
▪ Draw a flowchart to take a
number and prints if it is odd
or even.
1. Start
2. Read number in c
3. If c modulo 2 = 0 then
4. print even
5. Else
6. print odd
7. End If
8. End
23
EXAMPLE-4
▪ Draw a flowchart to take three numbers and
print the maximum of them
24
EXAMPLE-4 SOLUTION
▪ Draw a flowchart to take three
numbers and print the maximum
of them
1. Start
2. Read x, y, z
3. If x ≥ y then
4. Set Max = x
5. Else
6. Set Max = y
7. End If
8. If z ≥ Max then
9. Set Max = z
10.End If
11.print “The maximum number is Max”
12.End
25
EXAMPLE-5
▪ Draw a flowchart to print integers 1 to N
26
EXAMPLE-5 SOLUTION WITH IF
▪ Draw a flowchart to
print integers 1 to N
1. Start
2. Read N
3. Set Count = 1
4. Print Count
5. If Count < N then
6. Set Count = Count + 1
7. Goto Line 4
8. End If
9. End
27
EXAMPLE-5 SOLUTION WITH WHILE
▪ Draw a flowchart to
print integers 1 to N
1. Start
2. Read N
3. Set Count = 1
4. While Count ≤ N
5. Print Count
6. Set Count = Count + 1
7. End While
8. End
28
EXAMPLE-6
▪ Draw a flowchart to take integer N print N!
29
EXAMPLE-6 SOLUTION
▪ Draw a flowchart to take
integer N print N!
1. Start
2. Read N
3. Set Fact = 1, Count = 1
4. Set Fact = Fact * Count
5. Set Count = Count + 1
6. If Count ≤ N then
7. Goto Line 4
8. End If
9. print “The Factorial of N is
Fact”
10.End
30
EXAMPLE-6 SOLUTION2
▪ Draw a flowchart to take
integer N print N!
1. Start
2. Read N
3. Set Fact = 1, Count = 1
4. While Count ≤ N
5. Set Fact = Fact * Count
6. Set Count = Count + 1
7. End While
8. print “The Factorial of N is Fact”
9. End
31
CLASS AVERAGE ALGORITHM
▪ Problem: Calculate and report the grade-
point average for a class
▪ Discussion: The average grade equals the
sum of all grades divided by the number of
students
Output: Average grade
Input: Student grades
Processing: Find the sum of the grades; count
the number of students; calculate average
32
PSEUDOCODE
1. Start
2. Set Sum = 0, Count = 0, Avg = 0
3. While more data
4. Read Grade
5. Set Sum = Sum + Grade
6. Set Count = Count + 1
7. End While
8. Set Avg = Sum / Count
9. print “The average grade of the class is Avg”
10.End
33
FLOWCHART
34
TRACING
Trace the algorithm for:
43, 74, 65, 58, 81, 39
37