Newtons Backward formula implementation using CPP

///---------------Newtons Backward formula implementation using CPP--------------
#include<bits/stdc++.h>
using namespace std;

double fact(double n)
{
    double fct=1;
    for(int i=1;i<=n;i++){
        fct=fct*i;
    }
    return fct;
}
double fnc(double p,double n)
{
    double sum=p;
    for(int i=1;i<n;i++){
        sum=sum*(p+i);
    }
    return (sum/fact(n));
}
int main()
{
    double x[100];
    double y[100][100];
    double n;

    cout<<"Enter how many record you want to enter : ";

    cin>>n;

    cout<<endl;

    for(int i=0;i<n;i++){
        cout<<"Enter X"<<i<<" and Y"<<i<<" : ";
        cin>>x[i];
        cin>>y[0][i];
    }
    cout<<"Enter the finding value : ";
    double f;
    cin>>f;
    for(int i=1;i<n;i++){
        for(int j=0;j<(n-i);j++){
             y[i][j]=y[i-1][j+1]-y[i-1][j];
        }

    }

    cout<<"\n\nNewtons forward table : \n";
    cout<<"--------------------------------\n";
    cout<<"X\t";
    for(double i=0;i<n;i++){
        cout<<"Y("<<i<<")\t";
    }
    cout<<endl;
    for(int i=0;i<n;i++){
        cout<<"\n"<<x[i]<<"\t";
        for(int j=0;j<(n-i);j++){
            cout<<y[j][i]<<"\t";
        }
        cout<<endl;
    }
    double x0=x[0];
    double h=x[1]-x[0];
    double p=(f-x0)/h;

    cout<<"\n\nX0 is : "<<x0<<endl;
    cout<<"Height is : "<<h<<endl;
    cout<<"Finding X is : "<<f<<endl;
    cout<<"P is : "<<p<<endl;
    double sum=0;

    for(int i=2;i<n;i++){
        sum=sum+(fnc(p,i)*y[i][0]);
    }
    double ans;
    ans=sum+y[0][0]+(y[1][0]*p);
    cout<<"\n\nAns is : "<<ans<<endl;
    cout<<endl;
    return 0;
}

/// Md. Alamgir Hossain
/// Dept.of Computer Science & Engineering
/// Jessore University of Science & Technology
Previous
Next Post »