Predefined functions in math.h abs - abs(1.45) is ___ , abs(-1.45) is ____ sqrt(25) is ____ result is type ____ pow(x, y) is xy pow(3, 2) is ___ pow(2, 3) is _____, result is type ___ floor , ceil convert a real value to a whole number (also type real). x is 4.15 floor(x) x ceil(x) 4.0 4.15 5.0 … 6.0 7.0 x is -8.65 floor(x) x ceil(x) -9.0 -8.65 -8.0 … -7.0 -6.0 sin, cos, tan - trigonometric functions. Argument should be in radians, not degrees. If x is an angle in radians, cos(x) returns the cosine of x. converting from degrees to radians - 0 degrees is 0 radians 360 degrees is 2 ? radians in general, if w is in degrees, to convert to radians, mulitply by ? / 180.0 if theta_deg is angles in degrees, theta_rad = theta_deg * 3.14159 / 180.0; /* program to convert angle in degrees to radians * and print its sin and cosine */ #include #inlcude #define PI 3.14159 int main(void) { double rad_per_degree = PI / 180.0; double theta_deg, theta_rad; printf("Enter theta in degrees: "); scanf("%lf", &theta_deg); theta_rad = theta_deg * rad_per_degree; printf("Theta in radians is %.2f\n", theta_rad); printf("Its sin is %f and its cosine is %f\n", sin(theta_rad), cos(theta_rad)); return (0); } [thunder] ~ > cc -lm degrees.c [thunder] ~ > a.out Enter theta in degrees:90 Theta in radians is 1.57 Its sin is 1.000000 and its cosine is 0.000001 [thunder] ~ > a.out Enter theta in degrees:180 Theta in radians is 3.14 Its sin is 0.000003 and its cosine is -1.000000 Writing your own functions Example in book: Drawing some stick figures using standard shapes, triangle, square, circle. We can write our own functions to draw such shapes and then call them just like we call library functions. /* “Draws” a square */ void draw_square() { printf(“*******\n”); printf(“* *\n”); printf(“* *\n”); printf(“* *\n”); printf(“*******\n”); } Assume you have draw_circle, draw_triangle, draw_square functions: #include /* function prototypes */ void draw_square(); void draw_circle(); void draw_triangle(); void main() { draw_circle(); /* circle on top */ draw_triangle(); /* triangle next */ draw_square(); /* square on bottom*/ return 0; } /* function definitions go here. Order * doesn’t matter */ /* “Draws” a square */ void draw_square() { printf(“*******\n”); printf(“* *\n”); printf(“* *\n”); printf(“* *\n”); printf(“*******\n”); } Flow of execution for above program: Begin to execute main function: Execute 1st line of main - call to draw_circle Execute first line of draw_circle … Execute last line of draw_circle and return to where you left off In function main. Execute 2nd line of main function – call to draw_triangle Execute first line of draw_triangle … Execute last line of draw_triangle and return Execute 3rd line of main function – call to draw_square Execute first line of draw_square … Execute last line of draw_square and return Execute last line of main function and return 0. Function definition has the form: Result type function name argument list void draw_square () { function body } A void function does not return a result – it performs an action such as printing or displaying but doesn’t calculate a value (unlike sin or sqrt). Other uses of void functions: Displaying instructions to the program user to help the user understand how to interact with the program. void instruct() { printf(“This program multiplies ”); printf(“two fractions.\n”); printf(“Enter each one as: ”); printf(“n / d \n”); printf(“where n and d are ”); printf(“both integers.\n”); } When called this function displays: This program multiplies two fractions. Enter each one as: n / d where n and d are both integers. #include void instruct(); int main() { integer num_1, denom_1; … /* Display instructions */ instruct(); /* Read two fractions */ printf(“Enter first fraction: “); scanf(“%d/%d”, &num_1, &denom_1); … return 0; } /* end of main */ void instruct() { printf(“This program multiplies ”); printf(“two fractions.\n”); … } /* end of instruct */