Attached is a tiny program in which the function F has as an argument the function pointer double (*f) (double). F returns the sum f(1)+f(2)+f(3)+f(4).
(Naturally, I have a much more interesting application in mind, but this simple example makes the point.) What I really want to do is replace f by a function of two variables, say g(x,y). Then I'd compute g(1,y) + g(2,y)+g(3,y)+g(4,y), so that F() would be a function of y. Is there a way to do this? Default values almost do the trick, but not quite.

Another totally unrelated question is this: can I download a text file right into this window? Then you wwouldn't have to open an attachment to see the program.

Attachments
``````#include <iostream>
using namespace std;

double F(double (*f)(), int N);
double f(double);

double F(double (*f)(double), int N)
{   double sum = 0;
for(int i = 1; i <= N; i++)
sum += (*f)(1.0*i);
return sum;
}

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

main()
{   cout << F(f,4);
}``````
3
Contributors
3
Replies
4
Views
13 Years
Discussion Span
Last Post by Dave Sinkula

Well, I have to tell you I suck at maths, so I dont get what you wanna do with the g(x,y) (mmm, I think I saw that in calculus 2 or 3 at collegue...)
Anyway, is it something like this what you wanna accomplish?

``````#include <iostream>
using namespace std;

double F(double (*f)(), int N,double);
double f(double,double);

double F(double (*f)(double,double), int N,double y)
{   double sum = 0;
for(int i = 1; i <= N; i++)
sum += (*f)(1.0*i,y);
return sum;
}

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

int main(int argc,char* argv)
{   cout << F(f,4,6);

return 0;
}``````

And I answered your offtopic question... just put the sources inside code tags
(code)
(/code)

Replace the () with []

Cheers

Thanks a lot! Your program works fine and it tells me just what I need to know. I thought that I'd have to say in the declaration of F that *f is a function of two variables and I didn't know how to do that, but apparently you don't have to. I guess it suffices to say that *f is a function which returns a double.

As to the tangential question about entering the code without using an attachment, I meant without typing the code in. I know how to type the code using tags, but I'd rather download the actual source file that ran so that I don't intoduce any typos. Of course, with a code as short as mine it wouldn't be much trouble to type it in and proofread very carefully, but with a bigger code that migh be a problem. I tried dragging it (i.e., dragging the filr icon from my computer) but that didnt work. Is there a way to paste it?

Once again, thanks.
PS. Dont knock your mathematical ability.

>Is there a way to paste it?

That's my usual preferred way. I'll flip over to my editor, Ctrl-A to "Select All" of the file, Ctrl-C to copy; flip back to the message editor and Ctrl-V paste it right smack in the middle of [code][/code]. YMMV.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.