home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / A / AxoCalculator Package / AxoCalculator Documentation / Programming in C / Example Functions next >
Encoding:
Text File  |  1993-03-13  |  2.6 KB  |  103 lines  |  [TEXT/AxoC]

  1. LocalLanguage C
  2. /* -------------------------------------------------
  3.   The following functions demonstrate various aspects of 
  4.   AxoCalculator's C programming language. 
  5.  
  6. •  To load all functions, choose "Select All" 
  7.     under the "Edit" menu, then press "enter". 
  8.  
  9. •  To run a function, type its name followed
  10.     by any parameters, then press "enter".
  11.  
  12. •  After the functions are loaded, this file does 
  13.     not need to remain open in order to use them.
  14.  
  15. •  Here are some examples of how to use the functions.
  16.  
  17. BoxVolume (10,15,20)
  18. Factorial (10)
  19. RecursiveFact (10)
  20. SameBirthDateProb (25)
  21.  
  22. --------------------------------------------------*/
  23.  
  24.  
  25. /* ---------------- RectArea -------------------
  26.   This function calculates the area of a rectangle
  27.   given its height and width.
  28. -------------------------------------------------*/
  29. float RectArea (height, width)
  30. {
  31.     return (height * width)
  32. }
  33.  
  34. /* ---------------- BoxVolume ----------------------
  35.   This function calculates the volume of a box 
  36.   given its height, width and depth. It calls the
  37.   function RectArea to get the area of the bottom of
  38.   the box, then multiplies the result by the box depth. 
  39. -------------------------------------------------*/
  40. float BoxVolume (height, width, depth)
  41. {
  42.     return RectArea (height, width) * depth
  43. }
  44.  
  45. /* ---------------- Factorial -------------------
  46.   This function calculates the factorial of a number.
  47. -----------------------------------------------*/
  48. float Factorial (number)
  49. {
  50.     float f
  51.     int i
  52.  
  53.     f = 1
  54.     for (i = 1 ; i <= Number ; i++) f = f * i
  55.     return f
  56. }
  57.  
  58.  
  59. /* ---------------- Factorial -------------------
  60.   This function also calculates the factorial of a number,
  61.   but it uses a recursive algorithm. This approach is
  62.   inefficient, but demonstrates recursion.
  63. -----------------------------------------------*/
  64. float RecursiveFact (number)
  65. {    
  66.     if number > 1
  67.         return  number * RecursiveFact (number - 1) 
  68.     else
  69.         return  1
  70. }
  71.  
  72. /* ---------------- SameBirthDateProb -------------------
  73.   This function calculates the probability that two or more
  74.   people in a group have the same birth date, given the
  75.   number of people in the group. It works by calculating
  76.   1 - the probability that everyone in the group has a 
  77.   different birth date.
  78. -----------------------------------------------*/
  79. float SameBirthDateProb (numberOfPeople)
  80. {
  81.     float DiffBirthDateProb
  82.     int i
  83.  
  84.     if (numberOfPeople < 2) then 
  85.         DiffBirthDateProb = 1
  86.     else 
  87.     {
  88.         if (numberOfPeople > 365) then 
  89.             DiffBirthDateProb = 0
  90.         else
  91.         {
  92.             DiffBirthDateProb = 1
  93.             for (i = 2 ; i <= numberOfPeople ; i++)
  94.             {
  95.                 DiffBirthDateProb = DiffBirthDateProb * (366-i) / 365
  96.             }
  97.         }
  98.     }
  99.     return (1 - DiffBirthDateProb)
  100. }
  101.  
  102.  
  103.