주어진 행렬에서 대부분의 요소가 0일 때 희소 행렬이라고 합니다. 예시 - 3x3 행렬
1 1 0 0 0 2 0 0 0
이 행렬에서 대부분의 요소는 0이므로 희소 행렬입니다.
문제
행렬이 희소 행렬인지 확인하십시오.
해결책
-
행렬의 ZERO가 (행 * 열)/2보다 크다고 가정합니다.
-
그러면 행렬은 희소 행렬이 됩니다. 그렇지 않으면 그렇지 않습니다.
프로그램
다음은 주어진 행렬이 희소 행렬인지 확인하는 프로그램입니다. -
#include<stdio.h>
#include<stdlib.h>
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