Title: MODULAR DESIGN
1- MODULAR DESIGN
- AND ABSTRACTION
2- SPECIFYING THE DETAILS OF A PROBLEM
- INTO A RELATED SET OF SMALLER
- PROBLEMS.
3TOP- DOWN DESIGN
- STATE THE PROBLEM
- SUBDIVIDE THE PROBLEM INTO MAJOR SUBPROBLEMS
- SUBDIVIDE EACH SUBPROBLEM INTO SMALLER TASKS
- REPEAT THE PROCESS UNTIL TASKS ARE EASILY
- SOLVED.
4THE STRUCTURE CHART
A GRAPHICAL REPRESENTATION OF SYSTEM COMPONENTS.
USED TO IDENTIFY AND DEFINE RELATIONSHIPS AMONG
SUBPROBLEMS.
5A STRUCTURE CHART
main
LEVEL O
LEVEL 1
module1
module3
module2
6STEPWISE REFINEMENT
- EACH SUBDIVISION IS REFERRED TO AS A
- STEPWISE REFINEMENT.
- TASKS AT EACH STAGE OF THE SUBDIVISION ARE
- REFERRED TO AS MODULES.
7PROBLEM SOLVING WITHTOP- DOWN DESIGN
- PROBLEM
- GET THREE INTEGER NUMBERS REPRESENTING
- TEST MARKS. COMPUTE AND DISPLAY THEIR AVERAGE.
8THE STRUCTURE CHART FOR PROGRAM FindAvg
FindAvg
LEVEL 0
LEVEL 1
Get Numbers
Display Results
Calculate Average
9THE ALGORITHM
- 1 GET test1, test2, test3
- 2 CALCULATE average
- 3 DISPLAY average
10NEW CONCEPTS
MODULES
11MODULAR DESIGN ANDFUNCTIONS
MODULES ARE CODED AS SUBPROGRAMS. A C
FUNCTION IS A GROUPING OF STATEMENTS INTO A
SINGLE UNIT THAT PERFORMS A TASK. EACH C
PROGRAM IS MADE UP OF ONE OR MORE FUNCTIONS
(main() IS A FUNCTION).
12FUNCTION PROTOTYPE
A FUNCTION MUST BE DECLARED BEFORE IT MAY BE
CALLED.
type function_name ()
// FINDS THE AVERAGE OF THREE NUMBERS void
Find_average ()
13FUNCTION CALLS
A FUNCTION IS CALLED THROUGH THE EXECUTION OF A
FUNCTION CALL. LOGIC FLOW IS TRANSFERRED TO
function_name.
function_name ()
// A CALL TO FUNCTION Find_average Find_average
()
14FUNCTION DEFINITION
A FUNCTION CONSISTS OF 2 PARTS THE HEADING AND
THE BODY. THE HEADING INCLUDES A TYPE
SPECIFICATION, FUNCTION NAME, AND LEFT AND RIGHT
PARENTHESES. THE BODY CONSISTS OF LOCAL
DECLARATIONS AND EXECUTABLE STATEMENTS. THE
FUNCTION BODY BEGINS WITH A LEFT BRACE AND ENDS
WITH A RIGHT BRACE.
15type function_name ( ) local_declarations execut
able_statements
heading
body
16EXAMPLE
void Find_average() // Calculates the
average const int NUMBER_OF_TESTS 3
average (test1 test2 test3) /
NUMBER_OF_TESTS return // end Find_average
17FLOW OF CONTROL
- FUNCTIONS ARE DEFINED PHYSICALLY AFTER THE MAIN
PROGRAM. EXECUTION STARTS WITH THE STATEMENTS OF
THE MAIN PROGRAM. - THE CALL STATEMENT ACTS AS TRANSFER OF CONTROL.
THE RETURN STATEMENT SENDS THE CONTROL BACK TO
THE CALLING MODULE.
18EXAMPLE
... void main () ... Get_data ()
Find_average () Display_results ()
return
void Find_average () // Calculates the
average const int NUMBER_OF_TESTS 3
average ( test1 test2 test3 ) /
NUMBER_OF_TESTS return // end
Find_average ()
19THE CODE FOR FindAvg
- // FILE FindAvg2.cpp
- // Finds the average of three test scores
- include ltiostream.hgt
- // global data
- float test1, test2, test3, average
- void main ()
-
- // functions used
- void Get_data ()
- void Find_average ()
- void Display_results ()
- // function calls
- Get_data ()
- Find_average ()
- Display_results ()
- return
20void Get_data () // Gets the grade marks
cout ltlt "Enter the three test marks " cin gtgt
test1 gtgt test2 gtgt test3 return void
Find_average() // Calculates the average
const int NUMBER_OF_TESTS 3 average (test1
test2 test3) / NUMBER_OF_TESTS
return void Display_results () //
Displays the average cout ltlt "The average of
the 3 test marks is " ltlt average ltlt endl
return
21LOCAL DECLARATIONS
OBJECTS DECLARED LOCALLY WITHIN A FUNCTION CAN BE
REFERENCED ONLY WITHIN THAT FUNCTION.
22GLOBAL DECLARATIONS
OBJECTS DECLARED OUTSIDE OF FUNCTIONS CAN BE
REFERENCED FROM WITHIN ANY FUNCTION THAT FOLLOWS
THAT DECLARATION.
23MODULE COMMUNICATION
- PARAMETERS ARE USED TO COMMUNICATE BETWEEN
MODULES. - THE CALLING MODULE PASSES VALUES TO THE CALLED
MODULE TO USE THEM IN PROCESSING. - THE CALLED MODULE RETURNS THE RESULTS TO THE
CALLING MODULE.
24Flow of control in the program 24
1-?Execution starts from the beginning of the
program and reaches the statement
2-4?func is called. Param, temFarenheit,
evaluated, copied into tempFahr. Then control is
transferred to called function.
5? execution in the called function
6 - 7? return type is double and assigned to
tempCelsius
8 ?Execution follow normally in the main.
25EXAMPLE
PROBLEM GIVEN THE RADIUS OF A CIRCLE, COMPUTE AND
DISPLAY THE AREA AND THE CIRCUMFERENCE.
26ANALYSIS AND SPECIFICATIONS IN ORDER TO COMPUTE
THE CIRCLES AREA AND CIRCUMFERENCE (PROBLEMS
OUTPUT), WE NEED THE RADIUS (PROBLEMS INPUT).
WE ALSO KNOW THAT THE AREA MAY BE CALCULATED AS
p x RADIUS2 AND THE CIRCUMFERENCE MAY BE
CALCULATED AS 2 x p x RADIUS. DATA
REQUIREMENTS CONSTANT p 3.14159 INPUT
RADIUS (FLOAT) OUTPUT AREA (FLOAT)
CIRCUMFERENCE (FLOAT) FORMULAE AREA
p RADIUS RADIUS
CIRCUMFERENCE 2 p RADIUS
27DESIGN HAVING DEFINED THE PROBLEMS INPUT AND
OUTPUT, WE MAY START IDENTIFYING THE SYSTEMS
COMPONENTS AND STEPS NECESSARY TO SOLVE THE
PROBLEM.
28STRUCTURE CHART
AreaCircm
LEVEL 0
LEVEL 1
Get radius
Display Result
Compute
29STRUCTURE CHART
AreaCircm
LEVEL 0
LEVEL 1
Get radius
Display Result
Compute
area
circum
LEVEL 2
30ALGORITHM
1. GET DATA 2. COMPUTE 3. DISPLAY RESULTS
31ALGORITHM A Second Level Refinement
1. GET DATA 2. COMPUTE 2.1 COMPUTE AREA 2.2
COMPUTE CIRCUMFERENCE 3. DISPLAY RESULTS
32NEW CONCEPTS
MODULES COMMUNICATION
33FUNCTION PROTOTYPEUSING PARAMETERS
type function_name (parameter_type_list)
// computes the area of a circle float
Compute_area (float r)
34FUNCTION CALLS
function_name (parameter_list)
// A call to function Compute_area() Compute_area
(radius)
35FUNCTION DEFINITIONUSING PARAMETERS
type function_name ( parameter_list) local_decla
rations executable statements
heading
body
36EXAMPLE
DEFINITION
// computes the area of a circle float
Compute_area (float r) // compute and return
the area return PI r r // end
Compute_area ()
37CALL
area Compute_area (radius)
38NAMING RULES
- SAME RULES AS WITH IDENTIFIERS.
- void Module_name (float first, float second,
- float third, float result)
- OR
- void Calculate_average (float test1, float test2,
- float test3, float
average) - THE SECOND MODULE NAME IS MORE MEANINGFUL.
39IMPLEMENTATION FOR AreaCircm
// FILE AreaCircm.cpp // FINDS AND PRINTS THE
AREA AND CIRCUMFERENCE OF A CIRCLE include
ltiostream.hgt include "CmptArCr.cpp"
//Compute_area and Compute_circum void main
() // functions used // COMPUTES THE AREA
OF A CIRCLE float Compute_area (float) //
COMPUTES THE CIRCUMFERENCE OF A CIRCLE float
Compute_circum (float) // local data float
radius // radius of circle float
area // area of circle float
circum // circumference of circle
40 // get radius cout ltlt "enter the circle
radius " cin gtgt radius // compute the
area of the circle area Compute_area
(radius) // compute the circumference of the
circle circum Compute_circum (radius)
cout ltlt "the area of the circle is " ltlt area ltlt
endl cout ltlt "the circumference of the circle
is " ltlt circum ltlt endl return
41// FILE CmptArCr.cpp // Modules to calculate the
area and // circumference of a circle. // global
data const float PI 3.14159 // COMPUTES THE
AREA OF A CIRCLE float Compute_area (float r)
// compute and return the area return PI r
r // end Compute_area ()
42// COMPUTES THE CIRCUMFERENCE OF A CIRCLE float
Compute_circum (float r) // compute and
return the circumference return 2.0 PI r
// end Compute_circum ()
43ACTUAL PARAMETERS
- THE PARAMETERS IN THE CALL STATEMENT ARE
REFERRED TO AS ACTUAL PARAMETERS. THE VALUE OF AN
ACTUAL PARAMETER IS PASSED AND USED BY THE
FUNCTION.
44FORMAL PARAMETERS
THE PARAMETERS IN THE FUNCTION HEADING ARE
REFERRED TO AS FORMAL PARAMETERS. A FORMAL
PARAMETER REPRESENTS THE ACTUAL VALUE PASSED TO
THE FUNCTION EACH TIME IT IS CALLED.
45LIBRARIES OF FUNCTIONS
PROVIDE ACCESS TO PREVIOUSLY WRITTEN FUNCTIONS
THAT PERFORM USEFUL DIAGNOSTICS, MANIPULATIONS,
COMPUTATIONS, ETC.
46EXAMPLES OF SOMEMATHEMATICAL FUNCTIONS
- result sqrt (16) 10
- RETURNS THE VALUE 40.0. THE SQUARE ROOT
- OF 16 IS CALCULATED, THEN THE VALUE
- IS MULTIPLIED BY 10.
- floor (3.8) 3
- RETURNS THE LARGEST INTEGER NOT GREATER THAN THE
PARAMETER. - fabs (-3.8) 3.8
- RETURNS THE ABSOLUTE VALUE.
47// computes the area of a circle float
Compute_area (float r) // compute and return
the area return PI r r // end
Compute_area ()
48FILE INPUT/OUTPUT
- DATA IS OFTEN PREPARED AND STORED INTO A FILE.
- WHEN THE PROGRAM EXECUTES, IT ACCESSES
- THE FILE TO READ THE DATA.
- THE RESULTS ARE WRITTEN TO THE SCREEN OR TO AN
OUTPUT FILE.
49sssssssssssssssss
50- CIS113
- Assignment 2
- Implement and document a program that has the
following Behavior - Your program should
- display on the screen a prompt for a Fahrenheit
temperature. - The user will enter numeric value from the
keyboard, which the program will read. - The will compute the Celsius temperature
equivalent to that Fahrenheit, and - display this Celsius temperature on the screen
along with descriptive text
51- A- Determine if each is a valid C identifier.
If it is not, give a reason. - 1- XRay 2- Jeremiah 3- X 4- nite 5- to day
- B- Classify each as an integer literal, a real
literal, or neither. If it is neither, give a
reason. - 1- 12 2- 3.98 3- 24E0 4- 0E0
- C- Determine if each is a valid string literal,
If it is not, give a reason. - 1- X 2- 123 3- \twas
- D- Write constant declarations to associate each
name with the specified literal. - 1- 1.25 with the name RATE
- 2- 1776 with the name YEAR , the letter F with
FEMALE, and a blank character with BLANK.