home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch4 / exp.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  3KB  |  97 lines

  1. /*                 exp.c
  2.  *
  3.  *   Synopsis  - Accepts input of float values for x and epsilon 
  4.  *               and calculates e to the x using the Taylor 
  5.  *               series. The series is expanded to the point 
  6.  *               where term is less than epsilon.
  7.  *
  8.  *   Objective - Illustrates the use of pointers and the address
  9.  *               operator in simulating pass by reference.
  10.  */ 
  11.  
  12. /* Include Files */
  13. #include <stdio.h>
  14.  
  15. /* Function Prototypes */
  16. void intro( void );
  17. /*   PRECONDITION:  none
  18.  *
  19.  *   POSTCONDITION: displays messages about the program  
  20.  */
  21.  
  22. double e_to_the_x( double x, double eps, int *num_terms_ptr );
  23. /*   PRECONDITION:  x is a double representing the exponent; eps is a 
  24.  *                  double representing the desired precision; 
  25.  *                  num_terms contains the address of an int that will 
  26.  *                  contain the number of terms in the Taylor Series 
  27.  *                  necessary to achieve the desired precision.
  28.  *
  29.  *   POSTCONDITION: uses the Taylor series expansion to calculate "e 
  30.  *                  to the x" ; the series is terminated when the term 
  31.  *                  is less than epsilon; returns the value of "e to 
  32.  *                  the x" and enters the number of terms in 
  33.  *                  *num_terms  
  34.  */
  35.  
  36. void getinput( double *x_ptr, double *eps_ptr );
  37. /*   PRECONDITION:  x_ptr contains the address of a double for the 
  38.  *                  value of x; eps_ptr contains the address of a 
  39.  *                  double for the value of epsilon
  40.  *
  41.  *   POSTCONDITION: uses keyboard input, with scanf() calls, to 
  42.  *                  populate the passed (by reference) parameters
  43.  */
  44.  
  45. int main( void )
  46. {
  47.      double x, epsilon, exp_val;    
  48.      int num_terms;    
  49.  
  50.      intro();
  51.  
  52.      getinput( &x, &epsilon );
  53.  
  54.      exp_val = e_to_the_x( x, epsilon, &num_terms );
  55.  
  56.      printf( "e raised to the power %.5lf is %.5lf.\n", x, exp_val );
  57.      printf( "The value was approximated with %d terms.\n", num_terms );
  58.      return 0;
  59. }
  60.  
  61. /*******************************intro()**************************/
  62.  
  63. void intro( void )
  64. {
  65.      printf( "This program will calculate the value of e " );
  66.      printf( "raised to the power x.\nYou will be asked to" );
  67.      printf( " enter a real number x,\nand a value epsilon" );
  68.      printf( " that gives an indication of the desired " );
  69.      printf( " accuracy.\n" );
  70. }
  71.  
  72. /*******************************e_to_the_x()*********************/
  73.  
  74. double e_to_the_x( double x, double eps, int *num_terms_ptr )
  75. {
  76.      double term = 1, value = 0;
  77.      int count = 1;
  78.      while ( term >= eps ) {
  79.           value += term;
  80.           term *= x;
  81.           term /= count++;
  82.      }
  83.  
  84.      *num_terms_ptr = count - 1;
  85.      return( value );
  86. }
  87.  
  88. /*******************************getinput()***********************/
  89.  
  90. void getinput( double *x_ptr, double *eps_ptr )
  91. {
  92.      printf( "\nEnter a value for x : " );
  93.      scanf( "%lf", x_ptr );
  94.      printf( "Enter a value for precision that is < 1: " );
  95.      scanf( "%lf", eps_ptr );
  96. }
  97.