Function pointer problem

need suggestion in pointer to function

Page 1 of 1

5 Replies - 427 Views - Last Post: 23 September 2009 - 07:58 AM Rate Topic: -----

#1 aries0152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 13-September 09

Function pointer problem

Post icon  Posted 23 September 2009 - 12:11 AM

I like to solve a eqn using pointer to function. But I am having some problem in using it.

My code is-


 #include<iostream>
#include<cmath>

using namespace std;

double func(double t,double f)
{
	return 1.0/sqrt(1.0-pow((sin(t/2.0)*sin(f)),2));
}

double simp(double(*f)(double),double a,double b,int n)
{
  int i;
  double sum,ini,h;
  i=0;sum=0.0;ini=0.0;
  h=(b-a)/n;
  ini+=(*f)(a);
  ini+=(*f)(b);
  for(i=1;i<=n/2.0;i++)
	{
	  ini+=4*((*f)(a+((2*i)-1)*h));
	  ini+=2*((*f)(a+((2*i)-2)*h));
	}
  sum=(1.0/3.0)*h*ini;
  return sum;
}

int main()
{
  int i=10000;
  double a=0.0,b=M_PI/2.0;

  cout<<"The integral is "<<simp(func,a,b,i)<<endl;
  return 0;
}


But when I complied this code it shows -

In function ‘int main()’:
error: invalid conversion from ‘double (*)(double, double)’ to ‘double (*)(double)’
error: initializing argument 1 of ‘double simp(double (*)(double), double, double, int)’

I am a beginner in function pointer . So please suggest me how do I call the function to get rid of this error??

Is This A Good Question/Topic? 0
  • +

Replies To: Function pointer problem

#2 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Function pointer problem

Posted 23 September 2009 - 01:09 AM

What are you trying to do? are you trying to pass a pointer or pass a function to the function?

if you want to pass a pointer then it should be double simp(double *f,double a,double b,int n)
Then you would have problems with the compiler thinking that you are using f as a function. ini+=(*f)(a); looks like a is being passed to f. if you meant to do multiplycation then use *.

If you meant to pass a function then myb.
Was This Post Helpful? 0
  • +
  • -

#3 aries0152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 13-September 09

Re: Function pointer problem

Posted 23 September 2009 - 01:44 AM

View Postcarltech, on 23 Sep, 2009 - 12:09 AM, said:

are you trying to pass a pointer or pass a function to the function?


I want to pass the function
Was This Post Helpful? 0
  • +
  • -

#4 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: Function pointer problem

Posted 23 September 2009 - 02:03 AM

if a function has two parameters, you should always pass them to it, this was one mistake committed by you.
i have edited your code where necessary and put your wrong syntax inside comments.
here you go:
#include<iostream.h>
#include<math.h>

double func(double t,double f)
{
	return 1.0/sqrt(1.0-pow((sin(t/2.0)*sin(f)),2));
}

double simp(double(*f)(double,double),double a,double b,int n)
{
  int i;
  double sum,ini,h;
  i=0;sum=0.0;ini=0.0;
  h=(b-a)/n;
  ini+=f(a,b);
  for(i=1;i<=n/2.0;i++)
	{
	  ini+=4*(f(a+((2*i)-1),h));
	  //ini+=2*((*f)(a+((2*i)-2)*h));
	}
  sum=(1.0/3.0)*h*ini;
  return sum;
}

int main()
{
  int i=10000;
  double a=0.0,b=M_PI/2.0;

  cout<<"The integral is "<<simp(func,a,b,i)<<endl;
  return 0;
}


this program compiles and runs properly..
the output may not be what you desire, that's because i have not tried to understand what you are doing but have just corrected your syntax in certain places.
you can build on this code to do exactly what you want...

This post has been edited by aks29921: 23 September 2009 - 03:03 AM

Was This Post Helpful? 1
  • +
  • -

#5 aries0152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 13-September 09

Re: Function pointer problem

Posted 23 September 2009 - 07:36 AM

Error solved. Thank you
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2222
  • View blog
  • Posts: 9,208
  • Joined: 18-February 07

Re: Function pointer problem

Posted 23 September 2009 - 07:58 AM

nice job aks29921
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1