190 likes | 357 Vues
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
E N D
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
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
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 =
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
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(); }
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
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.
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; }
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.
Case study: Draw a Simple Diagram • Decomposition of the figure • Draw a circle • Draw a intersecting lines • Draw a base line
Flow of Control Between the main Function and a Function Subprogram
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); }