Lecture Notes for 10/22/04 Notes transcribed by: Megan Brown I give Professor Conrad permission to copy these notes and publically post them. Edited by P. Conrad, 12/13/2004 I.) Ceiling and Floor Functions The ceiling of a number is the smallest integer greater than or equal to the number. The floor of a number is the largest integer less than or equal to that number. For example: (may be on midterm) 1. ceil(7.0) = 7.0 floor(7.0) = 7.0 2. ceil(6.9) = 7.0 floor(6.9) = 6.0 3. ceil(-6.9) = -6.0 floor(-6.9) = -7.0 4. ceil(-6.1) = -6.0 floor(-6.1) = -7.0 Example code: #include /* math.h is needed to use ceiling, floor, and fmod */ int main(void) { printf("%5.3lf\n",ceil(7.1)); printf("%5.3lf\n",floor(7.1)); } The output should be: 8.000 7.000 Examples of function prototypes: (how to declare a function) 1. double ceil(double x); 2. double floor(double x); 3. double sqrt(double x); II.) Types of Pre-Processor Directives to Know 1. #include 2. #define 3. #if(0) comment or code /*this excludes the comment or code from compilation */ #endif III.) Declaring Variable vs. Declaring Functions Terms: 1. Scope: the part of a program where an identifier is defined. 2. Identifier: the variable name, the name of a function, name of a struct, etc. When declaring variables, you can only declare the variable once within a particular scope. Examples: int x; double d; When declaring functions, we use the function prototype or the function definition. IV.) Example Code 1. #include int squared(int x) /* the entire line is called the function head */ { /* x is the parameter of the function squared */ int result; result = x*x; /* this section is called the body */ return result; /* it is also the enitre definition of squared and the scope of result */ } int main(void) { int a,x; /* the x here is a local variable to main */ printf("....."); scanf("%d",&a); x = squared(a); /* function call- must be preceded by either a function prototype or a function definition */ printf("x = %d\n",x); return 0; } NOTE: Every set of braces creates a new scope. 2. int main(void) { double tonsRemaining; while(tonsRemaining > 0) { int result; result = tonsRemaining%12; /* This will not compile. See below example for details */ ... } /* end while */ } /* end main */ If you have: int a; int x;, you can do a%x. However, if you have double a; double x;, you should use the fmod command: fmod(x,x); The prototype is: double fmod(double a, double b); Here a and b are "stand-ins" for the actual variables a and x. (i.e. a and b are "formal parameters", a and x are "actual parameters"). 3.) May be on midterm... ... int result; result = tonsRemaining%12; if(result < 12) /* a scope is nested within another scope */ { int result; /* the variable result inside of the if statement differs from the variable result everywhere else */ result++; } printf("%d",result); 4.) #include double ceil(double x); /* redundancy is dumb, but it is legal */ double ceil(double x); #include /* long list of function prototypes */ /* The file math.h contains a function prototype for ceil, so if you #include it, you don't need to write "double ceil(double x);" */ int main(void) { printf("%5.3lf",ceil(7.0)); } 4.) int squared(int x); /* function prototype */ int main(void) { x = squared(a); /* function call */ } int squared(int x) /* function definition */ { return x * x; } *** end lecture notes for 10.22 ***