Anda di halaman 1dari 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.

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

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

### Let r = the radius of the circle  3

### 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 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
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 = 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
١٤

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

١٥

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

### 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? 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? 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 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. 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 N th 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.

٢٦

### variable names inside the flowchart symbols.     Figure 2.10 Flowchart solution for example 2.8.

٢٧

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

٢٩

### 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 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            Figure 2.13 Flowchart solution for example 2.10.

٣٢

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

٣٣

### Correct the Flowchart. ### Question 2: ٣٤

٣٥