4. Using Subprocesses in Flowcharts
• Flowcharts of a complex process can
be broken down into smaller sub-
processes for clarity and re-usability.
• In this case a flowchart can point to a
different sub-process within its flow.
• The predefined process symbol to the
right is used to show such
subprocesses.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
5. The subprocess is useful because:
• it permits the modularization of complex programs.
• it provides a means of simplifying programs by making common
processes available to a wide number of programs.
• it makes for more reliable programs since once it is shown that a
process works then it can be made a subprocess and need not be tested
again.
• In flow charts subprocesses are also useful in dealing with the
flowcharting rule that a flow chart should have no more than 15 or so
symbols on a page.
Using Subprocesses in Flowcharts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
6. How to decide which sub-programs are needed?
• Break the program into subtasks
• Examine each subtask and break it down if
needed into further subtasks
• Continue until the subtask cannot be broken
down into small subtasks
• Example: Baking a cake
Using Subprocesses in Flowcharts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
7. What are things/tasks you have to do to make a
cake?
• Ingredient preparation
• Process the ingredients
• Assemble the cake
Using Subprocesses in Flowcharts
Example 1 : Baking a cake
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
9. Using Subprocesses in Flowcharts
Example 2: Read temperature in Fahrenheit and print its
value in both Fahrenheit and Celcius
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
10. Using Subprocesses in Flowcharts
Example 3: Maximum of 3 Integers Revisited
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
15. Problem: Design using flowchart
and pseudocode a program that
prints a figure similar to the one
on the right for a given number of rows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
16. Problem: Design using flowchart
and pseudocode a program that
prints a figure similar to the one
on the right for a given number of rows.
Analysis:
Inputs : None.
Variables: N, Row_No, where:
1. N = No of Rows = 9
2. Row_No changes from 1:N
Steps:
For each row
1. Calculate number of stars
2. Print ‘*’ ( N - Row_No +1 ) times on the same row
3. New Line
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
17. Start
For Row_No =1 To N
No_of_stars=N-Row_No+1
For Col =1 To No_of_stars
Print ‘*’;
End For
New line
End For
Stop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
Hint:
Adding an “;” at the end of the Print statement
prevents skipping to the next line after printing
19. • Typically used for
working with two dimensions (or more)
such as in the above example
•When a loop is nested inside another loop,
the inner loop runs many times
inside the outer loop.
•In each iteration of the outer loop,
the inner loop will be re-started.
•The inner loop must finish all of its iterations
•before the outer loop can continue to its next iteration.
•The inner loop must have a different name for its
loop counter variable so that it will not conflict with the outer loop.
For Row_No =1,N
No_of_stars=N - Row_No +1
For Col =1, No_of_stars
Print ‘*’
End For
New line
End For
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
20. Design a program that types the binary numbers corresponding to the integers 0 – 7
Analysis:
Inputs: None
Define 3 variables A, B, C as the binary digits needed to represent the numbers from 0 -7
Initial values: A=0, B=0, C=0
Output: 0 000
1 001
2 010
.
.
7 111
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
25. Start
Read the number of elements n
Initialize i=0
Repeat
Read element no i
increment i
Until i = n
Initialize i=0
Repeat
write element no i
increment i
Until i = n
End
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
26. Assumption:
The array “a” is already filled
with N numbers, and N >=0
Print
largest
End
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
27. Given an array arr[] of N elements, the task is
to write a function to search a given
element x in arr[].
Example
Input: arr[] = {10, 20, 80, 30, 60, 50,110, 100,
130, 170}, x = 110;
Output: 6
Explanation: Element x is present at index 6
Input: arr[] = {10, 20, 80, 30, 60, 50,110, 100,
130, 170}, x = 175;
Output: -1
Explanation: Element x is not present in arr[].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
28. Input x, array
found = False
Length = array size
counter =0
WHILE found = False AND counter < length
IF array[counter] = x
THEN
Print counter
found = True
ELSE
counter = counter + 1
END IF
END WHILE
IF found = False
THEN
print 'Item not found’
END IF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
Input x, array
Length = array size
counter =0
WHILE counter < length
IF array[counter] = x
THEN
Print counter
counter = length
END IF
counter = counter + 1
END WHILE
IF counter = length
THEN
print 'Item not found’
END IF