Checking Sparse Matrix

CCBeginner
Practice Now

Introduction

Sparse matrix is a matrix in which number of 0's is greater than the number of non-zero elements. In this step-by-step lab, we will learn how to check whether a two-dimensional array is a sparse matrix or not using C programming.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Creating a Two-Dimensional Array

In this step, we will create a two-dimensional array to store our matrix elements. We will take user input for the number of rows and columns and then we will ask the user to enter the matrix elements one-by-one using a nested for loop.

#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]);
      }
   }

   // 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;
}

Print the Matrix

Here, we will print the matrix using the nested for loop in which we have already taken user input.

#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;
}

Complete the Program

Here we will add the final touches to our program. We will check if the matrix provided by the user is a sparse matrix or not. We will then print the result whether it is a sparse matrix or not.

#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;
}

Summary

In this step-by-step lab, we have learned how to check whether a two-dimensional array is sparse or not. We learned about the concept of a sparse matrix and how to code in C programming language to determine whether a matrix is sparse or not. We created a two-dimensional array, took user input, printed the matrix, and finally, wrote the logic to check if the matrix provided is sparse or not.

Other C Tutorials you may like