Anda di halaman 1dari 26

CHAPTER TWO Flowcharting

OBJECTIVES Upon completion of this chapter, you should be able to: • • • • Know
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.

CHAPTER TWO Flowcharting OBJECTIVES Upon completion of this chapter, you should be able to: • •

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

For example, consider this problem. A farmer had 100 chicken and all but 20 of them
2
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

For example, consider this problem. A farmer had 100 chicken and all but 20 of them
3
3

Next, all unnecessary information should be discarded. Many

problems contain information which has no meaning to the solution.

For example, consider this problem. A farmer had 100 chicken and all but 20 of them

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 a mount of the check (300 pounds) Does not matter; only the
In this problem, the a mount of the check (300 pounds)
Does not matter; only the amount deposited to the account
affects the problem so lution. In this case, the given figure o f 300 pounds
should be discarded
4 The next step is to d iscover relationships between the data and to
express these relations hips as equations.
For exampl e, read the radius of a circle from the ke yboard
then find its a rea and circumference.
Let R = the radi us of the circle
Let A = the area
of the circle
Let C = the circ umference of the circle
Thus,
A = π * r * r
C = 2 * π * r
Represents the relationships in the problem
5 The final step of p roblem solving is to arrange the equa tions into an
algorithm. An algorit hm is a set of step-by-step instructio ns that, when
completed, solves a
problem. After the algorithm is est ablished, the
actual procedures ma y be executed by either a computer
What we are concer ned with is how we translate the
algorithm into instruct ions that the computer can execute.
Now the procedures m ay be executed to obtain the solution.
or manually.
English-like
١٢

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

١٣

the ١٤
the
١٤

We can summarize t he main needed steps for problem solving as follows:

  • . Define the problem

  • . Assemble known q uantities and assign variable names

  • . Discard unimporta nt data.

  • . Establish relationsh ips and express them as equations.

  • . Determine the prop er algorithm by arranging the equation s in correct sequence.

2.2 FLOWCHAR TING

The flowchart is a me an of visually presenting the flow of data through an information proc essing systems, the oper ations

performed within

system and the seque nce in

which they are perfo rmed.

In this chapter, we

shall

concern ourselves wi th the

program flowchart,

which

describes what operati ons (and in what sequence) are requir ed to solve a

given problem.

2.2.1 THE MEANIN G OF A FLOWCHART

A flowchart is a diagr ammatic representation that illustrates the sequence of operations to be pe rformed to get the solution of a probl em. Once the flowchart is drawn, it becomes easy to write the program in a ny 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:

level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
 
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the
level language. Hence, it is correct to say that a flowchart is a must for the

Figure 2.1 Basic Flowcharting Shapes.

١٥

Note that the 5 basic flowcharting symbols are:

1-Terminal block 3- Process block

2-Input / Output

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.

Note that the 5 basic flowcharting symbols are: 1-Terminal block 3- Process block 2-Input / Output
Note that the 5 basic flowcharting symbols are: 1-Terminal block 3- Process block 2-Input / Output
Note that the 5 basic flowcharting symbols are: 1-Terminal block 3- Process block 2-Input / Output
  • 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.

Note that the 5 basic flowcharting symbols are: 1-Terminal block 3- Process block 2-Input / Output
Note that the 5 basic flowcharting symbols are: 1-Terminal block 3- Process block 2-Input / Output

١٦

  • e. Only one flow line is used in conjunction with terminal symbol.

e. Only one flow line is used in conjunction with terminal symbol. f. Write within standard
e. Only one flow line is used in conjunction with terminal symbol. f. Write within standard
  • f. Write within standard symbols briefly. As necessary, you can use the annotation symbol to describe data or computational steps more clearly.

e. Only one flow line is used in conjunction with terminal symbol. f. Write within standard
  • 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?
Example 2.1
Draw flowchart to represent what will you do if your lamp doesn't work?

Answer:

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
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 YES Is it 6 O’clock ? NO YES Is it 6:30 O’clock ? Go
Get up
YES
Is it 6
O’clock ?
NO
YES
Is it 6:30
O’clock ?
Go to school on feet
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
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 loca on which we call number1.

Example 2.3 Draw flowchart to represent the process of reading two numbers, dividing them, and then

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.

Example 2.4 Draw a flowchart to find the sum of first 100 natural numbers. This means

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.

Example 2.5 Draw a flowchart to find the sum of the first 25 odd natural numbers.

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
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 N th term=

Example 2.6 Draw a flowchart to find the sum of all terms of the following series

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:

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

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

Now let's solve this problem by Assuming that the read numbers are : A=20, B=10, and

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.

Answer: Let's label the data items given to us in this problem with variable names as
Answer: Let's label the data items given to us in this problem with variable names as
Answer: Let's label the data items given to us in this problem with variable names as
Answer: Let's label the data items given to us in this problem with variable names as
Answer: Let's label the data items given to us in this problem with variable names as

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.

Solution for Example 2.8. Now let's solve this problem by assuming that the read numbers are

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.

In addition to that you have to read today’s date that can be stored at variable
In addition to that you have to read today’s date that can be stored at variable
In addition to that you have to read today’s date that can be stored at variable

Figure 2.12 Flowchart solution for example 2.9.

٣٠

Example 2.10 Draw a flowchart to sol ve an equation of second order given by: 0
Example 2.10
Draw a flowchart to sol ve an equation of second order given by:
0
Given that its roots
and are given by
,
√ 4
2
… … … … … … … … … … … … . 2 .1
where a, b, and c are re al numbers. You have to take into consi deration
the following:
1.
If
4 is n egative , you have to say that we have
c omplex
roots.
2.
If
4 is e qual to zero, the two roots are equal to – b/2a.
3.
If
( 4 ) g reater than zero, the solution will given b y equation
2.1.
٣١

Solution:

Figure 2.13 shows a Flowchart to solve this problem

Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
   
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example
Solution: Figure 2.13 shows a Flowchart to solve this problem Figure 2.13 Flowchart solution for example

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:

Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
   
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
   
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
 
   
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That
Assuming that the equation needed to be solved is given by 2 13 15 0 That

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.

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

Question 2:

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

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

Question 4:

°

5

9

° 32

Draw a flowchart that reads the radius of a sphere “r”, then it calculates

its volume “V” and surface area “A” using formulas

where

4

3

4

π

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.

٣٥