Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-09-functions

Avatar for William Albritton William Albritton
September 23, 2015
260

 ics212-09-functions

Avatar for William Albritton

William Albritton

September 23, 2015
Tweet

Transcript

  1. Memory Allocation  Header files  C Standard Library 

    Math functions  Trigonometric functions  Random number generation
  2. #include  #include is a preprocessor directive  Using #include

    causes a copy of the specified file to be included in place of the directive  We have used it with <stdio.h> and other header files
  3. #include  #include is used for C Standard Library header

    files (note the angled brackets: < >)  And for programmer-defined header files (note the double-quotes: " ") #include <filename.h> #include “filename.h”
  4. #include "filename.h"  For programmer-defined header files, the preprocessor looks

    for the file in the same directory as the file being compiled
  5. Example Header File  For an example header file, see

    getdouble.h  The function definition for function getdouble() is stored in file getdouble.c #define MAX 100 double getdouble();
  6. Example Program  Any program that uses function getdouble() must

    include the header file  For example, file inputdouble.c includes the header file getdouble.h, so it can use the function getdouble() #include “getdouble.h”
  7. C Standard Library  Prepackaged functions already written  Each

    library has a header file, which is the interface (list of function prototypes) to the library functions  The header file also contains macros and variables  Library files are binary files in machine code  Header files are text files in the C language
  8. Header Files from C Standard Library  Header file :

    Description  <assert.h> : Diagnostics used to detect logical errors  <ctype.h> : Character functions to see if uppercase, etc.  <errno.h> : Test error codes reported by library functions  <float.h> : Macro constants for floating-point limits  <limits.h> : Macro constants for integer limits
  9. Header Files from C Standard Library  Header file :

    Description  <locale.h> : Locale-specific I/O for numbers, currency, etc.  <math.h> : Mathematical functions  <setjmp.h> : Non-local jumps to different functions  <signal.h> : How to handle signals, such as division by zero  <stdarg.h> : Functions can have infinite arguments
  10. Header Files from C Standard Library  Header file :

    Description  <stddef.h> : Macro and variable definitions, such as NULL  <stdio.h> : Input and output functions  <stdlib.h> : Utility functions, such as numeric conversions  <string.h> : String functions  <time.h> : Time and date functions
  11. More Details  For links to more details on these

    libraries and their functions, see “Examples and Resources” column on ICS 212 class webpage
  12. When Coding and Compiling  In the code, when you

    use the math library…  At compile time, you need to include the "-lm" flag #include <math.h> % gcc program.c -lm
  13. When Linking with a Makefile  For makefiles, put "-lm"

    flag at end of link command program: program.o gcc program.o –o program –lm program.o:program.c gcc –c program.c
  14. fabs(x)  Returns the absolute value of x  Negative

    numbers return a positive number with the same value  Positive numbers return the same positive number  Function prototype: double fabs(double x);  Parameter can be a double or integer (automatically converts it to a double), but always returns a double
  15. fabs(x)  To return an integer, you need cast it

    to an integer const double PI = 3.141592; const int TWO = 2; printf(“%f\n”, fabs(-PI)); //3.141592 printf(“%f\n”, fabs(TWO)); //2.000000 printf(“%d\n”, (int)fabs(TWO)); //2
  16. sqrt(x)  Computes the square root of x  Where

    x >=0  Function prototype: double sqrt(double x);  Always returns a double const int TWO = 2; printf(“%f\n”, sqrt(TWO)); //1.414214
  17. pow(x,y)  Computes x to the power of y 

    Function prototype: double pow(double x, double y);  Errors occur if:  x=0 and y<=0  x<0 and y is not an integer
  18. pow(x,y) const int TWO = 2; const int TEN =

    10; const double HALF = 0.5; printf(“%d\n”, (int)pow(TWO, TEN)); //1024 printf(“%f\n”, pow(TEN, -TWO)); //0.010000 printf(“%f\n”, pow(TWO, HALF)); //1.414214
  19. ceil(x)  Rounds x to the nearest integer UP 

    Function prototype: double ceil(double x); const double PI = 3.141592; printf(“%f\n”, (int)ceil(PI)); //4 printf(“%f\n”, (int)ceil(-PI)); //-3
  20. floor(x)  Rounds x to the nearest integer DOWN 

    Function prototype: double floor(double x); const double PI = 3.141592; printf(“%f\n”, (int)floor(PI)); //3 printf(“%f\n”, (int)floor(-PI)); //-4
  21. exp(x)  Computes value of ex  The number “e”

    is the Base of Natural Logarithms  An irrational number and important math constant  e = 2.7182818284590452353602874713526624977572...  e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + ...  Discovered from compound interest calculations  Also called Napier's Constant or Euler's Number
  22. exp(x)  Function prototype: double exp(double x); const int ZERO

    = 0; const int ONE = 1; printf(“%f\n”, exp(ZERO)); //1.000000 printf(“%f\n”, exp(ONE)); //2.718282 printf(“%f\n”, exp(-ONE)); //0.367879
  23. log(x)  The natural logarithm of x to the base

    e (log e x)  Errors occur if x<=0  Function prototype: double log(double x); const int ONE = 1; const double E = exp(ONE); printf(“%f\n”, log(ONE)); //0.000000 printf(“%f\n”, log(E)); //1.000000
  24. log10(x)  The common logarithm of x to the base

    10 (log 10 x)  Errors occur if x<=0  Function prototype: double log10(double x); const int ONE = 1; const int TEN = 10; printf(“%f\n”, log10(ONE)); //0.000000 printf(“%f\n”, log10(TEN)); //1.000000
  25. sin(x)  Calculates the sine of x, where x is

    in radians  On a right triangle, the sine is the angle’s opposite side divided by the hypotenuse  Sine = opposite/hypotenuse
  26. sin(x)  A radian is the angle made from wrapping

    the radius around the edge of the circle  1 radian = 180/π degrees ≈ 57.296°  2π radians = 360 degrees
  27. sin(x) for(i=begin; i<=end; i = i + jump){ printf(“sin(%+f) =

    %+f\n”, i, sin(i)); } /* sin(+0.000000) = +0.000000 sin(+1.570796) = +1.000000 sin(+3.141593) = +0.000000 sin(+4.712389) = -1.000000 sin(+6.283185) = -0.000000 */
  28. cos(x)  Computes the cosine of x where x is

    in radians  On a right triangle, the cosine is the angle’s adjacent side divided by the hypotenuse  Cosine = adjacent/hypotenuse
  29. cos(x) for(i=begin; i<=end; i = i + jump){ printf(“cos(%+f) =

    %+f\n”, i, cos(i)); } /* cos(+0.000000) = +1.000000 cos(+1.570796) = +0.000000 cos(+3.141593) = -1.000000 cos(+4.712389) = -0.000000 cos(+6.283185) = +1.000000 */
  30. tan(x)  Computes the tangent of x where x is

    in radians  On a right triangle, the tangent is the angle’s opposite side divided by the adjacent side  Tangent = opposite/adjacent
  31. tan(x)  Function prototype: double tan(double x);  Goes from

    negative to positive infinity from -π/2 radians (-90°) to π/2 radians (90°) and other angles
  32. tan(x) for(i=begin; i<=end; i = i + jump){ printf(“tan(%+f) =

    %+f\n”, i, tan(i)); } /* tan(+0.000000)=+0.000000 tan(+1.570796)=+16331239353195370.000000 tan(+3.141593)=-0.000000 tan(+4.712389)=+5443746451065123.000000 tan(+6.283185)=-0.000000 */
  33. asin(x)  Computes the arcsine or inverse sine of x,

    where x must be in the range [-1, 1]  The function returns an angle in radians in the range [-∏/2, ∏/2]
  34. acos(x)  Computes the arccosine or inverse cosine of x,

    where x must be in the range [-1, 1]  The function returns an angle in radians in the range [0, ∏]
  35. atan(x)  Computes the arctangent or inverse tangent of x,

    where x can be any value  The function returns an angle in radians in the range [-∏/2, ∏/2]
  36. Sequence of Random Numbers  May be generated for experiments

    or simulations  Numbers may be required to be within a range  For our example, we will simulate rolling a six-sided dice 1000 times
  37. Random-Number Seed  A seed is an unsigned integer number

    that is used to generate a random sequence  To set the seed, we need to use the srand() function, which is part of the stdlib.h library  Function definition: void srand(unsigned int seed);
  38. Time is the Seed  We may use our system’s

    clock to generate the seed for the random number generator  Function time(NULL) returns the "UNIX time," which is the number of seconds since January 1, 1970  NULL is the NULL pointer, which has the value zero (0) srand(time(NULL));
  39. Generating Random Numbers  Use function rand(), which in the

    stdlib.h library  Function prototype: int rand(void);  Generates a pseudo-random number from zero (0) to RAND_MAX (32,767 on UH UNIX)  Each time a program containing rand() is executed the same values will be generated, so that is why we seed it with a different value each time (UNIX time)
  40. Random Floating Point Numbers  To generate a random floating

    point number between 0 and 1 you can use this code x = rand(); f1 = (float)x; f2 = (float)RAND_MAX; f3 = f1/f2;
  41. Random Integers within a Range  To generate random integers

    starting at m with range n use the modulus (%) operator  Will generate numbers starting at m with range n x = m + rand() % n;
  42. Memory Management  Header files and C Standard Library 

    Math and Trigonometric functions  Compile/link using the –lm flag  Random number generation  srand() != rand()