Title: Design with Structure Charts
1Design withStructure Charts
2Design Process
- Problem solving and design should be done
independent of programming. - Code significantly clouds the design process.. It
is very difficult to see the design and address
design issues when coding - Design needs to take place in a mode which
minimizes code influence
34 Steps
1
2
Create a structure chart based on the
main item, NOT programming requirements
Modify structure chart based on the programming
requirements. Top-down design.
3
4
Write code to implement the top-down design. As
you mature this will be skipped by going to step
4.
Improve readabililty of the code by
incorporating functions.
4National Parcel Example
Example 2 Parcel Service Company. Emphasizes IF
notation for top down design. National Parcel
Service (NPS) specializes in nationwide delivery
of small packages. NPS will not accept any
packages whose largest dimension is greater than
3 feet or whose weight exceeds 50 pounds. The
charge for shipping a parcel is 0.75 plus an
amount based on package weight as follows
Weight (lb.) Rate ---------------------
-------- 20 or less 0.08 per lb. 40 or less
0.10 per lb. Over 40 0.15 per
lb. ----------------------------- There is an
additional 1.00 charge if the volume of the
package exceeds 18 cubic feet. Write a program
that will read the dimensions of a parcel (in
feet) and its weight (in pounds) and then compute
and print the postage due. If the package is
rejected, an appropriate message should be
printed. Example Test Data Length Width Depth
Weight 1.5 1.2 0.8 6.0
5Parcel Delivery
National Parcel Service
Parcel
Shipping Status
Weight
Size
Acceptable
Not Acceptable
Length
Width
Depth
Charge
Weight Charge
Volume Charge
0.75
Wtlt20
20ltWtlt40
Wtgt40
Vollt18
Volgt18
6Change in Perspective
- Our natural view of the problem domain may not
match the requirements. - There are multiple views of anything.
- Other views are not wrong, but do provide insight
into the structure we expect and perceive. - In this problem we need to adjust for a single
parcel.
7Parcel Delivery(adjust to one package)
Parcel
Shipping Status
Weight
Size
Acceptable
Not Acceptable
Length
Width
Depth
Charge
Weight Charge
Volume Charge
0.75
Wtlt20
20ltWtlt40
Wtgt40
Vollt18
Volgt18
8TV Survey
Example 3 Television Rating Service
Emphasizes WHILE notation for top down
design A television rating service makes a
survey of the viewing audience to sample the
popularity of tv shows. When a call is made
concerning a particular show, the sex and age of
the person called, as well as whether or not the
person watches the program regularly, are
recorded. Write a program that will process the
data gathered for the show. The total number of
people called, the number who said they watched
the show regularly, and the percentage of those
who watch the show on a regular basis should be
printed. The program should also print a table
showing the percentages of those who watch the
show by sex and age categories. The output table
should look something like the sample shown here.
SEX UNDER 18 18 to 35 36 to 55
OVER 55 ----------------------------------------
-------------------------- MALE 12.2
47.5 34.3 6.0 FEMALE
18.5 32.4 35.6
13.5 -----------------------------------------
-------------------------
9TV Survey
Survey
Call
Sex
Viewer Status
Age
Regular
Not Regular
Male
Female
Agelt18
18ltAgelt36
Agegt55
36ltAgelt55
10Requirements demandadjusting view
- Sex and age are typically related only through
the person, not as subcategories. - This problem views them as subcategories of one
another. - It doesnt make any difference which is a
subcategory of the other. - Next slide shows an adjusted view.
11TV Survey
Survey
Call
Viewer Status
Regular
Not Regular
Sex
Male
Female
Same as Male
Agelt18
18ltAgelt36
Agegt55
36ltAgelt55
12Step 2
- Adapt the structure chart
- to accommodate the
- program specifications
- DO A TOP-DOWN DESIGN
13Parcel DeliveryAdjusting Structure Chart for
Program (Top-Down) Design
Compute Postage
Determine Acceptability
Input Package Info
Not Acceptable
Acceptable
Weight
Size
Length
Width
Depth
Compute Charge
Output Charge
Output Rejection Message
Compute Weight Charge
Compute Volume Charge
Compute Total Charge
Charge Wt_Chrg Vol_Chrg 0.75
Wtlt20
20ltWtlt40
Wtgt40
Vollt18
Volgt18
Wt_chrg Wt0.08
Wt_chrg Wt0.1
Wt_chrg Wt0.15
Vol_chrg 0.0
Vol_chrg 1.0
14TV SurveyStructure Chart Conversion to Top Down
Design
Summarize Survey
Process Call
Initializations
Output Report
Process Regular Viewers
Calculate
Write Report
Input Call Info
Count of Calls
Regular
Not Regular
Count Regular Viewer
Sex
Male
Female
Same as Male but use WL18, WL36,...
Agelt18
18ltAgelt36
Agegt55
36ltAgelt55
Increment ML18
Increment ML36
Increment ML55
Increment MG55
15Step 3
- Write code to implement
- the design
16include ltiostream.hgt main () float
length,width,height,weight,WtChrg,
volume,VolChrg,TotalCharge // input package
information cout ltlt " Enter length, width and
height of package\n" cin gtgt length gtgt width
gtgt height cout ltlt " Enter weight of
package\n" cin gtgt weight // determine
acceptability if ( (lengthlt3) (heightlt3)
(widthlt3) (weightlt50)) //
ACCEPTABLE // COMPUTE CHARGE //
WEIGHT CHARGE if (weightlt20)
WtChrg 0.08weight else if (weightlt40)
WtChrg 0.10weight else
WtChrg 0.15weight // VOLUME
CHARGE volume lengthwidthheight
if (volume gt 18.0) VolChrg 1.0
else VolChrg 0.0 // TOTAL
CHARGE TotalCharge 0.75 WtChrg
VolChrg // OUTPUT CHARGE cout ltlt
"Total charge for the package is " ltlt
TotalCharge ltlt endl else // NOT
ACCEPTABLE cout ltlt "Package EXCEEDS
weight/size specs for shipment\n" cout ltlt
"...... Try another company!"
Parcel Delivery Implementation
17while (num_read lt NumberToRead)
// PROCESS CALL // input call
information cin gtgt sex gtgt age gtgt regular
// count number of calls tv
num_read // process regular viewer
if ( (regular 'R') (regular 'r'))
// count as a regular viewer
rv // tabulate age/sex info
if ( (sex 'M') (sex 'm'))
// process Male if (age lt
18) ml18 else
if (age lt 36) ml36
else if (age lt 55)
ml55 else
mg55 else
// process Female if (age lt
18) fl18 else
if (age lt 36) fl36
else if (age lt 55)
fl55 else
fg55 // loop
PercentRegularrv / tv // write reports to
output device // ........ // end
TV Survey Implementation
include ltiostream.hgt main () char sex,
regular float PercentRegular int age,
rv, tv, num_read, NumberToRead int
ml18,ml36,ml55,mg55,fl18,fl36,fl55,fg55 //
initializations tv0 rv0
ml180ml360ml550mg550
fl180fl360fl550fg550 cin gtgt
NumberToRead num_read 0
18Step 4
Use FUNCTIONS to implement the design
19Parcel Delivery Implementation w/functions
// FUNCTIONS int Acceptable(float
length, float height, float width,
float weight) return ( (lengthlt3)
(heightlt3) (widthlt3) (weightlt50) )
float WtChrg(float weight) float wc
if (weightlt20) wc 0.08weight
else if (weightlt40) wc 0.10weight
else wc 0.15weight return
wc float PackageVolume(float length, float
height, float width) return lengthheightwidth
float VolChrg(float v) float vcharge
if (v gt 18.0) vcharge 1.0 else
vcharge 0.0 return vcharge void
Reject() cout ltlt "Package EXCEEDS
weight/size specs for shipment" cout ltlt "
...... Try another company!"
include ltiostream.hgt // PROTOTYPES int
Acceptable(float length, float height, float
width, float weight) float
WtChrg(float weight) float PackageVolume(float
length, float height, float width) float
VolChrg(float v) void Reject() //
MAIN ROUTINE main () float length,width,height,
weight,volume,TotalCharge // input package
information cout ltlt " Enter length, width and
height of package\n" cin gtgt length gtgt width
gtgt height cout ltlt " Enter weight of
package\n" cin gtgt weight // determine
acceptability if ( Acceptable(length,height,wid
th,weight)) // ACCEPTABLE
volume PackageVolume(length,width,height)
TotalCharge 0.75 WtChrg(weight)
VolChrg(volume) cout ltlt "Total charge for
the package is " ltlt TotalCharge ltlt
endl else // NOT ACCEPTABLE
Reject()
20TV Survey Implementation w/functions
Its Your Turn!