稀疏矩阵的 C 程序!
在给定的矩阵中,当大多数元素为零时,我们称其为稀疏矩阵。示例-3x3矩阵
1 1 0 0 0 2 0 0 0
在这个矩阵中,大部分元素为零,因此是稀疏矩阵。
问题
检查矩阵是否为稀疏矩阵。
解决方案
让我们假设矩阵中的零大于(行*列)/2。
那么,矩阵是稀疏矩阵,否则不是。
程序
以下是检查给定矩阵是否为稀疏矩阵的程序-
#include输出结果#include int main(){ int row,col,i,j,a[10][10],count = 0; printf("Enter row\n"); scanf("%d",&row); printf("Enter Column\n"); scanf("%d",&col); printf("Enter Element of Matrix1\n"); for(i = 0; i < row; i++){ for(j = 0; j < col; j++){ scanf("%d",&a[i][j]); } } printf("Elements are:\n"); for(i = 0; i < row; i++){ for(j = 0; j < col; j++){ printf("%d\t",a[i][j]); } printf("\n"); } /*checking sparse of matrix*/ for(i = 0; i < row; i++){ for(j = 0; j < col; j++){ if(a[i][j] == 0) count++; } } if(count > ((row * col)/2)) printf("Matrix is a sparse matrix \n"); else printf("Matrix is not sparse matrix\n"); }
执行上述程序时,会产生以下结果-
Run 1: Enter row 3 Enter Column 2 Enter Element of Matrix1 1 0 2 0 2 0 Elements are: 1 0 2 0 2 0 Matrix is not sparse matrix Run 2: Enter row 3 Enter Column 2 Enter Element of Matrix1 1 0 0 0 0 0 Elements are: 1 0 0 0 0 0 Matrix is a sparse matrix
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下