C语言如何去掉或不显示列值为0的行
C language how to remove or not show rows with 0 value column
例如,我们有这个
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开发新人
- 判断每行每列是否显示
- 只打印应该显示的行和列。
#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;
}
将忽略所有行为零的矩阵转置为新矩阵。
将忽略全零行的新矩阵转置为第二个新矩阵。
打印第二个矩阵。
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);
您可以注册包含 非零 值的 rows/columns,而不是计算元素数等于零的循环。
喜欢:
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", &matrix[i][j])
之后更新“布尔”数组,从而避免额外的循环。
顺便说一句:小心 VLA。您当前的代码在创建矩阵之前不会检查数组维度。您的程序的用户可以输入大值,这可能会导致堆栈溢出。
例如,我们有这个
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开发新人
- 判断每行每列是否显示
- 只打印应该显示的行和列。
#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;
}
将忽略所有行为零的矩阵转置为新矩阵。
将忽略全零行的新矩阵转置为第二个新矩阵。
打印第二个矩阵。
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);
您可以注册包含 非零 值的 rows/columns,而不是计算元素数等于零的循环。
喜欢:
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", &matrix[i][j])
之后更新“布尔”数组,从而避免额外的循环。
顺便说一句:小心 VLA。您当前的代码在创建矩阵之前不会检查数组维度。您的程序的用户可以输入大值,这可能会导致堆栈溢出。