Advertisement
Guest User

32897432789ijhdfsaakjdfgs

a guest
Apr 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5. double** mac(const int n){
  6. double** a=new double*[n];
  7. for(int i=0;i<n;i++){
  8. a[i]=new double[n];
  9. }
  10. return a;
  11. }
  12. double** man(double** a,const int n){
  13. double s;
  14. cout<<"Podaj macierz:\n";
  15. for(int i=0;i<n;i++){
  16. cout<<"Wiersz "<<i+1<<".: ";
  17. for(int j=0;j<n;j++){
  18. cin>>s;
  19. a[i][j]=s;
  20. }
  21. }
  22. return a;
  23. }
  24. void clr(double** a,const int n){
  25. for(int i=0;i<n;i++){
  26. delete[] a[i];
  27. }
  28. delete[] a;
  29. }
  30. double* wyn(const int n){
  31. double* w=new double[n];
  32. double s;
  33. cout<<"Podaj macierz wynikową: ";
  34. for(int i=0;i<n;i++){
  35. cin>>s;
  36. w[i]=s;
  37. }
  38. return w;
  39. }
  40. void w1(double** A,double* Y,const int n){
  41. for(int i=0;i<n;i++){
  42. for(int j=0;j<n;j++){
  43. cout<<setw(4)<<setprecision(3)<<A[i][j]<<" ";
  44. }
  45. if(i==n/2){
  46. cout<<" * ";
  47. }
  48. else{
  49. cout<<" ";
  50. }
  51. cout<<"X"<<i+1;
  52. if(i==n/2){
  53. cout<<" = ";
  54. }
  55. else{
  56. cout<<" ";
  57. }
  58. cout<<setw(4)<<setprecision(3)<<Y[i]<<endl;
  59. }
  60. }
  61. bool domr(double** A,const int n){
  62. double s;
  63. for(int i=0;i<n;i++){
  64. s=0;
  65. for(int j=0;j<n;j++){
  66. if(i!=j) s+=A[i][j];
  67. }
  68. if(abs(A[i][i])<=s){
  69. cerr<<"Wyrazy głównej przekątnej nie są silnie dominujące!\n";
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. void gsei(double** A,double* B,double* X,const int n,const int iter){
  76. double* Xk=new double[n];
  77. for(int k=0;k<iter;k++){
  78. for(int i=0;i<n;i++){
  79. Xk[i]=0;
  80. double s1=0,s2=0;
  81. for(int j=0;j<i;j++){
  82. s1+=A[i][j]*Xk[j];
  83. }
  84. for(int j=n-1;j>i;j--){
  85. s2+=A[i][j]*X[j];
  86. }
  87. Xk[i]=(1/A[i][i])*(B[i]-s1-s2);
  88. }
  89. cout<<"Iteracja "<<k+1<<":";
  90. for(int l=0;l<n;l++){
  91. cout<<" X"<<l+1<<": "<<setw(6)<<setprecision(3)<<Xk[l];
  92. X[l]=Xk[l];
  93. }
  94. cout<<endl;
  95. }
  96. delete[] Xk;
  97. }
  98. int main(){
  99. int n,it;
  100. cout<<"Metoda Gaussa-Seidla\n\n";
  101. cout<<"Podaj stopień macierzy: ";
  102. cin>>n;
  103. double** a=man(mac(n),n);
  104. double* b=wyn(n);
  105. double* x=new double[n];
  106. cout<<"Podaj początkowe wartości x: ";
  107. for(int i=0;i<n;i++){
  108. cin>>x[i];
  109. }
  110. cout<<"Podaj liczbę iteracji: ";
  111. cin>>it;
  112. cout<<"Podany do rozwiązania układ:\n";
  113. w1(a,b,n);
  114. if(domr(a,n)){
  115. gsei(a,b,x,n,it);
  116. }
  117. clr(a,n);
  118. delete[] b;
  119. delete[] x;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement