Sei sulla pagina 1di 26

CHAPTER TWO

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.

Since this is the case, we should make every effort to


improve our problem solving ability. This study of problem solving and
flowcharting will demonstrate a systematic approach to problem solving
which will apply in particular to computer programming problems.

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?

The first impression is to subtract 20 from 100 to obtain an


answer, but careful examination of the problem
shows that this method will determine how
many chicken died, where we have 20 chicken
left.
So we should study each problem carefully and
be sure to determine what question must be
answered before attempting to solve the
problem.

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:

Let r = the radius of the circle

3 Next, all unnecessary information should be discarded. Many


problems contain information which has no meaning to the solution.

Consider this problem:


A man received a check for 300 pounds of which he deposited 120 pounds
into his account. If his account balance was 200 pounds after this deposit,
how much was his balance before the deposit?

١١
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

4 The next step is to discover relationships between the data and to


express these relationships as equations.

For example, read the radius of a circle from the keyboard


then find its area and circumference.

Let R = the radius of the circle


Let A = the area of the circle
Let C = the circumference of the circle
Thus,
A=π*r*r
C=2*π*r
Represents the relationships in the problem

5 The final step of problem solving is to arrange the equations into an


algorithm. An algorithm is a set of step-by-step
step step instructions that, when
completed, solves a problem. After the algorithm is established, the
actual procedures may be executed by either
either a computer or manually.
What we are concerned with is how we translate the English-like
English
algorithm into instructions that the computer can execute.
Now the procedures may be executed to obtain the solution.

١٢
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.

2.2.1 THE MEANING OF A FLOWCHART


A flowchart is a diagrammatic representation that illustrates the sequence
of operations to be performed to get the solution of a problem. Once the
flowchart is drawn, it becomes easy to write the program in any high

١٤
level language. Hence, it is correct to say that a flowchart
is a must for the better documentation of a complex program.

2.2.2 GUIDELINES FOR DRAWING A FLOWCHART


Flowcharts are usually drawn using some standard symbols. Some
standard symbols, which are frequently, required for flowcharting many
computer programs are shown below:

Figure 2.1 Basic Flowcharting Shapes.

١٥
Note that the 5 basic flowcharting symbols are:
1-Terminal block 2-Input / Output
3- Process block 4- Decision block 5- Flow lines.

The following are some guidelines in flowcharting:


a. The flowchart should be clear, neat and easy to follow. There
should not be any room for ambiguity in understanding the
flowchart.
b. The usual direction of the flow of a procedure or a system is from
left to right or top to bottom.
c. Only one flow line should come out or from a process symbol.

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.

f. Write within standard symbols briefly. As necessary, you can use


the annotation symbol to describe data or computational steps more
clearly.

g. If the flowchart becomes complex, it is better to use connector


symbols to reduce the number of flow lines. Avoid the intersection
of flow lines if you want to make it more effective and better way
of communication.
h. Ensure that the flowchart has a logical start and finish.
i. It is useful to test the validity of the flowchart by passing through it
with a simple test data.

2.2.3 ADVANTAGES OF USING FLOWCHARTS


The benefits of flowcharts are as follows:

a. Communication: Flowcharts are a good way of communicating the


logic of a system to all concerned.
b. Effective analysis: With the help of flowchart, problem can be
analyzed in more effective way.
c. Proper documentation: Program flowcharts serve as a good
program documentation.

١٧
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.

2.2.4 LIMITATIONS OF USING FLOWCHARTS


a. Complex logic: Sometimes, the program logic is quite complicated.
In this case, flowchart becomes complex and clumsy.
b. Alterations and modifications: If alterations are required the
flowchart may require re-drawing completely.
c. The essentials of what is done can easily be lost in the technical
details of how it is done.

2.3 SOME EXAMPLES ON FLOWCHARTING


Now we shall present few examples on flowcharting for proper
understanding of this technique. Problems 1 through 8 are given for the
normal students. Problems 9 through 11 are optional; they are used for
enrichment of the material for the distinguished students.

١٨
Example 2.1
Draw flowchart to represent what will you do if your lamp doesn't work?

Answer:

Figure 2.2 Flowchart for changing lamp problem.

١٩
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

Take a bus Take a taxi

Arrive to school

Figure 2.3 Flowchart for the method of “going to school” problem.

٢٠
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.

Figure 2.4 Flowchart for the division of two numbers problem.

٢١
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.

Answer: The required flowchart is shown below, where SUM is a


location in memory (variable) at which we store the sum in it and N is a
variable at which we store the natural number.

Figure 2.5 Sum of the first 100 natural numbers.

٢٢
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

Answer: The required flowchart is shown below, Where N is working as


a counter for the number of odd numbers that have been added to SUM,
and T represents the value of the Term to be added to SUM.

Figure 2.6 Sum of the first 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=


Figure 2.7 Sum of a series with unknown number of terms.

٢٤
Example 2.7

Draw a flowchart to find the largest of three numbers A, B, and C.

Answer: The required flowchart is shown below:

Figure 2.8 Flowchart solution for example 2.7.

٢٥
Now let's solve this problem by Assuming that the read

numbers are : A=20, B=10, and C=30

Figure 2.9 Example on flowchart solution for example 2.7.

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.

Figure 2.10 Flowchart solution for example 2.8.

٢٧
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.

Figure 2.11 Example of flowchart solution for example 2.8.

٢٨
• 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:

1- The total number of males and females.


2- The number of employees by the following age categories:
a. Less than 20
b. 20-29
c. 30-39
d. 40-49
e. 50-60
f. Over 60
3- The total number of people who have been working for the
company for 10 years or more.
4- The total number of engineers.

Draw the needed flowchart

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.

Figure 2.12 Flowchart solution for example 2.9.

٣٠
Example 2.10

Draw a flowchart to solve an equation of second order given by:


      0
Given that its roots  and  are given by
√   4
  √
,  … … … … … … … … … … … … . 2
2.1
2

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

Figure 2.13 Flowchart solution for example 2.10.

٣٢
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:

Figure 2.14 Example on flowchart solution for example 2.10.

٣٣
2-4 Questions
Question 1:
Donia drew a Flowchart to add two numbers and printed the result as
follows.

Correct the Flowchart.

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

If the read radius is negative, the flowchart should print a warning


message then terminates.

Question 5:
Draw a flowchart to read number “x”, then it calculate the value of
function y using the formula
  
    … … … … … ..
3 5 7

Where we add 100 terms.

٣٥

Potrebbero piacerti anche