Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. //lab4 - interpolacja newtona
  5. //Michal J. Sidor gr 3.5
  6.  
  7. using namespace std;
  8.  
  9. struct data
  10. {
  11. double x,f;
  12. };
  13.  
  14. int wprowadz(int n, data *tab);
  15. void wyswietl(int n, data *tab);
  16. void insertSort(int n, data *tab);
  17. int interpolacja(int n, data *tab);
  18.  
  19.  
  20. int main()
  21. {
  22. int n;
  23. cout<<"Ile wezlow?: "; cin>>n;
  24. data *tab = new data[n];
  25. if (wprowadz(n, tab) == 1)
  26. {
  27. insertSort(n, tab);
  28. wyswietl(n, tab);
  29. interpolacja(n, tab);
  30. }
  31.  
  32. return 0;
  33. }
  34.  
  35.  
  36. int wprowadz(int n, data *tab)
  37. {
  38. cout<<endl<<"Podawanie danych: (wezly musza byc od siebie rozne)"<<endl;
  39. for (int i=0; i<n; i++)
  40. {
  41. cout<<"Podaj x"<<i<<": "; cin>>tab[i].x;
  42. for (int j=i; j>0; j--)
  43. {
  44. if (tab[i].x == tab[j-1].x)
  45. {
  46. cout<<"Nie spelniono warunkow przy wprowadzaniu wezlow, przerywam dzialanie programu..."<<endl;
  47. delete tab;
  48. return 0;
  49. }
  50. }
  51. cout<<"Podaj f"<<i<<": "; cin>>tab[i].f;
  52. }
  53. cout<<endl;
  54. return 1;
  55. }
  56.  
  57. void wyswietl(int n, data *tab)
  58. {
  59. for (int i=0; i<n; i++)
  60. {
  61. cout<<"x"<<i<<" = "<<tab[i].x<<"; f"<<i<<" = "<<tab[i].f<<endl;
  62. }
  63. }
  64.  
  65. void insertSort(int n, data *tab)
  66. {
  67. int j;
  68. double temp;
  69. for (int i=1; i<=n-1; i++)
  70. {
  71. j=i;
  72. while (tab[j].x<tab[j-1].x and j>0)
  73. {
  74. temp = tab[j-1].x;
  75. tab[j-1].x = tab[j].x;
  76. tab[j].x = temp;
  77. temp = tab[j-1].f;
  78. tab[j-1].f = tab[j].f;
  79. tab[j].f = temp;
  80. j--;
  81. }
  82. }
  83. }
  84.  
  85. int interpolacja(int n, data *tab)
  86. {
  87. float szukana;
  88. double R[n],wynik,mnoznik=1;
  89. cout<<endl<<"Podaj x dla ktorego szukasz wartosci (musi byc zawarty w przedziale pomiedzy maksymalnym a minimalnym wezlem): ";
  90. cin>>szukana;
  91. if (szukana >= tab[0].x and szukana <= tab[n-1].x)
  92. {
  93. for (int i=0; i<n; i++)
  94. {
  95. R[i] = tab[i].f;
  96. }
  97.  
  98. for (int k=1; k<n; k++)
  99. {
  100. for (int j=n-1; j>=k; j--)
  101. {
  102. R[j] = (R[j] - R[j-1]) / (tab[j].x - tab[j-k].x);
  103. }
  104. }
  105. cout<<endl<<"Wartosci f[...]:"<<endl<<"**********************"<<endl;
  106. for (int i=0; i<n; i++)
  107. {
  108. cout<<"R["<<i<<"] = "<<R[i]<<endl;
  109. }
  110.  
  111. for (int i=0; i<n; i++)
  112. {
  113. wynik+=R[i]*mnoznik;
  114. mnoznik=mnoznik*(szukana-tab[i].x);
  115. }
  116. cout<<"*********************************"<<endl;
  117. cout<<"Wg interpolacji Newtona dla funkcji o podanych wezlach f(";
  118. cout<<szukana;
  119. cout<<") = "<<wynik<<endl;
  120.  
  121. }
  122. else
  123. {
  124. cout<<"Nie spelniono warunku przy podawaniu szukanego x, przerywam dzialanie programu..."<<endl;
  125. delete tab;
  126. return 0;
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement