0

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;
}

Edited by sexyzebra19: n/a

4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by sexyzebra19
Featured Replies
  • 1

    [QUOTE=sexyzebra19;1113432] however the cin doesn't seem to be working - I'm getting the initialised value of 0 for n no matter what I enter.[/QUOTE] That's because you are passing n by value. You need to pass it by reference instead, i.e. [code] double gauss(fun f, double a, double b, int … Read More

0

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.

Edited by firstPerson: n/a

0

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;
}

Edited by sexyzebra19: n/a

1

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;
}
This question has already been answered. 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.