Friday 12 September 2014

C program to check if a given matrix is a sparse matrix or not

A sparse matrix is a matrix in which most of the elements are zero.

For example let us consider two matrices  A and B

Matrix A
1 0 0
1 1 0
0 1 0

Matrix B
1 1 1
0 1 0
1 0 1


Here matrix A has  5 zero elements out of  9 and so it is Sparse Matrix. However Matrix B has only 3 elements out of 9 as zero so it is not a Sparse Matrix. It is a Dense Matrix

Here is a C program to verify whether a given matrix is sparse or not.

Source Code

  /*   C program to check  a  matrix is a sparse matrix or not. */

    #include <stdio.h>
    #include <conio.h>
    
    void main () {
        int matrix[10][10];
        int i, j, m, n;
        int countofzeros = 0;

        printf("Enter the order of the matix (Maximum : 10x10):");
        printf("\nNumber of rows:");
        scanf("%d", &m);
        printf("\nNumber of columns:");
        scanf("%d", &n);
       

        printf("Enter values of the matix:\n");
        for (i = 0; i < m; ++i) {
            for (j = 0; j < n; ++j) {
                printf("Enter value for %d, %d element:", i, j);
                scanf("%d", &matrix[i][j]);
                if (matrix[i][j] == 0) {
                    countofzeros ++;
                }
            }
        }

        printf("\nMatrix\n");
        for (i = 0; i < m; ++i) {           
            for (j = 0; j < n; ++j) {
                printf("%d ", matrix[i][j]);
            }
            printf("\n");
        }


        if (countofzeros > ((m * n) / 2)) {
            printf("\nGiven matrix is sparse matrix\n");
        }
        else
            printf("\nGiven matrix is not a sparse matrix\n");

        printf("\nSummary\n");

        printf("\nTotal number of elements in the matrix is %d", m * n);
        printf("\nNumber of zeros in the matrix is %d", countofzeros);
        printf("\nNumber of no-zeros in the matrix is %d", (m * n) - countofzeros);

        getch();
    }




Output

Case 1

Enter the order of the matix (Maximum : 10x10):
Number of rows:3

Number of columns:3
Enter values of the matix:
Enter value for 0, 0 element:1
Enter value for 0, 1 element:0
Enter value for 0, 2 element:0
Enter value for 1, 0 element:1
Enter value for 1, 1 element:1
Enter value for 1, 2 element:0
Enter value for 2, 0 element:0
Enter value for 2, 1 element:1
Enter value for 2, 2 element:0

Matrix
1 0 0
1 1 0
0 1 0

Given matrix is sparse matrix

Summary

Total number of elements in the matrix is 9
Number of zeros in the matrix is 5
Number of no-zeros in the matrix is 4


Case 2

Enter the order of the matix (Maximum : 10x10):
Number of rows:3

Number of columns:3
Enter values of the matix:
Enter value for 0, 0 element:1
Enter value for 0, 1 element:1
Enter value for 0, 2 element:1
Enter value for 1, 0 element:0
Enter value for 1, 1 element:1
Enter value for 1, 2 element:0
Enter value for 2, 0 element:1
Enter value for 2, 1 element:0
Enter value for 2, 2 element:1

Matrix
1 1 1
0 1 0
1 0 1

Given matrix is not a sparse matrix

Summary

Total number of elements in the matrix is 9
Number of zeros in the matrix is 3
Number of no-zeros in the matrix is 6

1 comment: