Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- int** getMatrix(int height, int width) {
- int** ret;
- ret = (int**)malloc(sizeof(int*) * width);
- for (int i = 0; i < width; i++) {
- ret[i] =(int*) malloc(sizeof(int) * height);
- }
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- printf("a[%d][%d] = ", i, j);
- scanf("%d", &ret[j][i]);
- }
- }
- return ret;
- }
- void getSize(int* height, int* width) {
- *height = -1;
- while (*height < 2 || *height > 20) {
- printf("Введите высоту матрицы в диапазоне 2..20\n");
- scanf("%d", height);
- if (*height < 2 || *height > 20)
- printf("Ошибка\n");
- }
- *width = -1;
- while (*width < 2 || *width > 20) {
- printf("Введите ширину матрицы в диапазоне 2..20\n");
- scanf("%d", width);
- if (*width < 2 || *width > 20)
- printf("Ошибка\n");
- }
- }
- int getCountOfZeros(int** matrix, int height, int width) {
- int count = 0;
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- if (matrix[i][j] == 0)
- count++;
- }
- }
- return count;
- }
- void printMatrix(int** matrix, int height, int width) {
- for (int i = 0; i < height; i++) {
- printf("\n");
- for (int j = 0; j < width; j++) {
- printf("%4d", matrix[j][i]);
- }
- }
- }
- int** deleteZeros(int** matrix, int height, int width) {
- int colIndexes[5];
- colIndexes[0] = -1;
- colIndexes[1] = -1;
- colIndexes[2] = -1;
- colIndexes[3] = -1;
- colIndexes[4] = -1;
- int index = 0;
- int nullCount = 0;
- printf("\nИндексы нулей:\n");
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- if (matrix[i][j] == 0 && index < 5) {
- colIndexes[index] = i;
- index++;
- printf("\n[%d][%d]", j, i);
- }
- }
- }
- for (int i = 0; i < 5; i++)
- if (colIndexes[i] != -1)
- matrix[colIndexes[i]] = NULL;
- for (int i = 0; i < width; i++)
- if (matrix[i] == NULL)
- nullCount++;
- width = width - nullCount;
- int** ret;
- ret = (int**)malloc(sizeof(int*) * width);
- int rowIndex = 0;
- for (int i = 0; i < width + nullCount; i++) {
- if (matrix[i] != NULL) {
- ret[rowIndex] = matrix[i];
- rowIndex++;
- }
- }
- printf("\nМатрица после удаления столбцов, содержащих нули\n");
- printMatrix(ret, height, width);
- return ret;
- }
- int main() {
- char* locale = setlocale(LC_ALL, "");
- int height, width;
- getSize(&height, &width);
- int** matrix = getMatrix(height, width);
- printf("Исходная матрица\n");
- printMatrix(matrix, height, width);
- int count = getCountOfZeros(matrix, height, width);
- if (count < 5)
- printf("\nКоличество нулей меньше 5\n");
- deleteZeros(matrix, height, width);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement