【问题标题】:C language how to remove or not show rows with 0 value columnC语言如何删除或不显示具有0值列的行
【发布时间】:2021-04-16 04:00:26
【问题描述】:

例如,我们有这个

0 1 0 1
0 3 1 0
0 1 5 0
0 0 0 0

矩阵我只想显示非零元素的行和列 最终的矩阵视图应该是这样的

1 0 1
3 1 0
1 5 0

如何删除零元素的行和列 我的代码

#include <stdio.h>

int main()
{
    int a[100],row,col,i,j,count=0,k=0;
    scanf("%d %d", &row, &col);
    int matrix[row][col];
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            scanf("%d", &matrix[i][j]);
        }
    }
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if(matrix[i][j]==0)
            {
                count++;    
            }
        }
    }
            for (i = 0; i < row; i++)
                {
                        for (j = 0; j < col; j++)
                        {
                            printf("%d ", matrix[i][j]);
                        }
                    printf("\n");
                }

}

return 0;   
}

我应该为count++设置什么条件我是c开发人员的新人。

【问题讨论】:

  • 为什么还要使用计数器?只需将条件移动到打印循环,然后翻转它(不)
  • 你的代码编译了吗?我相信还有一个额外的}
  • 要达到您想要的效果,您必须至少进行 2 次通过。 1. 如果行中至少有一个值不是0,则有一些display_row 数组变量不应设置为0。 2.第二遍:只显示display_row[i]不是0的矩阵。
  • @sagi 我做到了,但只处理全零 0 行感谢全零列我需要什么条件?

标签: arrays c matrix 2d


【解决方案1】:
  1. 判断是否应显示每一行和每一列。
  2. 仅打印应显示的行和列。
#include <stdio.h>

int main(void)
{
    int row,col,i,j;

    // read the matrix
    if (scanf("%d %d", &row, &col) != 2)
    {
        fputs("read error\n", stderr);
        return 1;
    }
    int matrix[row][col];
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (scanf("%d", &matrix[i][j]) != 1)
            {
                fputs("read error\n", stderr);
                return 1;
            }
        }
    }

    // judge if each rows should be shown
    int show_row[row];
    for (i = 0; i < row; i++)
    {
        show_row[i] = 0;
        for (j = 0; j < col; j++)
        {
            if(matrix[i][j]!=0)
            {
                // non-zero element found, so this row should  be shown
                show_row[i] = 1;
            }
        }
    }

    // judge if each columns should be shown
    int show_col[col];
    for (j = 0; j < col; j++)
    {
        show_col[j] = 0;
        for (i = 0; i < row; i++)
        {
            if(matrix[i][j]!=0)
            {
                // non-zero element found, so this column should be shown
                show_col[j] = 1;
            }
        }
    }

    // show rows and columns that should be shown
    for (i = 0; i < row; i++)
    {
        if (show_row[i])
        {
            for (j = 0; j < col; j++)
            {
                if (show_col[j])
                {
                    printf("%d ", matrix[i][j]);
                }
            }
            printf("\n");
        }
    }

    return 0;
}

【讨论】:

  • Nitpick: show_row[i] = 1;--> show_row[i] = 1; break;(同样适用于列循环)
【解决方案2】:

将忽略全零行的矩阵转置为新矩阵。
将忽略全零行的新矩阵转置为第二个新矩阵。
打印第二个矩阵。

int m1[4][4], m2[4][4], m[4][4] = {
    {0, 1, 0, 1},
    {0, 3, 1, 0},
    {0, 1, 5, 0},
    {0, 0, 0, 0},
};
transpose_notzero(m1, m);  // m1 = T(m);  ignore all-zero rows
transpose_notzero(m2, m1); // m2 = T(m1); ignore all-zero rows
mat_print(m2);

【讨论】:

  • transpose_notzeromat_print的实现在哪里?函数将如何识别矩阵的大小?
  • 那些实现细节留给 OP :-) 他可以将矩阵包装在一个结构中,例如:struct Matrix { int data[MAXROWS][MAXCOLS]; int rows, int cols; };
【解决方案3】:

您可以注册包含非零值的行/列,而不是计算元素数为零的循环。

喜欢:

int non_zero_row[row];
int non_zero_col[col];
memset(non_zero_row, 0, row * sizeof non_zero_row[0]);  // Set to zero, i.e. expect
memset(non_zero_col, 0, col * sizeof non_zero_col[0]);  // that the row/col is all-zero

for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        if(matrix[i][j] != 0)
        {
            non_zero_row[i] = 1;  // Mark row as containing a non-zero value
            non_zero_col[j] = 1;  // Mark row as containing a non-zero value
        }
    }
}

稍后在代码中,您可以使用两个“布尔”数组来确定如何处理元素。比如:

if (non_zero_row[i] && non_zero_col[j])
{
    // Do something with element [i][j], e.g. print it
}

原则上,您不需要在单独的循环中计算“布尔”数组。您可以在 scanf("%d", &amp;matrix[i][j]) 之后更新“布尔”数组,从而避免额外的循环。

顺便说一句:小心 VLA。您当前的代码在创建矩阵之前不会检查数组维度。您的程序的用户可以输入较大的值,这可能会导致堆栈溢出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 2016-07-18
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 2022-08-11
    • 2013-03-17
    相关资源
    最近更新 更多