Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

ILLUSTRATION OF POINTERS TO FUNCTIONS

Posted By: Reiner Fischer     Category: C Programming     Views: 4421

Write a program that uses a function pointer as a function argument.

Code for ILLUSTRATION OF POINTERS TO FUNCTIONS in C Programming

#include  <math.h>                                               
   #define  PI  3.1415926    
   double y(double);
   double cos(double);
   double table (double(*f)(), double, double, double);

   main()                                                           
   {   printf("Table of y(x) = 2*x*x-x+1\n\n");                     
       table(y, 0.0, 2.0, 0.5);                                     
       printf("\nTable of cos(x)\n\n");                             
       table(cos, 0.0, PI, 0.5);                                    
   }                                                                
   double table(double(*f)(),double min, double max, double step)
   {   double a, value;                                             
       for(a = min; a <= max; a += step)                            
       {                                                            
          value = (*f)(a);                                          
          printf("%5.2f  %10.4f\n", a, value);                      
       }                                                            
   }                                                                
   double y(double x)                                                   
   {  return(2*x*x-x+1);                                           
   }    
                                                            
 
Output  
Table of y(x) = 2*x*x-x+1                                        
            0.00      1.0000                                                
            0.50      1.0000                                                
            1.00      2.0000                                                
            1.50      4.0000                                                
            2.00      7.0000                                                
       Table of cos(x)                                                  
            0.00      1.0000                                                
            0.50      0.8776                                                
            1.00      0.5403                                                
            1.50      0.0707                                                
            2.00     -0.4161                                                
            2.50     -0.8011                                                
            3.00     -0.9900                
  
Share: 


Didn't find what you were looking for? Find more on ILLUSTRATION OF POINTERS TO FUNCTIONS Or get search suggestion and latest updates.

Reiner Fischer
Reiner Fischer author of ILLUSTRATION OF POINTERS TO FUNCTIONS is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!