fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_SIZE 100
  5.  
  6. // Прототипы функций
  7. int **allocate_matrix_dynamic(int rows, int cols);
  8. void free_matrix_dynamic(int **matrix, int rows);
  9. void read_matrix(int **matrix, int rows, int cols);
  10. void print_matrix(int **matrix, int rows, int cols);
  11. void add_matrices(int **A, int **B, int **C, int rows, int cols);
  12. void multiply_matrices(int **A, int **B, int **C, int rowsA, int colsA, int colsB);
  13. void transpose_matrix(int **A, int **B, int rows, int cols);
  14.  
  15. int main() {
  16. int operation;
  17. printf("Выберите операцию:\\n1 - Сложение матриц\\n2 - Умножение матриц\\n3 - Транспонирование матрицы\\n");
  18. if (scanf("%d", &operation) != 1 || operation < 1 || operation > 3) {
  19. printf("n/a\\n");
  20. return 1; // Ошибка выбора
  21. }
  22.  
  23. if (operation == 1) {
  24. // Сложение матриц
  25. int rows, cols;
  26. if (scanf("%d %d", &rows, &cols) != 2 || rows < 1 || cols < 1 || rows > MAX_SIZE || cols > MAX_SIZE) {
  27. printf("n/a\\n");
  28. return 1; // Ошибка при считывании размеров матриц
  29. }
  30.  
  31. int **A = allocate_matrix_dynamic(rows, cols);
  32. int **B = allocate_matrix_dynamic(rows, cols);
  33. int **C = allocate_matrix_dynamic(rows, cols);
  34.  
  35. if (A == NULL || B == NULL || C == NULL) {
  36. printf("n/a\\n");
  37. return 1; // Ошибка при выделении памяти
  38. }
  39.  
  40. read_matrix(A, rows, cols);
  41. read_matrix(B, rows, cols);
  42. add_matrices(A, B, C, rows, cols);
  43. print_matrix(C, rows, cols);
  44.  
  45. free_matrix_dynamic(A, rows);
  46. free_matrix_dynamic(B, rows);
  47. free_matrix_dynamic(C, rows);
  48. } else if (operation == 2) {
  49. // Умножение матриц
  50. int rowsA, colsA, rowsB, colsB;
  51. if (scanf("%d %d", &rowsA, &colsA) != 2 || rowsA < 1 || colsA < 1 || rowsA > MAX_SIZE || colsA > MAX_SIZE) {
  52. printf("n/a\\n");
  53. return 1; // Ошибка при считывании размеров первой матрицы
  54. }
  55.  
  56. if (scanf("%d %d", &rowsB, &colsB) != 2 || rowsB < 1 || colsB < 1 || rowsB > MAX_SIZE || colsB > MAX_SIZE) {
  57. printf("n/a\\n");
  58. return 1; // Ошибка при считывании размеров второй матрицы
  59. }
  60.  
  61. if (colsA != rowsB) {
  62. printf("n/a\\n");
  63. return 1; // Ошибка, если количество столбцов первой матрицы не совпадает с количеством строк второй
  64. }
  65.  
  66. int **A = allocate_matrix_dynamic(rowsA, colsA);
  67. int **B = allocate_matrix_dynamic(rowsB, colsB);
  68. int **C = allocate_matrix_dynamic(rowsA, colsB);
  69.  
  70. if (A == NULL || B == NULL || C == NULL) {
  71. printf("n/a\\n");
  72. return 1; // Ошибка при выделении памяти
  73. }
  74.  
  75. read_matrix(A, rowsA, colsA);
  76. read_matrix(B, rowsB, colsB);
  77. multiply_matrices(A, B, C, rowsA, colsA, colsB);
  78. print_matrix(C, rowsA, colsB);
  79.  
  80. free_matrix_dynamic(A, rowsA);
  81. free_matrix_dynamic(B, rowsB);
  82. free_matrix_dynamic(C, rowsA);
  83. } else if (operation == 3) {
  84. // Транспонирование матрицы
  85. int rows, cols;
  86. if (scanf("%d %d", &rows, &cols) != 2 || rows < 1 || cols < 1 || rows > MAX_SIZE || cols > MAX_SIZE) {
  87. printf("n/a\\n");
  88. return 1; // Ошибка при считывании размеров матрицы
  89. }
  90.  
  91. int **A = allocate_matrix_dynamic(rows, cols);
  92. int **B = allocate_matrix_dynamic(cols, rows); // Транспонированная матрица
  93.  
  94. if (A == NULL || B == NULL) {
  95. printf("n/a\\n");
  96. return 1; // Ошибка при выделении памяти
  97. }
  98.  
  99. read_matrix(A, rows, cols);
  100. transpose_matrix(A, B, rows, cols);
  101. print_matrix(B, cols, rows);
  102.  
  103. free_matrix_dynamic(A, rows);
  104. free_matrix_dynamic(B, cols);
  105. }
  106.  
  107. return 0;
  108. }
  109.  
  110. // Динамическое выделение: 2D массив
  111. int **allocate_matrix_dynamic(int rows, int cols) {
  112. int **matrix = malloc(rows * sizeof(int *));
  113. if (matrix == NULL) return NULL;
  114.  
  115. for (int i = 0; i < rows; i++) {
  116. matrix[i] = malloc(cols * sizeof(int));
  117. if (matrix[i] == NULL) {
  118. for (int j = 0; j < i; j++) {
  119. free(matrix[j]);
  120. }
  121. free(matrix);
  122. return NULL;
  123. }
  124. }
  125. return matrix;
  126. }
  127.  
  128. // Освобождение памяти для динамической матрицы
  129. void free_matrix_dynamic(int **matrix, int rows) {
  130. for (int i = 0; i < rows; i++) {
  131. free(matrix[i]);
  132. }
  133. free(matrix);
  134. }
  135.  
  136. // Чтение матрицы
  137. void read_matrix(int **matrix, int rows, int cols) {
  138. for (int i = 0; i < rows; i++) {
  139. for (int j = 0; j < cols; j++) {
  140. if (scanf("%d", &matrix[i][j]) != 1) {
  141. printf("n/a\\n");
  142. exit(1); // Ошибка при считывании
  143. }
  144. }
  145. }
  146. }
  147.  
  148. // Печать матрицы
  149. void print_matrix(int **matrix, int rows, int cols) {
  150. for (int i = 0; i < rows; i++) {
  151. for (int j = 0; j < cols; j++) {
  152. printf("%d", matrix[i][j]);
  153. if (j < cols - 1) {
  154. printf(" ");
  155. }
  156. }
  157. if (i < rows - 1) {
  158. printf("\\n");
  159. }
  160. }
  161. }
  162.  
  163. // Сложение матриц
  164. void add_matrices(int **A, int **B, int **C, int rows, int cols) {
  165. for (int i = 0; i < rows; i++) {
  166. for (int j = 0; j < cols; j++) {
  167. C[i][j] = A[i][j] + B[i][j];
  168. }
  169. }
  170. }
  171.  
  172. // Умножение матриц
  173. void multiply_matrices(int **A, int **B, int **C, int rowsA, int colsA, int colsB) {
  174. for (int i = 0; i < rowsA; i++) {
  175. for (int j = 0; j < colsB; j++) {
  176. C[i][j] = 0; // Инициализация элемента C
  177. for (int k = 0; k < colsA; k++) {
  178. C[i][j] += A[i][k] * B[k][j];
  179. }
  180. }
  181. }
  182. }
  183.  
  184. // Транспонирование матрицы
  185. void transpose_matrix(int **A, int **B, int rows, int cols) {
  186. for (int i = 0; i < rows; i++) {
  187. for (int j = 0; j < cols; j++) {
  188. B[j][i] = A[i][j];
  189. }
  190. }
  191. }
Success #stdin #stdout 0.01s 5284KB
stdin
1
2 2
4 3
9 0
1 1
2 2
stdout
Выберите операцию:\n1 - Сложение матриц\n2 - Умножение матриц\n3 - Транспонирование матрицы\n5 4\n11 2