Advertisement
sajid161

gauss for

Mar 14th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. double x[100];
  4. double y[100][100];
  5. double find_p(double x1,int n)
  6. {
  7. double temp;
  8. int k=1,s=4;
  9. if(n==0)
  10. {
  11. return 1;
  12. }
  13. for(int j=1; j<=n; j++)
  14. {
  15. if(j==1)
  16. {
  17. temp=x1;
  18. continue;
  19. }
  20. if(j==s)
  21. {
  22. s=s+2;
  23. if(j%2==0)
  24. k++;
  25. }
  26. if(j%2==0)
  27. {
  28. temp=temp*(x1-k);
  29.  
  30. }
  31. else
  32. {
  33. temp=temp*(x1+k);
  34.  
  35. }
  36.  
  37. }
  38.  
  39. return temp;
  40.  
  41. }
  42.  
  43.  
  44. int fact(int i)
  45. {
  46. int f=1;
  47. for(int j=i;j>=1;j--)
  48. {
  49. f*=j;
  50.  
  51. }
  52. return f;
  53. }
  54. int main()
  55. {
  56. int n,p;
  57. int q;
  58. cout<<"enter the total element of x and y:";
  59. cin>>n;
  60. for(int i=0;i<n;i++)
  61. {
  62. cout<<"enter the element of x:";
  63. cin>>x[i];
  64. cout<<"enter the element of y[][]:";
  65. cin>>y[i][0];
  66.  
  67. }
  68. for(int i=1;i<n;i++)
  69. {
  70. for(int j=0;j<n-i;j++)
  71. {
  72. y[j][i]=y[j+1][i-1]-y[j][i-1];
  73.  
  74. }
  75. }
  76. cout<<"difference table:"<<endl;
  77. for(int i=0;i<n;i++)
  78. {
  79. cout<<x[i]<<"\t";
  80. for(int j=0;j<n-i;j++)
  81. {
  82. cout<<y[i][j]<<"\t";
  83. }
  84. cout<<endl;
  85. }
  86. if(n%2==0)
  87. {
  88. p=n/2;
  89. p=p-1;
  90. q=x[p];
  91. }
  92. else
  93. {
  94. p=n/2;
  95. q=x[p];
  96. }
  97. double value,x1;
  98. cout<<"enter desired value:";
  99. cin>>value;
  100. x1=(value-q)/(x[1]-x[0]);
  101. double sum=0;
  102. int k=1;
  103. cout<<q<<endl;
  104.  
  105. for(int i=0;i<n;i++)
  106. {
  107. if(i==k+1)
  108. {
  109. k=k+2;
  110. p--;
  111.  
  112. }
  113.  
  114. sum=sum+(find_p(x1,i)*y[p][i])/fact(i);
  115.  
  116. }
  117. cout<<"desired y="<<sum;
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement