cp104 introduction to programming top-down design with functions lecture 6-8 __ 1 top-down design...
TRANSCRIPT
![Page 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/1.jpg)
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
![Page 2: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/2.jpg)
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 <math.h>
– To call a math function
![Page 3: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/3.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 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 =
2 4
2
b b ac
a
2 4
2
b b ac
a
![Page 4: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/4.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 4
Find the Roots of a Quadratic Equation (Cont’d)
• Algorithm designAlgorithm 11. Get a, b, c2. 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 21. Get a, b, c2. 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
![Page 5: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/5.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 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();}
![Page 6: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/6.jpg)
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 design1. Get the radius of a circle2. Calculate the area: area = PI * radius*radius3. Calculate the circumference: circum = 2*PI*radius4. Display the area and the circumference
• Implementation
![Page 7: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/7.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 7
Calculating the Area and the Circumference of a Circle
![Page 8: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/8.jpg)
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.
![Page 9: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/9.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 9
Using Self-Defined Functions
#include <stdio.h>#define PI 3.14159
double area_circle(double);double circum_circle(double);
intmain(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);}
![Page 10: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/10.jpg)
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.
![Page 11: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/11.jpg)
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
![Page 12: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/12.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 12
Structure Chart for Drawing a Stick Figure
![Page 13: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/13.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 13
Function Prototypes and Main Function for Stick Figure
![Page 14: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/14.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 14
Function draw_circle
![Page 15: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/15.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 15
Function draw_triangle
![Page 16: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/16.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 16
Program to Draw a Stick Figure
![Page 17: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/17.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 17
Program to Draw a Stick Figure (cont’d)
![Page 18: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/18.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 18
Flow of Control Between the main Function and a Function Subprogram
![Page 19: 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](https://reader031.vdocuments.mx/reader031/viewer/2022032707/56649e215503460f94b0e050/html5/thumbnails/19.jpg)
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 19
Case Study: Multiply two Numbers (floating)
#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);}
#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);}