C Programming Language Lecture 4 (10/13/2000) Instructor: Wen-Hung Liao E-mail: whliao@cs.nccu.edu.tw Extension: 62028
Building Programs from Existing Information Analysis Design Implementation Testing 2
Case Study Finding the area and circumference of a circle Analysis: one input: radius two outputs: area, circumference Constant: PI Formula 3
Case Study: Initial Algorithm Get the circle radius Calculate the area Calculate the circumference Display the results 4
Case Study: Implementation /* Calculates and displays the area and circumference of a circle */ #include <stdio.h> #define PI 3.14159 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); 5
Library Functions Pre-defined functions -> code reuse Example: sqrt(x) to compute the square root of #include <math.h> 6
C Mathematical Library Functions Function Header File Argument Result abs(x) <stdlib.h> int int ceil(x) <math.h> double double cos(x) <math.h> double double exp(x) <math.h> double double fabs(x) <math.h> double double floor(x) <math.h> double double log(x) <math.h> double double log10(x) <math.h> double double 7
More C Math Functions Function Header File Argument Result pow(x,y) <math.h> double, double double sin(x) <math.h> double double (radians) sqrt(x) <math.h> double double tan(x) <math.h> double (radians) double 8
Dealing with abnormalities What happens if you send a negative number as an argument to the function sqrt(x)? How about log10(x)? How about abs(5.6)? It s up to the programmer, not the compiler, to deal with these potential problems. 9
Mathematical Expressions Compute the value of a, given: a 2 = b 2 + c 2 2bc cos( α) b α a a=sqrt(b*b + c*c - 2*b*c*cos(alpha*PI/180.0)) c 10
Other Examples Evaluate ceil(-7.2)*pow(4.0,2.0) Write the expression for: ( x y) 3 11
Top-down Design Case study: drawing simple diagrams Problem: Draw simple diagrams, such as a house, on your screen. Analysis: draw figures with some basic components: a circle a base line parallel lines intersecting lines 12
Structure Chart Drawing Stick Figures Draw a figure Draw a circle Draw a triangle Draw intersecting lines Draw intersecting lines Draw a base 13
Function without arguments Example: void draw_circle(void) Function Prototypes (Important!) a function must be declared before it is referenced. One way to declare a function is to insert a function prototype before the main function. 14
Function Prototype Tells the C compiler the data type of the function, the function name, and information about the arguments that the function expects. Example: double sin(double x); double pow(double x, double y); 15
Example /* * Draws a circle */ void draw_circle(void) { printf(" * \n"); printf(" * *\n"); printf(" * * \n"); } 16
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); 17
Functions with input arguments Void function with input arguments functions with input arguments and a single result functions with input arguments and multiple results (use pointer ) 18
Advantage of using functions Procedural abstraction Function reuse Easier to maintain, debug 19
Your Homework Read Chapter 3 of textbook Use the top-down design approach to write a C program that will print your initials (e.g., my name is Wen-Hung Liao, then I'll have to write three functions, draw_w( ), draw_h( ), and draw_l( ) and use a main program to call these functions to print out the letters.). 20
Your Homework (cont d) Write three functions, named dsin, dcos, and dtan, respectively, so that a user can pass the argument in degrees, instead of radians, to compute sin, cos, and tan. Use a main program to test your code. Due on 10/20/2000. Please send the source code to your TA via e-mail. 21