Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- double x[100];
- double y[100][100];
- double find_p(double x1,int n)
- {
- double temp;
- int k=1,s=4;
- if(n==0)
- {
- return 1;
- }
- for(int j=1; j<=n; j++)
- {
- if(j==1)
- {
- temp=x1;
- continue;
- }
- if(j==s)
- {
- s=s+2;
- if(j%2==0)
- k++;
- }
- if(j%2==0)
- {
- temp=temp*(x1-k);
- }
- else
- {
- temp=temp*(x1+k);
- }
- }
- return temp;
- }
- int fact(int i)
- {
- int f=1;
- for(int j=i;j>=1;j--)
- {
- f*=j;
- }
- return f;
- }
- int main()
- {
- int n,p;
- int q;
- cout<<"enter the total element of x and y:";
- cin>>n;
- for(int i=0;i<n;i++)
- {
- cout<<"enter the element of x:";
- cin>>x[i];
- cout<<"enter the element of y[][]:";
- cin>>y[i][0];
- }
- for(int i=1;i<n;i++)
- {
- for(int j=0;j<n-i;j++)
- {
- y[j][i]=y[j+1][i-1]-y[j][i-1];
- }
- }
- cout<<"difference table:"<<endl;
- for(int i=0;i<n;i++)
- {
- cout<<x[i]<<"\t";
- for(int j=0;j<n-i;j++)
- {
- cout<<y[i][j]<<"\t";
- }
- cout<<endl;
- }
- if(n%2==0)
- {
- p=n/2;
- p=p-1;
- q=x[p];
- }
- else
- {
- p=n/2;
- q=x[p];
- }
- double value,x1;
- cout<<"enter desired value:";
- cin>>value;
- x1=(value-q)/(x[1]-x[0]);
- double sum=0;
- int k=1;
- cout<<q<<endl;
- for(int i=0;i<n;i++)
- {
- if(i==k+1)
- {
- k=k+2;
- p--;
- }
- sum=sum+(find_p(x1,i)*y[p][i])/fact(i);
- }
- cout<<"desired y="<<sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement