Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. #include <windows.h>//malloc, system("pause")
  2. #include <stdio.h> //i/o
  3. #include <conio.h> //getch
  4. #include <math.h>
  5. #include <cctype>
  6. #include <clocale>
  7.  
  8. void ShowVector(int n, double * vec);
  9. void PryamoiHod(int n, double **a, double *b);
  10. void ObratniHod(int n, double **a, double *b, double *x);
  11.  
  12. int main()
  13. {
  14. int i,j,n;
  15. double **a, *b, *x;
  16. do
  17. {
  18. printf("Enter NUM of equations: ");
  19. scanf("%d",&n);
  20. //Выделяем память под матрицу А и векторы В и Х
  21. a = (double **)malloc(n*sizeof(double));
  22. b = (double *)malloc(n*sizeof(double));
  23. x = (double *)malloc(n*sizeof(double));
  24. for(i = 0; i < n; i++)
  25. {
  26. a[i] = (double *)malloc(n*sizeof(double));
  27. //Ввод a
  28. for(j = 0; j < n; j++)
  29. {
  30. printf("a[%d][%d] = ",i + 1,j + 1);
  31. scanf("%lf",&a[i][j]);
  32. }
  33. }
  34. //Ввод b
  35. for(i = 0; i < n; i++)
  36. {
  37. printf("b[%d] = ",i + 1);
  38. scanf("%lf",&b[i]);
  39. }
  40.  
  41. printf("\tSee input\r\n");
  42. printf("Matrix A:\r\n");
  43. for(i = 0; i < n; i++)
  44. ShowVector(n, a[i]);
  45. printf("Vector B:\r\n");
  46. ShowVector(n, b);
  47.  
  48. printf("\tSolving on Gauss method\r\n");
  49. PryamoiHod(n, a, b);
  50. printf("Forvard Gauss course\r\n");//Прямой ход
  51. printf("Matrix A:\r\n");
  52. for(i = 0; i < n; i++)
  53. ShowVector(n, a[i]);
  54. printf("Vector B:\r\n");
  55. ShowVector(n, b);
  56.  
  57. ObratniHod(n, a, b, x);
  58. printf("Back Gauss course\r\n");//Обратный ход
  59. printf("Matrix A:\r\n");
  60. for(i = 0; i < n; i++)
  61. ShowVector(n, a[i]);
  62. printf("Vector B:\r\n");
  63. ShowVector(n, b);
  64.  
  65. printf("Results :\r\n");
  66. ShowVector(n, x);
  67.  
  68. printf("Press Y for new input\r\n");
  69. //Чистим память
  70. free((void *)a);
  71. free((void *)b);
  72. free((void *)x);
  73. }
  74. while(toupper(getch()) == 'Y');
  75. return 0;
  76. }
  77.  
  78. void ShowVector(int n, double * vec)
  79. {
  80. for(int i = 0; i < n; i++)
  81. printf("%.3f ",vec[i]);
  82. printf("\r\n");
  83. }
  84.  
  85. void PryamoiHod(int n, double **a, double *b)
  86. {
  87. double v;
  88. for(int k = 0,i,j,im; k < n - 1; k++)
  89. {
  90. im = k;
  91. for(i = k + 1; i < n; i++)
  92. {
  93. if(fabs(a[im][k]) < fabs(a[i][k]))
  94. {
  95. im = i;
  96. }
  97. }
  98. if(im != k)
  99. {
  100. for(j = 0; j < n; j++)
  101. {
  102. v = a[im][j];
  103. a[im][j] = a[k][j];
  104. a[k][j] = v;
  105. }
  106. v = b[im];
  107. b[im] = b[k];
  108. b[k] = v;
  109. }
  110. for(i = k + 1; i < n; i++)
  111. {
  112. v = 1.0*a[i][k]/a[k][k];
  113. a[i][k] = 0;
  114. b[i] = b[i] - v*b[k];
  115. if(v != 0)
  116. for(j = k + 1; j < n; j++)
  117. {
  118. a[i][j] = a[i][j] - v*a[k][j];
  119. }
  120. }
  121. }
  122. }
  123.  
  124. void ObratniHod(int n, double **a, double *b, double *x)
  125. {
  126. double s = 0;
  127. x[n - 1] = 1.0*b[n - 1]/a[n - 1][n - 1];
  128. for(int i = n - 2, j; 0 <= i; i--)
  129. {
  130. s = 0;
  131. for(j = i + 1; j < n; j++)
  132. {
  133. s = s+a[i][j]*x[j];
  134. }
  135. x[i] = 1.0*(b[i] - s)/a[i][i];
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement