0

Possible Duplicate:
Merging two modules into one. The first is an equation; the second takes the integral of this equation

#include <stdio.h>
#include <math.h>

double f(double x){
   return (x*x);
}

double integrateF(double (function)(double) ){
    double area;
    double x_width;

    int k; // Counter necessary for For-Loop
    int n; // # of bars

    double min; // Limit min
    double max; // Limit max

    printf("Please enter the limit minima, 'a'==>\n");
    scanf("%lf",&min);
    printf("Please enter the limit maxima, 'b'==>\n");
    scanf("%lf",&max);
    printf("Please enter # of bars needed to span [a,b]==>\n");
    scanf("%d",&n);

   x_width=(max-min)/n;

   for(k=1;k<=n;k++){
      area+=function(min+x_width*(k-0.5));
   }
   area*=x_width;

   return area;
}

int main(void){
   double resultant;
   resultant=integrateF(f);
   printf("The value of the integral is: %f \n",resultant);
   return 0;
}

Evening all,

My first module consists of the function (x^2). The returned value proceeds onto integrateF(f), which then initializes the second module. This is when things get messy...

What does this line do?

double integrateF(double (function)(double) ){

Important note: My program runs smoothly but I have no idea why because of this line.

Is there any way I can remodel this code to exclude my first module AND this odd line (and whatever needs to go can go as well) so I only have the integration module that nests the (x^2) function.

My main(void) module can stay, of course.

Community
  • 1
  • 1
user1742525
  • 45
  • 1
  • 6

2 Answers2

1

Your integrant method takes as parameter a function pointer, that can be any that is compliant with the definition of the parameter it takes, in this case, any function that takes a unique parameter of type double and returns a double.

This is what the parameter in integrateF specifies.

In my opinion, I would keep the design as is. In this way you can change the function you integrate in a very clean way.

If you have no option then, replace:

      area+=function(min+x_width*(k-0.5));

and instead add:

      value = min+x_width * (k - 0.5);
      value *= value; 
      area += value;

where value is a double, declare like:

double value;

and integrateF will not require any parameters.

double integrateF()
Picarus
  • 760
  • 1
  • 10
  • 25
0

Another opinion also has merit. integrateF is written now in such a way that it can be extracted to another library and compile separately from function f. If you never expect it to happen you can rewrite integrateF like this:

double integrateF(){

...

area+=f(min+x_width*(k-0.5));

...

function f does not need changes. When you want to integrate another function (not x*x) just modify body of f() and recompile the whole program.

farfareast
  • 2,179
  • 1
  • 23
  • 22
  • This is a fantastic improvement, thank you. However, it's still required to somehow incorporate the module 'f' in the module 'integrateF', somewhere... From what I see, the main function initializes the variable 'resultant', then this variable requests the use of the module 'integrateF', and this module requests the use of 'f', correct? My main function can only request one module, so merging is necessary. – user1742525 Oct 13 '12 at 00:17
  • @user1742525: You are using some interesting "non-traditional" terminology to say the least. But good effort! What you call 'module' (f and integrateF) are actually functions (the same as main()). resultant variable is not initialized - it is assigned. = is used in that line for assignment operator. It is called initialization when = is used in declaration like `int i=1;` Not the variable requests the use of f but the expression on the right side of assignment **calls** the function f. – farfareast Oct 13 '12 at 03:10
  • Long story short: instead of line `area+=f(min+x_width*(k-0.5));` write two lines: `double x = min+x_width*(k-0.5);` and `area += x*x;`. Definition of the function f() `double f(double x){...}` in the beginning of the file you can delete. – farfareast Oct 13 '12 at 03:19