Advertisement
meta1211

Untitled

Dec 20th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. //Матрицы
  2. //19. По заданным коэффициентам и правым частям найти решение "треугольной" системы из n уравнений.
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9. #include <ctime>
  10.  
  11. double** create_array(int string, int column)
  12. {
  13. double **a = new double*[string];
  14. for (unsigned int i = 0; i < string; i++)
  15. {
  16. a[i] = new double[column];
  17. }
  18. return a;
  19. }
  20.  
  21. void delete_array(double **a, int string)
  22. {
  23. for (int j = 0; j < string; j++)
  24. delete[]a[j];
  25. }
  26.  
  27. void print_array(double **a, int string, int column)
  28. {
  29. for (unsigned int k = 0; k < string; k++)
  30. {
  31. printf("\n");
  32. for (unsigned int j = 0; j < column; j++)
  33. {
  34. printf("%f\t", a[k][j]);
  35. }
  36. }
  37. printf("\n");
  38. }
  39. void fill_array(double **a, int string, int column)
  40. {
  41. for (unsigned int k = 0; k < string; k++)
  42. for (unsigned int j = 0; j < column; j++)
  43. {
  44. if (j == column - 1 || j <= k)
  45. a[k][j] = rand() % 10 + 1;
  46. else
  47. a[k][j] = 0;
  48. }
  49. }
  50. double **find_solution(double **a, int string, int column)
  51. {
  52. double **arr = create_array(string, 1);
  53. for (int i = 0; i < string; i++)
  54. {
  55. arr[i][0] = 0;
  56. }
  57. arr[0][0] = a[0][column - 1] / a[0][0];
  58. for (int i = 1; i < string; i++)
  59. {
  60. arr[i][0] += a[i][column - 1];
  61. for (int j = 0; j < i; j++)
  62. {
  63. arr[i][0] -= (a[i][j] * arr[j][0]);
  64.  
  65. }
  66. arr[i][0] /= a[i][i];
  67. }
  68. return arr;
  69. }
  70.  
  71.  
  72. int main()
  73. {
  74. int string;
  75. int column;
  76. srand(time(0));
  77.  
  78. printf("put the number of unknowns, please:\n");
  79. scanf("%d", &string);
  80. column = string + 1;
  81. double **a = create_array(string, column);
  82. fill_array(a, string, column);
  83. printf("Matrix:");
  84. print_array(a, string, column);
  85. double **b = find_solution(a, string, column);
  86. printf("\nSolution:");
  87. print_array(b, string, 1);
  88.  
  89.  
  90. delete_array(a, string);
  91. delete_array(b, string);
  92. system("pause");
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement