I've just read up about pointers, so wanted to see if I can try and use them in my program.

I have the function "equation", which at present is t^2. I also have the function "gauss", in which I am going to use the equation t^2, (code for this is not written yet). I was hoping to be able to use a pointer so that if I change the function "equation" to t^3 + t + 1 for example, I wont need to change the code for "gauss".

Could someone please tell me if this is the correct set up? Not sure if gauss should be defined within "double equation (double t)" or separately as I have done below?

Thanks in advance!

#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>

using namespace std;

typedef double(*fun)(double);

//Declare functions
double gauss(fun f, double a, double b, int n);
double equation(double t);


//=============================================================================================


int main()
{  
    gauss(equation,a,b,n);

    return 0;
}

//============================================================================================

double gauss(fun f, double a, double b, int n)
{
    
}

//=============================================================================================

double equation (double t)
{
    return t*t;
}

Recommended Answers

All 5 Replies

Why do you need pointers?

Now just do something with the gauss function, like so :

double gauss(fun f, double a, double b, int n){
  return ( (*f)(a) + (*f)(b) )  * n;
}

The above is just a random example.

Thanks for the reply firstPerson, I'm not sure that I actually need to

I've now added something to the gauss function, however the cin doesn't seem to be working - I'm getting the initialised value of 0 for n no matter what I enter. I can't see anything wrong with the way it is written I'm assuming it must be something to do with the pointer? Could somebody please tell me what is wrong?

#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>

using namespace std;

typedef double(*fun)(double);

//Declare functions
double gauss(fun f, double a, double b, int n);
double equation(double t);


//============================================================================================


int main()
{
    double a=0.0;
    double b=0.0;
    int n=0;
    vector<double> abscisass(29,0);
    vector<double> weights(29,0);

    gauss(equation,a,b,n);

    cout << "the value of n is " << n << endl;

    return 0;
}

//=============================================================================================

double gauss(fun f, double a, double b, int n)
{
    cout << "\n********** GAUSS QUADRATURE **********"<< endl;
    cout << "For the equation f(t) = t^2" << endl;
    cout << "Enter the quadrature order, n you wish to use: " << endl;
    cin >> n;
    cout << "Enter the interval of integration, [a,b]: " << endl;
    cin >> a >> b;
}

//=============================================================================================

double equation (double t)
{
    return t*t;
}

You've passed the function pointer but where are you using it?

however the cin doesn't seem to be working - I'm getting the initialised value of 0 for n no matter what I enter.

That's because you are passing n by value. You need to pass it by reference instead, i.e.

double gauss(fun f, double a, double b, int & n)
{
    cout << "\n********** GAUSS QUADRATURE **********"<< endl;
    cout << "For the equation f(t) = t^2" << endl;
    cout << "Enter the quadrature order, n you wish to use: " << endl;
    cin >> n;
    cout << "Enter the interval of integration, [a,b]: " << endl;
    cin >> a >> b;
}

Thank you mitrmkar :)

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.