Advertisement
deced

Untitled

Sep 6th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <locale.h>
  5.  
  6. int** getMatrix(int height, int width) {
  7. int** ret;
  8. ret = (int**)malloc(sizeof(int*) * width);
  9. for (int i = 0; i < width; i++) {
  10. ret[i] =(int*) malloc(sizeof(int) * height);
  11. }
  12. for (int i = 0; i < height; i++) {
  13. for (int j = 0; j < width; j++) {
  14. printf("a[%d][%d] = ", i, j);
  15. scanf("%d", &ret[j][i]);
  16. }
  17. }
  18. return ret;
  19. }
  20.  
  21. void getSize(int* height, int* width) {
  22. *height = -1;
  23. while (*height < 2 || *height > 20) {
  24. printf("Введите высоту матрицы в диапазоне 2..20\n");
  25. scanf("%d", height);
  26. if (*height < 2 || *height > 20)
  27. printf("Ошибка\n");
  28. }
  29. *width = -1;
  30. while (*width < 2 || *width > 20) {
  31. printf("Введите ширину матрицы в диапазоне 2..20\n");
  32. scanf("%d", width);
  33. if (*width < 2 || *width > 20)
  34. printf("Ошибка\n");
  35. }
  36. }
  37.  
  38. int getCountOfZeros(int** matrix, int height, int width) {
  39. int count = 0;
  40. for (int i = 0; i < width; i++) {
  41. for (int j = 0; j < height; j++) {
  42. if (matrix[i][j] == 0)
  43. count++;
  44. }
  45. }
  46. return count;
  47. }
  48.  
  49. void printMatrix(int** matrix, int height, int width) {
  50. for (int i = 0; i < height; i++) {
  51. printf("\n");
  52. for (int j = 0; j < width; j++) {
  53. printf("%4d", matrix[j][i]);
  54. }
  55. }
  56. }
  57.  
  58. int** deleteZeros(int** matrix, int height, int width) {
  59. int colIndexes[5];
  60. colIndexes[0] = -1;
  61. colIndexes[1] = -1;
  62. colIndexes[2] = -1;
  63. colIndexes[3] = -1;
  64. colIndexes[4] = -1;
  65. int index = 0;
  66. int nullCount = 0;
  67. printf("\nИндексы нулей:\n");
  68. for (int i = 0; i < width; i++) {
  69. for (int j = 0; j < height; j++) {
  70. if (matrix[i][j] == 0 && index < 5) {
  71. colIndexes[index] = i;
  72. index++;
  73. printf("\n[%d][%d]", j, i);
  74. }
  75. }
  76. }
  77. for (int i = 0; i < 5; i++)
  78. if (colIndexes[i] != -1)
  79. matrix[colIndexes[i]] = NULL;
  80. for (int i = 0; i < width; i++)
  81. if (matrix[i] == NULL)
  82. nullCount++;
  83. width = width - nullCount;
  84. int** ret;
  85. ret = (int**)malloc(sizeof(int*) * width);
  86. int rowIndex = 0;
  87. for (int i = 0; i < width + nullCount; i++) {
  88. if (matrix[i] != NULL) {
  89. ret[rowIndex] = matrix[i];
  90. rowIndex++;
  91. }
  92. }
  93. printf("\nМатрица после удаления столбцов, содержащих нули\n");
  94. printMatrix(ret, height, width);
  95. return ret;
  96. }
  97.  
  98. int main() {
  99. char* locale = setlocale(LC_ALL, "");
  100. int height, width;
  101. getSize(&height, &width);
  102. int** matrix = getMatrix(height, width);
  103. printf("Исходная матрица\n");
  104. printMatrix(matrix, height, width);
  105. int count = getCountOfZeros(matrix, height, width);
  106. if (count < 5)
  107. printf("\nКоличество нулей меньше 5\n");
  108. deleteZeros(matrix, height, width);
  109. return 0;
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement