Advertisement
Guest User

ddd

a guest
Apr 27th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void main(){
  5. int n, i, j, count = 0;
  6. float **a, *b, *x, *tmp_x, exp, e = 0.0001;
  7.  
  8. cout << "Matrix Size\n";
  9. cout << "n = ";
  10. cin >> n;
  11.  
  12. a = new float*[n];
  13. for (i = 0; i < n; i++){
  14. a[i] = new float[n];
  15. }
  16.  
  17.  
  18. b = new float[n];
  19. x = new float[n];
  20. tmp_x = new float[n];
  21.  
  22. cout << "Write Matrix\n";
  23. for (i = 0; i < n; i++){
  24. for (j = 0; j < n; j++){
  25. cin >> a[i][j];
  26. }
  27. }
  28.  
  29. cout << "Free elements\n";
  30. for (i = 0; i < n; i++){
  31. cin >> b[i];;
  32. x[i] = 0;
  33. }
  34.  
  35. do{
  36. count++;
  37.  
  38. for (i = 0; i < n; i++){
  39. tmp_x[i] = 0.0;
  40. for (j = 0; j < n; j++){
  41. if (i != j){
  42. tmp_x[i] = tmp_x[i] + (a[i][j] * x[j]);
  43. }
  44. }
  45. tmp_x[i] = (b[i] - tmp_x[i]) / a[i][i];
  46. }
  47.  
  48. exp = 0;
  49.  
  50. for (i = 0; i < n; i++){
  51. if (fabs(x[i] - tmp_x[i]) > exp){
  52. exp = fabs(x[i] - tmp_x[i]);
  53. }
  54. x[i] = tmp_x[i];
  55. }
  56. } while (exp > e);
  57.  
  58. delete [] tmp_x;
  59. for (i = 0; i < n; i++){
  60. delete a[i];
  61. }
  62. delete [] a;
  63. delete [] b;
  64.  
  65. printf("Answer\n");
  66. for (i = 0; i < n; i++){
  67. printf("x[%d] = %.6f\n", i + 1, x[i]);
  68. }
  69. cout << "Number of iterations " << count << endl;
  70. delete[] x;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement