Dear friends The following code is correct, but it is written in C, Can anyone help me to change it to C++?

Thank you

////////////////////////////////////////////////////////

#include <stdio.h>

#include <math.h>

#define pi 3.141593

#define A 1.

#define qx (0.005*pi)

#define qy (0.005*pi)

#define F(x) ((*f)(x))

#define sqr(x) ((x)*(x))

#define no_intv 5000 /* no. of (equal) intervals for the integral */double xsav;

double (*nrfunc)();main()

{

double ellip2d(),funcn(),funca(),Nq,Aq;

Nq=ellip2d(-pi,pi,funcn);

Aq=ellip2d(-pi,pi,funca);

printf("Nq=%f\n",Nq);

printf("Aq=%f\n",Aq);

system("pause");

}double ellip2d(a,b,func)

double a,b;

double (*func)();

{

double simpson(),f1();

nrfunc=func;

return simpson(a,b,f1);

}double f1(x)

double x;

{

double simpson(),f2();

xsav=x;

return simpson(-pi,pi,f2);

}double f2(y)

double y;

{

return ((*nrfunc)(xsav,y));

}double funcn(x,y)

double x,y;

{

return x*x+y;

}double funca(x,y)

double x,y;

{

return x*x+y*y;

}

double simpson(a,b,f)

double a,b;

double (*f)();

/* integrnant = f(x), interval = [a,b], no. of intervals = no_int

i=1 -> x=a, i=2 -> x=a+h, ..., i=no_int+1 -> x=b; no_int = even */

{

int i;

double x,h,sum=0.;

if(fabs(b-a)<0.000001) return 0.;

h=(b-a)/((float)no_intv);

x=a+h;

for(i=2;i<=no_intv;i++) {

if(i&1) sum+=2.*F(x); /* bitwise definition of odd no. */

else sum+=4.*F(x);

x+=h;

}

return (h*(F(a)+sum+F(b))/3.);

}

I can't see anything there that isn't valid C++. Why do you want to change it?

Dear ijack Thank you very much for your reply. This code is valid and there is no problem with it, but I have additional steps which are written in C++ to add them to this code. I will be grateful if could please change it for me from C to C++.

Best Wishes

Just add the additional steps. As this code is already valid C++ it should interoperate with the new code without any problems.

Dear ijack Thank you for your quick response. I know it is, but I would really change it to C++, I will be really appreciate if could help me with this issue.

with all the respect

I really don't know what more you can want. The code is already valid C++, it works, so what do you want changed? I'm beginning to suspect that this is a homework assignment and that you have a requirement to use particular C++ features. I'm afraid that I'm not interested in helping with that.

Thank you very much sir for your help and cooperation

Ask Your Question

Weekly Poll