Thursday, December 11, 2008

Introduction to Flowcharting

This appendix provides a brief introduction to flowcharting. It includes example flowcharts
for programs that appear in Chapters 1 through 6. In addition, a complete lesson
on flowcharting is included on the Student CD. The lesson is stored in a PowerPoint file
named
Flowcharting.ppt
.
A flowchart is a diagram that depicts the “flow” of a program. It contains symbols that
represent each step in the program. The figure shown here is a flowchart for Program 1-1,
the pay-calculating program in Chapter 1.
START
END
Display message
”How many
hours did
you work?”
Display message
”How much
do you get
paid per hour?”
Multiply hours
by payRate.
Store result in
grossPay.
Read hours
Read payRate
Display
grossPay
2
Appendix D: Introduction to Flowcharting
Notice there are three types of symbols in this flowchart: rounded rectangles (representing
terminal points), parallelograms (representing input/output operations), and a rectangle
(representing a process).
The rounded rectangles, or terminal points, indicate the flowchart’s starting and ending
points. The parallelograms designate input or output operations. The rectangle depicts a
process such as a mathematical computation, or a variable assignment. Notice that the
symbols are connected with arrows that indicate the direction of program flow.
Connectors
Sometimes a flowchart is broken into two or more smaller flowcharts. This is usually done
when a flowchart does not fit on a single page, or must be divided into sections. A connector
symbol, which is a small circle with a letter or number inside it, allows you to connect
two flowcharts.
In the figure below, the A connector indicates that the second flowchart segment begins
where the first flowchart segment ends.
Terminal Input/Output Processes
Operations
A
END
START A
A
Appendix D: Introduction to Flowcharting
3
Flowchart Structures
There are four general flowchart structures:

Sequence

Decision

Repetition

Case
A sequence structure is a series of actions or steps, performed in order. The flowchart for
the pay-calculating program is an example of a sequence structure. The following flowchart
is also a sequence structure. It depicts the steps performed in Program 2-20, from
Chapter 2.
Flowchart for Program 2-20
The following flowchart, which is another sequence structure, depicts the steps performed
in Program 3-15 (from Chapter 3). Notice the use of connector symbols to link the two
flowchart segments.
END
START
Display
Total Pay
Calculate Total
Wages as Regular
Wages plus
Overtime Wages.
Calculate Regular
Wages as Base
Pay times
Regular Hours.
Calculate Overtime
Wages as
Overtime Pay
times Overtime
Hours.
4
Appendix D: Introduction to Flowcharting
Flowchart for Program 3–15
The Decision Structure
In a decision structure, one of two possible actions is performed, depending on a condition.
In a decision structure, a new symbol, the diamond, represents a yes/no question. If
the answer to the question is “yes,” the program flow follows one path. If the answer to
the question is “no,” the program flow follows another path. The following figure shows
the general form of a decision structure.
START
END
Ask user to enter
beginning
inventory value
for all three stores.
Store begInv
in store1,
store2, and
store3.
Subtract sold
from store1.
Read begInv.
Read sold.
Ask user to enter
number of widgets
sold at Store 1.
A
A
Ask user to enter
number of widgets
sold at Store 2.
Subtract sold
from sold2.
Subtract sold
from store3.
Read sold.
Read sold.
Ask user to enter
number of widgets
sold at Store 3.
Display inventory
values for all
three stores.
Appendix D: Introduction to Flowcharting
5
In the following flowchart segment, the question “is x < y?” is asked. If the answer is
“no,” then process A is performed. If the answer is “yes,” then process B is performed.
The following flowchart depicts the logic of Program 4-8, from Chapter 4. The decision
structure shows that one of two possible messages is displayed on the screen, depending
on the value of the expression
number % 2
.
NO YES
NO
Process A Process B
YES
x < y?
6
Appendix D: Introduction to Flowcharting
Flowchart for Program 4–8
The Case Structure
In a case structure, one of several possible actions is taken, depending on the contents of a
variable. The following flowchart segment shows the general form of a case structure.
The following flowchart depicts the logic of Program 4–31, which is also from Chapter 4.
One of 4 possible paths is followed, depending on the value stored in the variable
feedGrade
.
NO
Display
”Number is
odd.”
number % 2
equal to 0?
Read number.
Ask user to enter
an integer.
Display
”Number is
even.”
YES
START
END
Appendix D: Introduction to Flowcharting
7
Flowchart for Program 4–31
Repetition Structures
A repetition structure represents part of the program that repeats. This type of structure
is commonly known as a loop. The following figure shows an example of a repetition
structure.
Notice the use of the diamond symbol. A repetition structure tests a condition, and if the
condition exists, it performs an action. Then it tests the condition again. If the condition
still exists, the action is repeated. This continues until the condition no longer exists. In
the flowchart segment above, the question “is x < y?” is asked. If the answer is yes, then
Start
Ask the user to
enter the desired
feed grade.
Read input into
feedGrade.
End
'a'
'A'
'b'
'B'
'c'
'C' Other
case feedGrade
Display "30 cents
per pound".
Display "20 cents
per pound".
Display "15 cents
per pound".
Display "That is an
invalid choice".
YES
x < y? Process A
8
Appendix D: Introduction to Flowcharting
Process A is performed. The question “is x < y?” is asked again. Process A is repeated as
long as x is less than y. When x is no longer less than y, the repetition stops and the structure
is exited.
There are two forms of repetition structure: pre-test and post-test. A pre-test repetition
structure tests its condition
before
it performs an action. The flowchart segment above
shows a pre-test structure. Notice that Process A does not execute at all if the condition “x
< y” is not true. The pre-test repetition structure is coded in C++ as a
while
loop.
A post-test repetition structure tests its condition
after
it performs an action. This type of
loop always performs its action at least once. The following flowchart segment shows an
example.
The post-test repetition structure is coded in C++ as a
do-while
loop.
The following flowchart depicts the logic of Program 5-6, which appears in Chapter 5.
Flowchart for Program 5-6
YES
NO
Process A
x < y?
START
number = 1.
Display Table
Headings.
Display number
and number
Squared.
number <=
10?
YES
NO
Add 1 to number.
END
Appendix D: Introduction to Flowcharting
9
Modules
A program module (such as a function in C++) is represented by the special symbol shown
below:
The position of the module symbol indicates the point the module is executed, as shown in
the following figure:
A separate flowchart can then be constructed for the module. The following figure shows
a flowchart for Program 6-19 from Chapter 6. The
getBasePay
and
getOvertimePay
modules appear as separate flowcharts.
START
END
Read Input.
Call calc_pay
function.
Display results.
10
Appendix D: Introduction to Flowcharting
Flowchart for Program 6-19
Start
Ask the user to
enter the number
of hours worked.
hours >
BASE_HOURS?
Call
getBasePay
Yes
Call
getOvertimePay
Calculate total pay.
No
Display base pay,
overtime pay, and total
pay.
End
Start of
getBasePay
No hours > Yes
BASE_HOURS?
basePay =
BASE_HOURS *
PAY_RATE
basePay =
hoursWorked *
PAY_RATE
End of
getBasePay
Start of
getOvertimePay
No hours > Yes
BASE_HOURS?
overtimePay = (hoursWorked
- BASE_HOURS) *
PAY_RATE *
OT_MULTIPLIER
overtimePay = 0.0
End of
getOvertimePay

Using UML in Class Design

When designing a class it is often helpful to draw a UML diagram.
UML
stands for
Unified
Modeling Language
. The UML provides a set of standard diagrams for graphically
depicting object-oriented systems. Figure E-1 shows the general layout of a UML diagram
for a class. Notice that the diagram is a box that is divided into three sections. The top
section is where you write the name of the class. The middle section holds a list of the
class’s member variables. The bottom section holds a list of the class’s member functions.
For example, in Chapter 13, Introduction to Classes, you studied a
Rectangle
class that
could be used in a program that works with rectangles. The class has the following member
variables:

width

length
The class also has the following member functions:

setWidth

setLength

getWidth

getLength

getArea
Figure E-1
Class name goes here
Member variables are listed here
Member functions are listed here
2
Appendix E: Using UML in Class Design
From this information alone we can construct a simple UML diagram for the class, as
shown in Figure E-2.
The UML diagram in Figure E-2 tells us the name of the class, the names of the member
variables, and the names of the member functions. Compare this diagram to the actual
C++ class declaration, which follows.
class Rectangle
{
private:
double width;
double length;
public:
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
double getArea() const;
};
The UML diagram in Figure E-2 does not convey many of the class details, such as access
specification, member variable data types, parameter data types, and function return
types. The UML provides optional notation for these types of details.
Showing Access Specification in UML Diagrams
The UML diagram in Figure E-2 lists all of the members of the
Rectangle
class but does
not indicate which members are private and which are public. In a UML diagram you may
optionally place a
-
character before a member name to indicate that it is private, or a
+
character to indicate that it is public. Figure E-3 shows the UML diagram modified to
include this notation.
Figure E-2
Rectangle
width
length
setWidth()
setLength()
getWidth()
getLength()
getArea()
Appendix E: Using UML in Class Design
3
Data Type and Parameter Notation in UML Diagrams
The Unified Modeling Language also provides notation that you may use to indicate the
data types of member variables, member functions, and parameters. To indicate the data
type of a member variable, place a colon followed by the name of the data type after the
name of the variable. For example, the
width
variable in the
Rectangle
class is a
double
.
It could be listed as follows in the UML diagram:
- width : double
The return type of a member function can be listed in the same manner: After the function’s
name, place a colon followed by the return type. The
Rectangle
class’s
getLength
function returns a
double
, so it could be listed as follows in the UML diagram:
+ getLength() : double
Parameter variables and their data types may be listed inside a member function’s parentheses.
For example, the
Rectangle
class’s
setLength
function has a
double
parameter
named
len
, so it could be listed as follows in the UML diagram:
+ setLength(len : double) : void
Figure E-4 shows a UML diagram for the
Rectangle
class with parameter and data type
notation.
Figure E-3
NOTE:
In UML notation the variable name is listed first, then the data type. This is
opposite of C++ syntax, which requires the data type to appear first.
Rectangle
- width
- length
+ setWidth()
+ setLength()
+ getWidth()
+ getLength()
+ getArea()
4
Appendix E: Using UML in Class Design
Showing Constructors and Destructors in a UML Diagram
There is more than one accepted way of showing a class’s constructor in a UML diagram.
In this text we show a constructor just as any other method, except we list no return type.
Figure E-5 shows a UML diagram for the first version of the
InventoryItem
class, which
was discussed in Chapter 13. This version of the class has one constructor, which accepts
three arguments.
Figure E-6 shows a UML diagram for the second version of the
InventoryItem
class,
which has three overloaded constructors, as well as some additional member functions.
Figure E-4
Figure E-5
Rectangle
- width : double
- length : double
+ setWidth(w : double) : void
+ setLength(len : double) : void
+ getWidth() : double
+ getLength() : double
+ getArea() : double
InventoryItem
- description : char*
- cost : double
- units : int
+ InventoryItem(desc : char*,
c : double, u : int) :
+ ~Inventory() :
+ getDescription() : char*
+ getCost() : double
+ getUnits() : int
Appendix E: Using UML in Class Design
5
Aggregation UML Diagrams
Aggregation occurs when a class contains an instance of another class as a member. You show
aggregation in a UML diagram by connecting two classes with a line that has an open diamond
at one end. The diamond is closest to the class that contains instances of other classes.
For example, suppose we have the
PersonalInfo
class shown in Figure E-7, which holds
information about a person.
Figure E-6
Figure E-7
InventoryItem
- description : char*
- cost : double
- units : int
+ InventoryItem():
+ InventoryItem(desc : char*) :
+ InventoryItem(desc : char*,
c : double, u : int) :
+ ~Inventory() :
+ setDescription(d : char*) : void
+ setCost(c : double) : void
+ setUnits(u : int) : void
+ getDescription() : char*
+ getCost() : double
+ getUnits() : int
- customerName : char*
- customerAddress : char*
- customerCity : char*
- customerState : char*
- customerZip : char*
+ PersonalInfo(name : char*,
address : char*,
city : char*,
state : char*,
zip : char*) :
+ getName() : char*
+ getAddress() : char*
+ getCity() : char*
+ getState() : char*
+ getZip() : char*
PersonalInfo
6
Appendix E: Using UML in Class Design
Suppose we also have the
BankAccount
class shown in Figure E-8, which holds the balance
of a bank account, and can perform operations such as making deposits and withdrawals.
Figure E-9 shows a UML diagram for another class,
BankCustomer
, which contains
instances of the
PersonalInfo
and
BankAccount
classes as members. The relationship
between the classes is shown by the connecting lines with the open diamond. The open
diamond is closest to the
BankCustomer
class because it contains instances of the other
classes as members.
Inheritance in UML Diagrams
You show inheritance in a UML diagram by connecting two classes with a line that has an
open arrowhead at one end. The arrowhead points to the base class. For example,
Figure E-10 shows a UML diagram depicting the relationship between the
GradedActivity
and
FinalExam
classes that you studied in Chapter 15. The arrowhead points toward the
GradedActivity
class, which is the base class.
Showing Protected Members
Protected class members may be denoted in a UML diagram with the
#
symbol. In the second
version of the
GradedActivity
class, in Chapter 15, the
score
member variable was
declared
protected
. Figure E-11 shows a UML diagram for the class.
Figure E-8
BankAccount
- balance : double
- interestRate : double
- interest : double
+ BankAccount(startBalance : double,
intRate : double) :
+ deposit(amount : double) : void
+ withdraw(amount : double) : void
+ addInterest() : void
+ getBalance() : double
+ getInterest() : double
Appendix E: Using UML in Class Design
7
Figure E-9
- info : PersonalInfo
- checking : BankAccount
- savings : BankAccount
+ BankCustomer(i : PersonalInfo,
c : BankAccount,
s : BankAccount) :
+ getCheckingBalance() : double
+ getSavingsBalance() : double
+ checkingDeposit(amt : double)
: void
+ checkingWithdrawal(amt : double)
: void
+ savingsDeposit(amt: double) : void
+ savingsWithdrawal(amt : double)
: void
BankCustomer
BankAccount
- balance : double
- interestRate : double
- interest : double
+ BankAccount(startBalance : double,
intRate : double) :
+ deposit(amount : double) : void
+ withdraw(amount : double) : void
+ addInterest() : void
+ getBalance() : double
+ getInterest() : double
- customerName : char*
- customerAddress : char*
- customerCity : char*
- customerState : char*
- customerZip : char*
+ PersonalInfo(name : char*,
address : char*,
city : char*,
state : char*,
zip : char*) :
+ getName() : char*
+ getAddress() : char*
+ getCity() : char*
+ getState() : char*
+ getZip() : char*
PersonalInfo
8
Appendix E: Using UML in Class Design
Figure E-10
Figure E-11
- score : double
+ GradedActivity() :
+ GradedActivity(s : double) :
+ setScore(s : double) : void
+ getScore() : double
+ getLetterGrade() : char
GradedActivity
- numQuestions : int
- pointsEach : double
- numMissed : int
+ FinalExam() :
+ FinalExam(questions : int,
missed : int) :
+ set(questions : int,
missed : int) : void
+ getNumQuestions() : double
+ getPointsEach() : double
+ getNumMissed() : int
FinalExam
# score : double
+ GradedActivity() :
+ GradedActivity(s : double) :
+ setScore(s : double) : void
+ getScore() : double
+ getLetterGrade() : char
GradedActivity