프로그램 완성
여기서는 프로그램에 최종적인 마무리를 할 것입니다. 사용자로부터 제공된 행렬이 희소 행렬 (sparse matrix) 인지 여부를 확인합니다. 그런 다음 희소 행렬인지 아닌지 결과를 출력합니다.
#include <stdio.h>
int main()
{
int matrix[10][10], row, column, i, j;
printf("Enter the number of rows and columns of the matrix:\n");
scanf("%d%d", &row, &column);
printf("Enter the elements of the matrix:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
scanf("%d", &matrix[i][j]);
}
}
// Printing the matrix
printf("The matrix:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Checking if the matrix is sparse or not
int counter = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
if (matrix[i][j] == 0)
counter++;
}
}
if(counter > (row * column) / 2)
printf("The matrix is a sparse matrix\n");
else
printf("The matrix is not a sparse matrix\n");
return 0;
}