Presentation is loading. Please wait.

Presentation is loading. Please wait.

COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.

Similar presentations


Presentation on theme: "COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot."— Presentation transcript:

1 COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot Koffman, Course lecturer: Assoc. Prof. Stoyan Bonev, PhD

2 Lecture 7: Top-Down Design with Functions

3 3 Lecture Contents: t Top-Down Design t Step-wise refinement and structure charts t Functions topics: –Prototype statement, –Call statement, –Function Definition. t Functions without arguments.

4 4 Top-Down design There exist problems whose solving algorithms are too complicated, more complex than those already solved. Then developer breaks up problem into sub problems to solve and develop program solution. If necessary some sub problems are to be broken up into sub sub problems and so on. The process described is called Top-Down Design. Illustration based on real problems: to draw figures.

5 5 Top-Down design Solving problems or creating programs is possible without applying the top-down design approach. Ignoring the top-down design approach results in a program composed of one only main() function full of executable statements. This is a bad, non structured, non flexible style of programming which is not recommended. It is known as “spaghetti code” programming.

6 6 Top-Down design The process described is called Top-Down Design. Illustration based on concrete problems: t To draw simple diagrams on screen: –To draw a house –To draw a stick figure of a lady

7 7 Top-Down design – Analysis House is composed of:triangle parallel lines base line Triangle is composed of:intersecting lines base line We need three basic components (building blocks): parallel lines, base line, intersecting lines

8 8 To draw a house using spaghetti code programming void main() { cout <

9 9 Top-Down design – Design To create a house problem is divided into 3 sub problems: House is composed of: a triangle, parallel lines, base line Triangle is not a building block. It is to be divided into 2 sub sub problems: intersecting lines, base line We need three basic components (building blocks): parallel lines,base line,intersecting lines

10 10 Top–Down design of a house House Problem TriangleParallel_lines Base_line Sub-problems Intersect_lines Base_line Sub-sub-problems The figure shown is called a structure chart.

11 11 Top-Down design – Analysis Stick figure is composed of:circle triangle intersecting lines Triangle is composed of:intersecting lines base line We need totally four basic components (building blocks): parallel lines,base line,intersecting lines, circle

12 12 Introduction to the Function concept

13 13 Top-Down design – Implementation We need a program feature (module, unit, procedure, sub program, routine) to do the job for the basic components, specified as leaves of the tree for the structure chart. Such a program feature in C++ is called a function.

14 14 Top-Down design – Implementation Introduction To functions without arguments and no return value

15 15 Top-Down design – Implementation Function implementing 1 st basic component (solving a sub sub problem) “to draw a base line” void DrawBase(void) { cout << endl << “ ”; }

16 16 Top-Down design – Implementation Function implementing 2 nd basic component (solving a sub sub problem) “to draw parallel lines” void DrawParallel(void) { cout << endl << “| |”; }

17 17 Top-Down design – Implementation Function implementing 3 rd basic component (solving a sub sub problem) “to draw intersecting lines” void DrawIntersect(void) { cout << endl << “ /\\ “; cout << endl << “/ \\”; }

18 18 Top-Down design – Implementation Functions implementing all basic components void DrawParallel(void)// draw parallel lines { cout << endl << “| |”; } void DrawBase(void)// draw a base line { cout << endl << “ ”; } void DrawIntersect(void)// draw intersecting lines { cout << endl << “ /\\ “; cout << endl << “/ \\”; }

19 19 Top-Down design – Further Implementation Function to draw a triangle (solving a sub problem). It is based on calling (activating) functions for basic components. void DrawTriangle(void) { DrawIntersect(); DrawBase(); }

20 20 Top-Down design – Final Implementation Function to draw a house (solving a problem). It is based on calling (activating) functions for basic components (sub sub problems) and functions (solving sub problem) void DrawHouse(void) { DrawTriangle(); DrawParallel(); DrawBase(); }

21 21 Top-Down design – the main() function #include using namespace std; … void main() //source text of function main { DrawHouse(); } …

22 22 Why to use functions? See next two slides Problem: Draw a house #include using namespace std; void main() //source text of function main { DrawHouse(); }...

23 23 Why to use functions? Problem: Draw two houses. Solution: We add one only more statement to duplicate the function call #include using namespace std; void main()// source text of function main { DrawHouse(); }...

24 24 Why to use functions? Problem: Draw three houses. Solution: We add two more statements to triple the function call #include using namespace std; void main()// source text of function main { DrawHouse(); }...

25 25 Details on functions Three important topics when dealing with functions:  prototype (signature) statement  function call statement  function definition

26 26 Example Function prototype: void DrawHouse(void); Function call: DrawHouse(); Function definition: void DrawHouse(void) { DrawTriangle(); DrawParallel(); DrawBase(); }

27 27 The entire program – have a look at the handout #include using namespace std; void DrawHouse(void); void DrawTriangle(void); void DrawParallel(void); void DrawBase(void); void DrawIntersect(void); void main(){ DrawHouse();} void DrawHouse(void){DrawTriangle(); DrawParallel(); DrawBase();} void DrawTriangle(void){ DrawIntersect(); DrawBase();} void DrawParallel(void){ cout<<“\n| |”; cout<< “\n| |”;} void DrawBase(void){ cout << “\n ”;} void DrawIntersect(void){ cout<< “\n /\\“;cout<<“\n / \\ “; cout<< “\n/ \\”; }

28 28 Two important topics t Placement of Functions in a Program t Order of Execution of Functions

29 29 Placement of Functions in a Program The previous slide shows the complete program with function subprograms. The subprogram prototypes precede the main() function (after any #include directives) and the subprogram definitions follow the main() function. The relative order of the function definitions does not affect their order of execution; that is determined by the order of execution of the function call statements.

30 30 Order of Execution of Functions The prototypes for the function subprograms appear before main() function, so the compiler processes the function prototypes before it translates the main() function. The information in each prototype lets the compiler correctly translate a call to that function. The compiler translates a function call statement as a transfer of control (or unconditional branch) to that function. After compiling the main() function, the compiler translates each function subprogram. During translation, when the compiler reaches the end of the function body, it inserts a machine language statement that causes a transfer of control (again unconditional branch) back from the function to the calling statement.

31 31 Summary on functions Prototype: void fname(void); Call: fname( ); Definition: void fname(void) { }

32 32 Conclusion on functions: Why to use functions? (two important reasons) 1. Dividing a program into functions is one of the major principles of structured programming. 2. Using functions results in reduced program size.

33 33 Valid reasons to create a routine: t Reduce complexity –“Properly designed functions permit to ignore how a job’s done. Knowing what is done is sufficient.” B.Kernighan & D.Ritchie –“A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. ” B.Kernighan & D.Ritchie t Avoid duplicate code –Usually functions are specified to be called many times.

34 34 Exercise 7.1 Using functions Build and run a program: To draw a stick figure

35 35 Exercise 7.2 Using functions Build and run a program: to display block letters. Write a program to display the initial letter of your first name as a block letter using a function like this: void print_x(void) {... }

36 36 Before lecture end Lecture: Top-Down Design using Functions More to read: Friedman/Koffman, Chapter 03

37 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Top-Down Design with Functions and Classes Problem Solving, Abstraction, and Design using C++ 5e by Frank L. Friedman and Elliot B. Koffman

38 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Building Programs with Existing Information Analysis and design phases provide much information to help plan and complete a program Can start with data requirements to develop constant and variable declarations Use the algorithm as a first step in coding executable statements

39 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 39 Case Study: Finding the Area and Circumference of a Circle Problem statement Get the radius of a circle. Compute and display the circle’s area and circumference. Analysis –input is the circle’s radius –need calculation for the area –need calculation for the circumference

40 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 40 Case Study: Data Requirements Problem Constant PI = Problem input float radius// radius of a circle Problem output float area // area of a circle float circum // circumference of a circle

41 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 41 Case Study: Formulas Area of a circle =   radius 2 Circumference of a circle = 2    radius

42 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 42 Case Study: Design - Algorithm 1. Get the circle radius 2. Compute the area of circle 3. Compute the circumference of circle 4. Display area and circumference

43 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 43 Case Study: Design - Algorithm 1. Get the circle radius 2. Compute the area of circle 2.1 Assign PI * radius * radius to area 3. Compute the circumference of circle 3.1 Assign 2 * PI * radius to circum 4. Display area and circumference

44 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 44 Listing 3.2 Outline of area and circumference program

45 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 45 Listing 3.3 Finding the area and circumference of a circle

46 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 46 Listing 3.3 Finding the area and circumference of a circle (continued)

47 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 47 Case Study: Testing Radius of 5.0 Should get area of … Should get circumference of

48 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 48 Case Study: Weight of Flat Washers Problem statement You work for a hardware company that manufactures flat washers. To estimate shipping costs, you company needs a program that computes the weight of a specified quantity of flat washers.

49 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 49 Case Study: Weight of Flat Washers Analysis –flat washer is like a small donut –need to know rim area, thickness, density –rim area will be computed from knowing the washer’s outer and inner diameters.

50 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 50 Case Study: Data Requirements Problem Constant PI = Problem inputs float holeDiameter // diameter of hole float edgeDiameter // diameter of outer edge float thickness// thickness of washer float density// density of material used float quantity// number of washers made

51 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 51 Case Study: Data Requirements Problem output float weight// weight of batch of washers Program Variables float holeRadius// radius of hole float edgeRadius// radius of outer edge float rimArea// area of rim float unitWeight// weight of 1 washer

52 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 52 Case Study: Formulas Area of circle =   radius 2 Radius of circle = diameter / 2 Rim area = area of outer circle - area of hole Unit weight = rim area  thickness  density

53 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 53 Listing 3.4 Washer program

54 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 54 Listing 3.4 Washer program (continued)

55 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 55 Case Study: Testing Input data inner diameter of 1.2 outer diameter of 2.4 thickness of 0.1 material density of 7.87 quantity in batch of 1000 Should produce expected weight of batch of grams

56 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Top-Down Design and Structure Charts Top-down design –process to break down complex problem into smaller, simpler subproblems –similar to development of an algorithm Structure chart –graphical representation of relationship of subproblems

57 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 57 Case Study: Simple Figures Problem statement Draw some simple diagrams on the screen, e.g. a house and a female stick figure. Analysis –house is formed by displaying a triangle without its base, on top of a rectangle –stick figure consists of a circular shape, a triangle, and a triangle without its base. –4 basic shapes: circle, base line, parallel lines, intersecting lines

58 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 58 Figure 3.4 House and stick figure

59 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 59 Case Study: Design - Algorithm (no real data involved, so skip Data Requirements) For stick figure: 1.Draw a circle. 2.Draw a triangle. 3.Draw intersecting lines.

60 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 60 Case Study: Design - Algorithm (no real data involved, so skip Data Requirements) For stick figure: 1.Draw a circle. 2.Draw a triangle. 2.1 Draw intersecting lines. 2.2 Draw a base line. 3.Draw intersecting lines.

61 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 61 Case Study: Structure Chart Original Problem Detailed subproblems Level 0 Level 1 Level 2 Subproblems

62 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Functions without Arguments Functions important part of top-down design main( ) is a function called by the OS Form of call:fname( ); Example:drawCircle( ); Interpretation:the function fname is activated. After fname finishes execution, the program statement that follows the function call will be executed next.

63 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 63 Some Notes on Functions Don’t need to know details about how a function is implemented to know how to call. E.g. y = sqrt(x);// don’t know how sqrt implemented Do know how function is used (called). Empty parentheses indicate no arguments (more on this later).

64 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 64 Function Prototype Declares the function to the compiler Appears before function main. Tells compiler the function’s type, its name, and information about arguments.

65 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 65 Function Prototype (no Arguments) Form:ftype fname( ); Example:void skipThree( ); Interpretation: identifier fname is declared to be the name of a function. The identifier ftype specifies the data type of the function result. Ftype of void indicates the function does not return a value.

66 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 66 Function Definitions Specifies the function’s operations Function header similar to function prototype Function body contains declarations for local variables and constants and executable statements Prototypes precede main function (after #include directives) Function definitions follow main function

67 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 67 Function Definition (no arguments) Syntax:ftype fname( ) { local declarations executable statements }

68 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 68 Function Definition (no arguments) Example: // Displays block-letter H void printH( ) { cout << “** **” << endl; cout << “******” << endl; cout << “** **” << endl; }// end printH

69 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 69 Order of Execution int main( ) { drawCircle( ); drawTriangle( ); drawIntersect( ); return 0; } void drawCircle( ) { cout << “ * “ << endl; cout << “ * * “ << endl; //return to calling function } // end drawCircle

70 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 70 Notes on Function Execution Execution always begins at first statement of main function When a function is called –space is set aside for function variables –statements of function are executed –control returns to statement following call –function ends with space released

71 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 71 Function Advantages Team assignments on large project Simplify tasks Each function is a separate unit Top-down approach Reuse (e.g. drawTriangle) Procedural abstraction Information hiding

72 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 72 Displaying User Instructions Simple functions (no arguments, no return value) are of limited use Useful for displaying instructions to user E.g. void instruct( );// prototype … instruct( );//function call

73 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 73 // Displays instructions to user of area/circumference program void instruct () { cout << "This program computes the area and " << endl; cout << "circumference of a circle. " << endl << endl; cout << "To use this program, enter the radius of the " << endl; cout << "circle after the prompt" << endl; cout << "Enter the circle radius: " << endl << endl; cout << "The circumference will be computed in the same” << endl; cout << "units of measurement as the radius. The area " << endl; cout << "will be computed in the same units squared." << endl << endl; } Figure 3.10 Function instruct

74 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 74 Functions with Input Arguments Functions used like building blocks Build systems one function at a time –E.g. stereo components Use function arguments to carry information into function subprogram (input arguments) or to return multiple results (output arguments)

75 75 Thank You For Your Attention!


Download ppt "COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot."

Similar presentations


Ads by Google