home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / var2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-19  |  519 b   |  26 lines

  1. // C++ program illustrates local variables in a function
  2.  
  3. #include <iostream.h>
  4.       
  5. double f(double x)
  6. {
  7.   return x * x - 5 * x + 10;
  8. }                          
  9.  
  10. double slope(double x)
  11. {
  12.   double f1, f2, incrim = 0.01 * x;
  13.   f1 = f(x + incrim);
  14.   f2 = f(x - incrim);
  15.   return (f1 - f2) / 2 / incrim;
  16. }
  17.       
  18. main()
  19. {                               
  20.   double x = 3.5;
  21.   
  22.   cout << "f("  << x << ") = " << f(x) << "\n"
  23.        << "f'(" << x << ") = " << slope(x) << "\n";
  24.        
  25.   return 0;
  26. }