C Program to Check if a Matrix is a Sparse Matrix on Command Line

Let’s start the article to discuss the topic of “C Program to Check if a Matrix is a Sparse Matrix on Command Line“. Shall we start the discussion below? Come on! Let’s go.

C Program to Check if a Matrix is a Sparse Matrix on Command Line

C Program to Check if a Matrix is a Sparse Matrix on Command Line

Source Code

/*
 * C Program to check if a Matrix is a Sparse Matrix
 */
#include <stdio.h>
 
void main ()
{
    int matrix[10][10];
    int i, j, m, n;
    int sparse_counter = 0;
 
    printf("Enter the order of the matix n");
    scanf("%d %d", &m, &n);
    printf("Enter the elements of the matix n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            scanf("%d", &matrix[i][j]);
            if (matrix[i][j] == 0)
            {
                ++sparse_counter;
            }
        }
    }
    if (sparse_counter > ((m * n) / 2))
    {
        printf("The given matrix is Sparse Matrix !!! n");
    }
    else
        printf("The given matrix is not a Sparse Matrix n");
    printf("There are %d number of Zeros.", sparse_counter);
}

Final Thoughts

We hope this article “C Program to Check if a Matrix is a Sparse Matrix on Command Line” will be helpful to you. Please contact us via the comment section if you have any doubts about this topic. Don’t forget to bookmark us.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment