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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") c/FunctionsGroup -.-> c/recursion("`Recursion`") subgraph Lab Skills c/output -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/comments -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/variables -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/data_types -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/operators -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/if_else -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/for_loop -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/user_input -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/memory_address -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/pointers -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/function_parameters -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/function_declaration -.-> lab-123224{{"`Checking Sparse Matrix`"}} c/recursion -.-> lab-123224{{"`Checking Sparse Matrix`"}} end

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