fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_SIZE 100
  5.  
  6. void read_matrix_static(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols);
  7. void print_matrix_static(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols);
  8.  
  9. int **allocate_matrix_dynamic1(int rows, int cols);
  10. void read_matrix_dynamic1(int **matrix, int rows, int cols);
  11. void print_matrix_dynamic1(int **matrix, int rows, int cols);
  12. void free_matrix_dynamic1(int **matrix, int rows);
  13.  
  14. int *allocate_matrix_dynamic2(int rows, int cols);
  15. void read_matrix_dynamic2(int *matrix, int rows, int cols);
  16. void print_matrix_dynamic2(int *matrix, int rows, int cols);
  17. void free_matrix_dynamic2(int *matrix);
  18.  
  19. int **allocate_matrix_dynamic3(int rows, int cols);
  20. void read_matrix_dynamic3(int **matrix, int rows, int cols);
  21. void print_matrix_dynamic3(int **matrix, int rows, int cols);
  22. void free_matrix_dynamic3(int **matrix, int rows);
  23.  
  24. int main() {
  25. int choice, rows, cols;
  26. printf("Выберите метод выделения памяти:\n1. Статически\n2. Динамически (2D массив указателей)\n3. Динамически (1D массив)\n4. Динамически (массив указателей на строки)\n");
  27.  
  28. if (scanf("%d", &choice) != 1 || choice < 1 || choice > 4) {
  29. printf("n/a\n");
  30. return 1;
  31. }
  32.  
  33. if (scanf("%d %d", &rows, &cols) != 2 || rows < 1 || cols < 1 || rows > MAX_SIZE || cols > MAX_SIZE) {
  34. printf("n/a\n");
  35. return 1;
  36. }
  37.  
  38. switch (choice) {
  39. case 1: {
  40. int matrix[MAX_SIZE][MAX_SIZE];
  41. read_matrix_static(matrix, rows, cols);
  42. print_matrix_static(matrix, rows, cols);
  43. break;
  44. }
  45. case 2: {
  46. int **matrix = allocate_matrix_dynamic1(rows, cols);
  47. if (matrix == NULL) {
  48. printf("n/a\n");
  49. return 1;
  50. }
  51. read_matrix_dynamic1(matrix, rows, cols);
  52. print_matrix_dynamic1(matrix, rows, cols);
  53. free_matrix_dynamic1(matrix, rows);
  54. break;
  55. }
  56. case 3: {
  57. int *matrix = allocate_matrix_dynamic2(rows, cols);
  58. if (matrix == NULL) {
  59. printf("n/a\n");
  60. return 1;
  61. }
  62. read_matrix_dynamic2(matrix, rows, cols);
  63. print_matrix_dynamic2(matrix, rows, cols);
  64. free_matrix_dynamic2(matrix);
  65. break;
  66. }
  67. case 4: {
  68. int **matrix = allocate_matrix_dynamic3(rows, cols);
  69. if (matrix == NULL) {
  70. printf("n/a\n");
  71. return 1;
  72. }
  73. read_matrix_dynamic3(matrix, rows, cols);
  74. print_matrix_dynamic3(matrix, rows, cols);
  75. free_matrix_dynamic3(matrix, rows);
  76. break;
  77. }
  78. }
  79.  
  80. return 0;
  81. }
  82.  
  83. void read_matrix_static(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols) {
  84. for (int i = 0; i < rows; i++) {
  85. for (int j = 0; j < cols; j++) {
  86. if (scanf("%d", &matrix[i][j]) != 1) {
  87. printf("n/a\n");
  88. exit(1);
  89. }
  90. }
  91. }
  92. }
  93.  
  94. void print_matrix_static(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols) {
  95. for (int i = 0; i < rows; i++) {
  96. for (int j = 0; j < cols; j++) {
  97. printf("%d", matrix[i][j]);
  98. if (j < cols - 1) {
  99. printf(" ");
  100. }
  101. }
  102. if (i < rows - 1) {
  103. printf("\n");
  104. }
  105. }
  106. }
  107.  
  108. int **allocate_matrix_dynamic1(int rows, int cols) {
  109. int **matrix = malloc(rows * sizeof(int*));
  110. if (matrix == NULL) return NULL;
  111.  
  112. for (int i = 0; i < rows; i++) {
  113. matrix[i] = malloc(cols * sizeof(int));
  114. if (matrix[i] == NULL) {
  115. for (int j = 0; j < i; j++) {
  116. free(matrix[j]);
  117. }
  118. free(matrix);
  119. return NULL;
  120. }
  121. }
  122. return matrix;
  123. }
  124.  
  125. void read_matrix_dynamic1(int **matrix, int rows, int cols) {
  126. for (int i = 0; i < rows; i++) {
  127. for (int j = 0; j < cols; j++) {
  128. if (scanf("%d", &matrix[i][j]) != 1) {
  129. printf("n/a\n");
  130. exit(1);
  131. }
  132. }
  133. }
  134. }
  135.  
  136. void print_matrix_dynamic1(int **matrix, int rows, int cols) {
  137. for (int i = 0; i < rows; i++) {
  138. for (int j = 0; j < cols; j++) {
  139. printf("%d", matrix[i][j]);
  140. if (j < cols - 1) {
  141. printf(" ");
  142. }
  143. }
  144. if (i < rows - 1) {
  145. printf("\n");
  146. }
  147. }
  148. }
  149.  
  150. void free_matrix_dynamic1(int **matrix, int rows) {
  151. for (int i = 0; i < rows; i++) {
  152. free(matrix[i]);
  153. }
  154. free(matrix);
  155. }
  156.  
  157. int *allocate_matrix_dynamic2(int rows, int cols) {
  158. return malloc(rows * cols * sizeof(int));
  159. }
  160.  
  161. void read_matrix_dynamic2(int *matrix, int rows, int cols) {
  162. for (int i = 0; i < rows; i++) {
  163. for (int j = 0; j < cols; j++) {
  164. if (scanf("%d", &matrix[i * cols + j]) != 1) {
  165. printf("n/a\n");
  166. exit(1);
  167. }
  168. }
  169. }
  170. }
  171.  
  172. void print_matrix_dynamic2(int *matrix, int rows, int cols) {
  173. for (int i = 0; i < rows; i++) {
  174. for (int j = 0; j < cols; j++) {
  175. printf("%d", matrix[i * cols + j]);
  176. if (j < cols - 1) {
  177. printf(" ");
  178. }
  179. }
  180. if (i < rows - 1) {
  181. printf("\n");
  182. }
  183. }
  184. }
  185.  
  186. void free_matrix_dynamic2(int *matrix) {
  187. free(matrix);
  188. }
  189.  
  190. int **allocate_matrix_dynamic3(int rows, int cols) {
  191. int **matrix = malloc(rows * sizeof(int*));
  192. if (matrix == NULL) return NULL;
  193.  
  194. for (int i = 0; i < rows; i++) {
  195. matrix[i] = malloc(cols * sizeof(int));
  196. if (matrix[i] == NULL) {
  197. for (int j = 0; j < i; j++) {
  198. free(matrix[j]);
  199. }
  200. free(matrix);
  201. return NULL;
  202. }
  203. }
  204. return matrix;
  205. }
  206.  
  207. void read_matrix_dynamic3(int **matrix, int rows, int cols) {
  208. for (int i = 0; i < rows; i++) {
  209. for (int j = 0; j < cols; j++) {
  210. if (scanf("%d", &matrix[i][j]) != 1) {
  211. printf("n/a\n");
  212. exit(1);
  213. }
  214. }
  215. }
  216. }
  217.  
  218. void print_matrix_dynamic3(int **matrix, int rows, int cols) {
  219. for (int i = 0; i < rows; i++) {
  220. for (int j = 0; j < cols; j++) {
  221. printf("%d", matrix[i][j]);
  222. if (j < cols - 1) {
  223. printf(" ");
  224. }
  225. }
  226. if (i < rows - 1) {
  227. printf("\n");
  228. }
  229. }
  230. }
  231.  
  232. void free_matrix_dynamic3(int **matrix, int rows) {
  233. for (int i = 0; i < rows; i++) {
  234. free(matrix[i]);
  235. }
  236. free(matrix);
  237. }
Success #stdin #stdout 0.01s 5280KB
stdin
2
2 2
4 3
9 0
stdout
Выберите метод выделения памяти:
1. Статически
2. Динамически (2D массив указателей)
3. Динамически (1D массив)
4. Динамически (массив указателей на строки)
4 3
9 0