1 / 19

Top-Down Design with Functions

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

steel-hunt
Télécharger la présentation

Top-Down Design with Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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 <math.h> • To call a math function

  3. Case Study: Find the Roots of a Quadratic Equation ax2+bx+c = 0 • Problem • Get the coefficients a, b, and c. Compute and display the roots of ax2+bx+c = 0 • Analysis • Input: a, b, c • Output: root_1, root_2 • Relevant formulas root_1 = root_2 =

  4. Find the Roots of a Quadratic Equation (Cont’d) • Algorithm designAlgorithm 1 • Get a, b, c • Calculate root_1 = (-b + sqrt( pow(b,2) – 4*a*c))/(2*a) • Calculate root_2 = (-b - sqrt(pow(b,2) – 4*a*c ))/(2*a) • Display root_1, root_2. Algorithm 2 • Get a, b, c • Calculate the discriminant: disc = pow(b,2) – 4*a*c; • Calculate the square root of discriminant: sqrt_disc = sqrt(disc); • Calculate root_1 = (-b + sqrt_disc)/(2*a) • Calculate root_2 = (-b – sqrt_disc)/(2*a) • Display root_1, root_2 • Optimize the algorithm by reducing the number of operations. Tradeoff between space and time

  5. Implementation: using existing math functions #include <stdio.h> #include <math.h> 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. 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 • Get the radius of a circle • Calculate the area: area = PI * radius*radius • Calculate the circumference: circum = 2*PI*radius • Display the area and the circumference • Implementation

  7. Calculating the Area and the Circumference of a Circle

  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. Using Self-Defined Functions /* Calculate the area */ double area_circle(double r) { return(PI * r * r); } /* Calculate the circumference */ double circum_circle(double r) { return(2*PI *r); } #include <stdio.h> #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; }

  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. Case study: Draw a Simple Diagram • Decomposition of the figure • Draw a circle • Draw a intersecting lines • Draw a base line

  12. Structure Chart for Drawing a Stick Figure

  13. Function Prototypes and Main Function for Stick Figure

  14. Function draw_circle

  15. Function draw_triangle

  16. Program to Draw a Stick Figure

  17. Program to Draw a Stick Figure (cont’d)

  18. Flow of Control Between the main Function and a Function Subprogram

  19. Case Study: Multiply two Numbers (floating) #include <stdio.h> /* 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); } #include <stdio.h> /* 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); }

More Related