Académique Documents
Professionnel Documents
Culture Documents
Flowcharting
OBJECTIVES
Upon completion of this chapter, you should be able to:
• Know the main steps for problem solving.
• Define term flowchart.
• List and discus advantages and disadvantages of flowchart
as a logic development tool.
• Solve word problems using flowcharts.
2.1 Introduction
A computer is simply a tool which serves to assist in problem solving.
As a scissor helps to cut a piece of paper but cannot cut
it itself, a computer helps to solve a problem.
Computers share a common drawback, alone they are
useless. The human brain is the only device capable of
creative thinking.
1 The first step in problem solving is to define the problem. The problem
statement should be examined carefully to fully determine what question
should be answered.
١٠
For example, consider this problem. A farmer had 100 chicken
and all but 20 of them died. How many chicken does the farmer
have left?
2 After the problem is defined, the next step is to assemble all given data
and to assign variable names to those quantities which are unknown.
Like the following statment:
١١
In this problem, the amount of the check (300 pounds)
Does not matter; only the amount deposited to the account
affects the problem solution. In this case, the given figure of 300 pounds
should be discarded
١٢
The following example will illustrate the sequence of
steps used in problem solving.
A school has 1000 students of whom 48 percent are boys. The school gave a
party which 70 percent of the students attended. If 60 percent of those
attending were boys, how many girls attended the party?
Solution:
1. The question is: “How many girls attended the party?”
2. Let x = the total number of students attending the party
Let y = the number of boys attending the party
Let z = the number of girls attending the party
3. The given fact that 48 percent of the students are boys has no effect on
the problem and may be discarded.
4. The relationships stated are:
a. 70 percent of the students attended ( x )
b. 60 percent of those attending were boys ( y )
c. This implies that the difference between x and y represents the
number of girls attending (z).
So, we express these relationships as the following equations:
a. x = 1000 * 0.70
b. y = x * 0.60
c. z = x – y
5. Since the variable x must be known to find the variable y and both x
and y must be known in order to find z, the equations must be solved in
the given order.
١٣
We can summarize the main needed steps for problem
solving as follows:
1. Define the problem
2. Assemble known quantities and assign variable names
3. Discard unimportant data.
4. Establish relationships and express them as equations.
5. Determine the proper algorithm by arranging the equations in correct
sequence.
2.2 FLOWCHARTING
The flowchart is a mean of visually presenting the flow of data through
an information processing
systems, the operations
performed within the
system and the sequence in
which they are performed.
In this chapter, we shall
concern ourselves with the
program flowchart, which
describes what operations (and in what sequence) are required to solve a
given problem.
١٤
level language. Hence, it is correct to say that a flowchart
is a must for the better documentation of a complex program.
١٥
Note that the 5 basic flowcharting symbols are:
1-Terminal block 2-Input / Output
3- Process block 4- Decision block 5- Flow lines.
d. Only one flow line should enter a decision symbol, but two or three
flow lines, one for each possible answer, should leave the decision
symbol.
١٦
e. Only one flow line is used in conjunction with terminal
symbol.
١٧
d. Efficient coding: The flowcharts act as a guide during
the system’s analysis and program development phase.
e. Proper debugging: The flowchart helps in debugging process.
f. Efficient program maintenance: The maintenance of a program
becomes easy with the help of flowchart.
١٨
Example 2.1
Draw flowchart to represent what will you do if your lamp doesn't work?
Answer:
١٩
Example 2.2
Draw flowchart to represent what will you do if you get up and you want to
go to school?
Answer:
Get up
Is it 6 YES
O’clock ?
NO
YES Is it 6:30
Go to school on feet
O’clock ?
NO
Arrive to school
٢٠
Example 2.3
Draw flowchart to represent the process of reading two numbers,
dividing them, and then displaying the result.
Answer: You have to note that “Read number1” means that we read a
number and store it in memory in a locaon which we call number1.
٢١
Example 2.4
Draw a flowchart to find the sum of first 100 natural numbers. This
means that we want to find sum where sum is given by:
Sum = 1 + 2 + 3 + …………………………………+ 99 + 100.
٢٢
Example 2.5
Draw a flowchart to find the sum of the first 25 odd natural numbers. This
means that we want to find sum where sum is given by:
Sum = 1 + 3+ 5 + ………………………………… here we add 25 odd natural
numbers
٢٣
Example 2.6
Draw a flowchart to find the sum of all terms of the following
series greater than or equal to 0.01.
1 1 1 1
1 …………………………
2 3 4 5
Answer: The required flowchart is given below. You have to note that T
stands for Term and that the Nth term=
٢٤
Example 2.7
٢٥
Now let's solve this problem by Assuming that the read
Example 2.8
Ahlia Company desires to generate its payroll via computer. The information
collected that every payroll period includes the employee social security
number, pay rate, and working hours. The payroll department would like a
report listing these items.
Add to this, overtime pay, gross pay, tax withheld (20% of gross pay), and
net pay. Note that the employee should be given overtime pay 1.5 of pay rate
in case that he worked a number of hours greater than 150 hours.
٢٦
Answer: Let's label the data items given to
us in this problem with variable names as given below:
SSN Social Security Number
PR Pay Rate
WH Working Hours
GP Gross Pay
OP Overtime Pay
TAX Tax Withheld
NP Net Pay
This approach will make the flowcharting process easier by using these
variable names inside the flowchart symbols.
٢٧
Solution for Example 2.8.
Now let's solve this problem by assuming that the read numbers
are :SSN=12345, HW=160 Hours, and PR=6 L.E.
٢٨
• Some optional enrichment examples for distinguished
Students.
Example 2.9
The manager of your company has requested a report from the
Personnel Department about all employees. The report should include
the following information:
Solution:
Most of the variables that will be used in this problem are counters
that must be initialized to zero at the beginning of the problem.
These variables are:
Male_count = number of males
Female_count = number of females
under_20 = number of employees under 20
20_29 = number of employees between 20 and 29
30_39 = number of employees between 30 and 39
40_49 = number of employees between 40 and 49
50_60 = number of employees between 50 and 60
over_60 = number of employees over 60
٢٩
In addition to that you have to read today’s date that can
be stored at variable ‘Today’ , birthday that can be stored in
variable ‘BD’ , in addition to the date of employment that can be stored
in variable ‘DOE’. Assume that variable ‘OCC’ stores the occupation
code which is ‘ENG’ for engineers.
٣٠
Example 2.10
where a, b, and c are real numbers. You have to take into consideration
the following:
1. If 4
is negative , you have to say that we have complex
roots.
2. If 4
is equal to zero,
zero the two roots are equal to –b/2a.
–
3. If ( 4
)) greater than zero,
zero the solution
n will given by equation
2.1.
٣١
Solution:
Figure 2.13 shows a Flowchart to solve this problem
٣٢
Assuming that the equation needed to be solved is given by
2 13 15
0
That means that a , b, and c are equal to 2, -13, 15. The following
flowchart shows the steps for the solution of this problem:
٣٣
2-4 Questions
Question 1:
Donia drew a Flowchart to add two numbers and printed the result as
follows.
Question 2:
Draw a flowchart to read the age of Hany and Hesham, then it prints the
name of the elder.
٣٤
Question 3:
Draw a flowchart that reads a temperature in Fahrenheit degrees and
convert it into Celsius degrees, using the formula
5
° ° 32
9
Question 4:
Draw a flowchart that reads the radius of a sphere “r”, then it calculates
its volume “V” and surface area “A” using formulas
4
3
4
where
22
π
7
Question 5:
Draw a flowchart to read number “x”, then it calculate the value of
function y using the formula
… … … … … ..
3 5 7
٣٥