Dear Friends

Can you help me to convert the following code from c into c++ and save the file as file.cpp. I would do that in order to use cout instead of fprint and for another reasons.

#include <stdio.h>
#include <math.h>
#define pi 3.141593
#define F(x) ((*f)(x))
#define sqr(x) ((x)*(x))
#define no_intv 5000       

double xsav;
double (*nrfunc)();

main()
{
  double ellip2d(),funcn(),Nq;
  Nq=ellip2d(-pi,pi,funcn);
 
  printf("Nq=%f\n",Nq);
  system("pause");
}
//////////////////////////////////////

double ellip2d(a,b,func)
double a,b;
double (*func)();
{
  double simpson(),f1();
  nrfunc=func;
  return simpson(a,b,f1);
}

double f1(s1)
double s1;
{
  double simpson(),f2();
  xsav=s1;
  return simpson(-pi,pi,f2);
}

double f2(s2)
double s2;
{
  return ((*nrfunc)(xsav,s2));
}
////////////////////////////////////
double funcn(x,y)
double x,y;
{
  return x*x+y;
}

double simpson(a,b,f)
double a,b;
double (*f)();
{
  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); 
    else    sum+=4.*F(x);
    x+=h;
  }
  return (h*(F(a)+sum+F(b))/3.);
}

You can use printf in C++. Just #include <cstdio> .
You will want to declare main as being of type int and will likely want to provide a return value.

It will also be necessary to change the <math.h> header to <cmath> and resolve the std namespace by some means.

Additionally, if you want to use cout, you'll need the <iostream> header.

The syntax is either way off for C or so confuscated as to be unreadable. Semicolons are missing or misplaced. It's hard to tell if there are function declarations or variable declarations. Etc.

The syntax is either way off for C or so confuscated as to be unreadable. Semicolons are missing or misplaced. It's hard to tell if there are function declarations or variable declarations. Etc.

It's not only C syntax. It has bad style even FOR C and I don't think it will compile in C compiler.

Surprisingly, it is actually valid C, or rather, it was valid about twenty-five years ago. The function argument declarations are of a form that was used in 1st edition K&R C, but which most modern compilers wouldn't accept as far as I am aware (though interestingly, GCC accepts it without even a warning). The same applies to the function prototypes.

Is

double (*func)();

a function pointer?

GCC supports a lot of things without a warning, but it gives me one when I use standard C++0x features for some reason :D

I realize that I may be overreaching myself, but to save everyone trouble, I've converted the code for the OP:

#include <cstdio>
#include <cmath>
#define pi 3.141593
#define F(x) ((*f)(x))
#define sqr(x) ((x)*(x))
#define no_intv 5000

double xsav;
double (*nrfunc)(double x, double y);

double ellip2d(double a, double b, double (*func)(double x, double y));
double f1(double s1);
double f1(double s1);
double f2(double s2);
double funcn(double x, double y);
double simpson(double a, double b, double (*f)(double x));

int main()
{
    double Nq;
    Nq = ellip2d(-pi, pi, funcn);

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

    return 0;
}
//////////////////////////////////////

double ellip2d(double a, double b, double (*func)(double x, double y))
{
    nrfunc = func;
    return simpson(a, b, f1);
}

double f1(double s1)
{
    xsav = s1;
    return simpson(-pi, pi, f2);
}

double f2(double s2)
{
    return ((*nrfunc)(xsav,s2));
}

////////////////////////////////////
double funcn(double x, double y)
{
    return x * x + y;
}

double simpson(double a, double b, double (*f)(double x))
{
    int i;
    double x,h,sum = 0.0;
    if(fabs(b-a) < 0.000001) return 0.0;
    h = (b - a) / ((float)no_intv);
    x = a + h;
    for(i = 2; i <= no_intv; i++)
    {
        if(i&1) sum += 2.0 * F(x);
        else    sum += 4.0 * F(x);
        x+=h;
    }
    return (h*(F(a)+sum+F(b))/3.0);
}

This is just a straight translation; since Physicist presumably knows what changes he wants to make, I'll leave it to him to convert the I/O and so forth.

Is

double (*func)();

a function pointer?

Yes, though by C++ standards, the typing is incomplete.

GCC supports a lot of things without a warning, but it gives me one when I use standard C++0x features for some reason :D

I'm pretty sure that GCC defaults to the C++98 standard, as the C++11 standard wasn't actually formalized until a little over a month ago (hence the 'x' in the older working name of the standards group). The GCC development team treat C++11 as 'experimental', as they are still working on the new features and aren't entirely compliant with it yet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.