Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions

Similar presentations


Presentation on theme: "CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions"— Presentation transcript:

1 CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
(Chapter 3) Lecturer: Narong Wesnarat

2 Building Programs from Existing Information and programs
Programs are often developed from existing solution(s) to other problems or other programs write a new program by editing source code of an existing program can safe time in typing necessary parts of the program, for examples Preprocessor directives and macros Constant and variable declarations #include <stdio.h> /* printf, scanf definitions */ #define PI /* The constant PI */ int main(void) { double x,y,z; int i,j,k; these statements are common to all programs

3 Building Programs from Existing Information and programs
System documentation during software development phase are useful and may be reused description of a problem’s data requirement solution algorithms Initial algorithm and its refinements can be used as program comments int main(void) { double miles; /* input - distance in miles. */ double kms; /* output - distance in kilometers */ /* Get the distance in miles */ ………………. insert C statement(s) here /* Convert the distance to kilometers */ /* Distance in kilometers is 1.609 * distance in miles */ /* Display the distance in kilometers */ return (0);

4 Case Study: Finding the Area and Circumference
Problem Get the radius of a circle. Compute and display the circled’s area and circumference. ANALYSIS Clearly, the input is the circle’s radius Two outputs are required the circle’s area the circumference These variable should be type double because input/outputs may contain fraction parts Data Requirements Problem Constant PI Problem Input radius /* radius of a circle */ Problem Outputs area /*area of a circle */ circum /* circumference of a circle */ Relevant Formulas area of a circle = ¶ x radius 2 circumference of a circle = 2 ¶ x radius

5 Case Study: Finding the Area and Circumference of a Circle
Design Initial Algorithm get the circle radius calculate the area calculate the circumference display the area and the circumference Algorithm Refinements Step 2 Refinement 2.1 Assign PI * radius * radius to area Step 3 Refinement 3.1 Assign 2* PI * radius to circum.

6 Case Study: Finding the Area and Circumference of a Circle
IMPLEMENTATION Figure 3.2 Outline of Program Circle /* * Calculates and displays the area and circumference of a circle */ #include <stdio.h> #define PI int main(void) { double radius; /* input - radius of a circle */ double area; /* output - area of a circle */ double circum; /* output - circumference */ /* Get the circle radius */ /* Calculate the area */ /* Assign PI * radius * radius to area. */ /* Calculate the circumference */ /* Assign 2 * PI * radius to circum. */ /* Display the area and circumference */ return (0);

7 Case Study: Finding the Area and Circumference
Figure 3.3 Calculating the Area and the Circumference of a Circle /* * Calculates and displays the area and circumference of a circle */ #include <stdio.h> #define PI int main(void) (continued) Figure 3.3 (continued) { double radius; /* input - radius of a circle */ double area; /* output - area of a circle */ double circum; /* output - circumference */ /* Get the circle radius */ printf("Enter radius> "); scanf("%lf", &radius); /* Calculate the area */ area = PI * radius * radius; /* Calculate the circumference */ circum = 2 * PI * radius; /* Display the area and circumference */ printf("The area is %.4f\n", area); printf("The circumference is %.4f\n", circum); return (0); } Sample Run: Enter radius> 5.0 The area is The circumference is

8 Case Study: Computing the Weight of a Batch of a Flat Watchers
Problem You work for a hardware company that manufactures flat washers. To estimate shipping costs, your company needs a program that computes the weights of a specified quantity of flat washers.

9 Case Study: Computing the Weight of a Batch of a Flat Watchers
ANALYSIS To compute the weight of a single flat washers you need to know its rim area its thickness the density of material Data Requirements Problem Constant PI Problem Inputs double hole_diameter; /* input - diameter of hole */ double edge_diameter; /* input - diameter of outer edge */ double thickness; /* input - thickness of washer */ double density; /* input - density of material used */ double quantity; /* input - number of washers made */

10 Case Study: Computing the Weight of a Batch of a Flat Watchers
Problem Outputs double weight; /* output - weight of washer batch */ Program Variables double hole_radius; /* radius of hole */ double edge_radius; /* radius of outer edge */ double rim_area; /* area of rim */ double unit_weight; /* weight of 1 washer */ Relevant Formulas area of a circle = ¶ x radius 2 radius of a circle = diameter/2 rim area = area of outer circle – area of hole unit weight = rim area x thickness x density

11 Case Study: Computing the Weight of a Batch of a Flat Watchers
Design Initial Algorithm Get the washer’s inner diameter, outer diameter, thickness Get the material density and quantity of washers manufactured Compute the rim area Compute the weight of one flat washer Computer the weight of the batch of washers Display the weight of the batch of washers Step 3 Refinement 3.1 Compute hole_radius and edge_radius 3.2 rim_area is PI * edge_radius * edge_radius – PI * hole_radius * hole_radius Step 4 Refinement 4.1 unit_weight is rim_area * thichness * density

12 Case Study: Computing the Weight of a Batch of a Flat Watchers
Figure 3.5 Flat Washer Program /* Computes the weight of a batch of flat washers. */ #include <stdio.h> #define PI int main(void) { double hole_diameter; /* input - diameter of hole */ double edge_diameter; /* input - diameter of outer edge */ double thickness; /* input - thickness of washer */ double density; /* input - density of material used */ double quantity; /* input - number of washers made */ double weight; /* output - weight of washer batch */ double hole_radius; /* radius of hole */ double edge_radius; /* radius of outer edge */ double rim_area; /* area of rim */ double unit_weight; /* weight of 1 washer */ /* Get the inner diameter, outer diameter, and thickness.*/ printf("Inner diameter in centimeters> "); scanf("%lf", &hole_diameter); printf("Outer diameter in centimeters> "); scanf("%lf", &edge_diameter); printf("Thickness in centimeters> "); scanf("%lf", &thickness); Implementation

13 Implementation (cont.)
Case Study: Computing the Weight of a Batch of a Flat Watchers /* Get the material density and quantity manufactured. */ printf("Material density in grams per cubic centimeter> "); scanf("%lf", &density); printf("Quantity in batch> "); scanf("%lf", &quantity); /* Compute the rim area. */ hole_radius = hole_diameter / 2.0; edge_radius = edge_diameter / 2.0; rim_area = PI * edge_radius * edge_radius - PI * hole_radius * hole_radius; /* Compute the weight of a flat washer. */ unit_weight = rim_area * thickness * density; /* Compute the weight of the batch of washers. */ weight = unit_weight * quantity; /* Display the weight of the batch of washers. */ printf("\nThe expected weight of the batch is %.2f", weight); printf(" grams.\n"); return (0); } Implementation (cont.) Inner diameter in centimeters> 1.2 Outer diameter in centimeters> 2.4 Thickness in centimeters> 0.1 Material density in grams per cubic centimeter> 7.87 Quantity in batch> 1000 The expected weight of the batch is grams.

14 3.2 Library Functions Predefined Functions and Code Reuse
A Goal of Software Engineering write error-free code Code Reuse, reusing program fragments that have already been written and tested whenever possible. Promotion of Code Reuse in C predefined functions C’s standard math library Example: a function sqrt( ) performs the square root computation y = sqrt(x); function name function call argument

15 Fig. 3.7 Square Root Program
/* * Performs three square root computations */ #include <stdio.h> /* definitions of printf, scanf */ #include <math.h> /* definition of sqrt */ int main(void) { double first, second, /* input - two data values */ first_sqrt, /* output - square root of first */ second_sqrt, /* output - square root of second */ sum_sqrt; /* output - square root of sum */ /* Get first number and display its square root. */ printf("Enter the first number> "); scanf("%lf", &first); first_sqrt = sqrt(first); printf("The square root of the first number is %.2f\n", first_sqrt); /* Get second number and display its square root. */ printf("Enter the second number> "); scanf("%lf", &second); second_sqrt = sqrt(second); printf("The square root of the second number is %.2f\n", second_sqrt); /* Display the square root of the sum of the two numbers. */ sum_sqrt = sqrt(first + second); printf("The square root of the sum of the two numbers is %.2f\n", sum_sqrt); return (0); } Enter the first number> 9.0 The square root of the first number is 3.00 Enter the second number> 16.0 The square root of the second number is 4.00 The square root of the sum of the two numbers is 5.00

16 Some Mathematical Library Functions

17 Some Mathematical Library Functions (cont.)

18 Using C Library Functions
Example 3.2

19 Using C Library Functions
Example 3.3 Triangle with an Unknown Side If we know the lengths of two sides (b and c0 of a triangle and the angle between them in degree ( α), the length of the third side (a) can be computed a 2 = b 2 + c 2 – 2bc cos α In C language: a = sqrt(pow(b,2) + pow(c,2) - 2*b*c*cos(alpha *PI/180.0));

20 3.3 Top-Down Design and Structure Charts
Some algorithms are more complex Programmers must break up the problem into sub-problems to develop the program solution Attempting to solve a problem at one level, new sub-problems at lower levels may be needed This process is called “Top-Down Design” approach proceeds from the original problem at the top level to the sub-problems at each lower level Structure Chart is a documentation tool for Top-Down Design

21 3.3 Top-Down Design and Structure Charts
An example of a Structure Chart Fig. 3.10

22 3.3 Top-Down Design and Structure Charts
Case Study: Drawing Simple Diagrams Problem Draw a house and a female stick figure Analysis The house and the female stick figure can be drawn from simple shapes, i.e. a circle parallel lines a base line intersecting lines Fig. 3.9

23 3.3 Top-Down Design and Structure Charts
Case Study: Drawing Simple Diagrams (cont.) DESIGN Initial Algorithm Draw a circle Draw a triangle Draw intersecting lines Algorithm Refinements Step 2 Refinement 2.1 Draw intersecting lines 2.2 Dray a base The structure chart is shown in Fig. 3.10

24 3.4 Functions without arguments
functions with argument (s) functions without argument Example: The atatement draw_circle(); /*call a function draw_circle with out argument */ This statement call a function draw_circle that implement the algorithm step Draw a circle. Function Call Statement (without argument) SYNTAX: fname(); EXAMPLE: draw_circle(); Interpretation: The function fname is called. after fname has finished execution, the statement that follows the function call will be executed.

25 Function Prototypes A function must be declared before it can be referenced (called) To declare a function: insert a function prototype before the main function Figure Function Prototypes and Main Function for Stick Figure /* Draws a stick figure */ #include <stdio.h> /* function prototypes */ void draw_circle(void); /* Draws a circle */ void draw_intersect(void); /* Draws intersecting lines */ void draw_base(void); /* Draws a base line */ void draw_triangle(void); /* Draws a triangle */ int main(void) { /* Draw a circle. */ draw_circle(); /* Draw a triangle. */ draw_triangle(); /* Draw intersecting lines. */ draw_intersect(); return (0); } Function Prototype (without argument) FORM: ftype fname(void); EXAMPLE: void draw_circle(void); ftype specifies the type of returned value, e.g., int, double, char, etc. ftype = void means the function does not return a value argument = void means no argument

26 Function Definitions Function definitions
Function Heading (not ended by ‘;’ ) Function body enclosed in brackets Return statement (can be eliminated if no value is returned) Figure Function scale /* * Multiplies its first argument by the power of 10 specified * by its second argument. * Pre : x and n are defined and math.h is included. */ double scale(double x, int n) { double scale_factor; /* local variable */ scale_factor = pow(10, n); return (x * scale_factor); }

27 Placement of Functions in a Program
Figure Program to Draw a Stick Figure /* Draws a stick figure */ #include <stdio.h> /* Function prototypes */ void draw_circle(void); /* Draws a circle */ void draw_intersect(void); /* Draws intersecting lines */ void draw_base(void); /* Draws a base line */ void draw_triangle(void); /* Draws a triangle */ int main(void) { /* Draw a circle. */ draw_circle(); /* Draw a triangle. */ draw_triangle(); /* Draw intersecting lines. */ draw_intersect(); return (0); } /* Draws a circle */ void draw_circle(void) printf(" * \n"); printf(" * * \n"); printf(" * * \n"); Placement of Functions in a Program

28 Placement of Functions in a Program
/* * Draws intersecting lines */ void draw_intersect(void) { printf(" / \\ \n"); /* Use 2 \'s to print 1 */ printf(" / \\ \n"); printf("/ \\\n"); } * Draws a base line draw_base(void) printf(" \n"); * Draws a triangle draw_triangle(void) draw_intersect(); draw_base(); Figure Program to Draw a Stick Figure (continue) Placement of Functions in a Program


Download ppt "CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions"

Similar presentations


Ads by Google