Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.

Similar presentations


Presentation on theme: "CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down."— Presentation transcript:

1 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down design and structure charts Basic concepts about functions –Prototype, definition and function call –Input arguments –Output arguments –Void function and void argument –Actual parameter / formal parameter –Local variable Building programs from existing information

2 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 2 Reuse of Existing Code C has a rich function library consists many predefined functions. –What are they? arguments, output, and their data types. Use C library functions –To simply a program –To reduce the errors –To write program efficiently Example: mathematical function library –contains commonly used math function routines: ceil(x), cos(x), exp(x), fabs(x), floor(x), log(x), log10(x), pow(x,y), sqrt(x), sin(x), tan(x) –To include math library by adding #include –To call a math function

3 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 3 Case Study: Find the Roots of a Quadratic Equation ax 2 +bx+c = 0 Problem –Get the coefficients a, b, and c. Compute and display the roots of ax 2 +bx+c = 0 Analysis –Input: a, b, c –Output: root_1, root_2 –Relevant formulas root_1 = root_2 =

4 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 4 Find the Roots of a Quadratic Equation (Cont’d) Algorithm design Algorithm 1 1.Get a, b, c 2.Calculate root_1 = (-b + sqrt( pow(b,2) – 4*a*c ))/(2*a) 3.Calculate root_2 = (-b - sqrt( pow(b,2) – 4*a*c ))/(2*a) 4.Display root_1, root_2. Algorithm 2 1.Get a, b, c 2.Calculate the discriminant: disc = pow(b,2) – 4*a*c; 3.Calculate the square root of discriminant: sqrt_ disc = sqrt(disc); 4.Calculate root_1 = (-b + sqrt_disc)/(2*a) 5.Calculate root_2 = (-b – sqrt_disc)/(2*a) 6.Display root_1, root_2 Optimize the algorithm by reducing the number of operations. Tradeoff between space and time

5 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 5 Implementation: using existing math functions #include main() { double a, b, c; double root_1, root_2; double disc, sqrt_disc; printf("Enter the coefficients a, b, and c:\n"); scanf("%lf%lf%lf", &a, &b, &c); disc = b*b - 4*a*c; sqrt_disc = sqrt(disc); /* sqrt() is a math function in math lib */ root_1 = (-b + sqrt_disc)/(2*a); root_2 = (-b - sqrt_disc)/(2*a); printf("The roots of %fx^2 + %fx + %f = 0 are \nroot_1 = %f, root_2 = %f\n", a, b, c, root_1, root_2); fflush(stdin); getchar(); }

6 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 6 Case Study: Finding the Area and Circumferences of a Circle Problem –Get the radius of a circle. Compute and display the circle’s area and circumference. Analysis –Input: radius –Output: area, circumference –Relevant formulas Problem constants: PI 3.14159 Area = PI * radius 2 Circum = 2* PI * radius –Data requirements: double radius, area, circum Algorithm design 1.Get the radius of a circle 2.Calculate the area: area = PI * radius*radius 3.Calculate the circumference: circum = 2*PI*radius 4.Display the area and the circumference Implementation

7 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 7 Calculating the Area and the Circumference of a Circle

8 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 8 User Defined Function and Top-Down Design Method C allow a user to define a function. This makes th top-down design possible Top-down design_ a problem-solving method in which one first break a problem up into its major subproblems and then solve the subproblems to derive the solution to the original problem Structure chart __ a documentation tool that shows the relationships among the sub-problems of a problem.

9 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 9 Using Self-Defined Functions #include #define PI 3.14159 double area_circle(double); double circum_circle(double); 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 */ printf("Enter radius> "); scanf("%lf", &radius); area = area_circle(radius); circum = circum_circle(radius); /* Display the area and circumference */ printf("The area is %.4f\n", area); printf("The circumference is %.4f\n", circum); return 0; } /* Calculate the area */ double area_circle(double r) { return(PI * r * r); } /* Calculate the circumference */ double circum_circle(double r) { return(2*PI *r); }

10 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 10 Structured Programming A program in which individual program tasks are performed by independent section of program code Advantages –Easier to write structured program become complex programming problems are broken into a number of smaller and simpler taskes –Easier to debug, i.e., easier to isolate a bug to a specific section of code –Code reuse, functions in one program can be used in another program for the same task With functions top-down design method can be applied to write structured program.

11 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 11 Case study: Draw a Simple Diagram Decomposition of the figure Draw a circle Draw a intersecting lines Draw a base line

12 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 12 Structure Chart for Drawing a Stick Figure

13 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 13 Function Prototypes and Main Function for Stick Figure

14 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 14 Function draw_circle

15 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 15 Function draw_triangle

16 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 16 Program to Draw a Stick Figure

17 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 17 Program to Draw a Stick Figure (cont’d)

18 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 18 Flow of Control Between the main Function and a Function Subprogram

19 CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 19 Case Study: Multiply two Numbers (floating) #include /* function prototype */ void printMessage(void); double multiply(double, double); int main(void){ double a, b, c; printMessage(); scanf(“%lf %lf”,&a,&b); c = multiply(a,b); printf(“%f”, c); } /* definition of function multiply */ void printMessage(){ printf(“Input two numbers:\n”); } double multiply(double x, double y){ double z; z = x * y; return(z); } #include /* definition of function multiply */ void printMessage(){ printf(“Input two numbers:\n”); } double multiply(double x, double y){ double z; z = x * y; return(z); } main(void){ double a, b, c; printMessage(); scanf(“%lf %lf”,&a,&b); c = multiply(a,b); printf(“%f”, c); }


Download ppt "CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down."

Similar presentations


Ads by Google