Program to Check Sparse Matrix in C Language

Program-to-Check-Sparse-Matrix-in-C-Language

Program Description:

Write a Program to Check Sparse Matrix in C programming language. The program should accept a Matrix (2D array) from the user and check whether the matrix is a Sparse Matrix or not.

Prerequisites:

It is recommended to know how to create arrays and multi-dimensional arrays(Matrices) in C. Please go through the following articles to learn more

before looking at the example inputs and output of the program, Let’s understand what is a sparse matrix.

What is a Sparse Matrix?

A sparse Matrix is a Matrix that contains a very small number of non-zero elements. So The elements of the sparse matrix is dominated by the Zeros.

We can call a matrix a Sparse Matrix, If the total number of elements with a value equal to zero is greater than the ((rows * columns)/2) value.

For example, We have a Matrix X with the dimensions rows x columns. Where the rows is the total number of rows in the Matrix-X and columns is the total number of Columns in the Matrix-X.

Let’s say the variable nZeros holding the total number of zeros in the Matrix-X

Then We can call the Matrix-X a Sparse Matrix, If nZeros > ((rows * columns)/2) condition is satisfied.

Example Input and Output:

Input:

Output:

Let’s look at the program to Check the Sparse Matrix in C programming language.

📢This program is part of the Array Practice Programs Series.

Check Sparse Matrix in C Program Explanation:

Here is a step-by-step explanation of the program.

  1. Create two integer constants named ROWS and COLUMNS, Which holds the max number of rows and columns in the Matrix. (Modify this number if you want to use large matrices)
  2. Start the Program by declaring a Matrix (2D Arrays) named X (Matrix-X). The row size and column size of the matrices are ROWS and COLUMNS constants respectively.
  3. Initialize the nZeros variable with Zero(0).
  4. Prompt the user to provide the desired Row size and Column Size, and store them in the variables rows and columns respectively. (Note, These rows and columns are small letters – C is Case Sensitive language)
  5. Check for array bounds using the (rows >= ROWS || rows <= 0 || columns >= COLUMNS || columns <= 0) condition, And display an error message if the row/column sizeis beyond the present limits.
  6. Take the user input for Input Matrix i.e Matrix-X, We need to use two For Loops, One For Loop will iterate over the rows, and the Second For Loop will iterate over the columns
  7. Create the First For Loop ( Outer Loop), Start the iteration from i=0 and go till the i<rows (that is for(i=0; i<rows; i++) ). At Each iteration of the Outer Loop,
    • Create the second For Loop ( Inner Loop). This loop is a Nested loop that will be inside the Outer Loop. Start the Loop from j=0 and go till the j<columns (i.e for(j=0; j<columns; j++) ). The Inner Loop will iterate over the columns. At each iteration of the Inner Loop
      • Prompt the user to provide the Matrix element and Read the element using the scanf function and Update the X[i][j] element.
      • Check if the X[i][j] element is equal to zero ( use (X[i][j] == 0)), If the element is equal to Zero(0), Then increment the nZeros variable – nZeros++
      • Repeat the above step for all elements in the Matrix-X
  8. Once the above Step 7 is completed, The nZero variable contains the total number of Zeroes in the given Matrix.
  9. Now, To Check if Matrix X is a Sparse Matrix, We need to check the (nZeros > ((rows * columns) / 2)) condition.
    • If the above condition is true, Then the input matrix (Matrix-X) is a Sparse Matrix.
    • If it is false, Then the Matrix-X is not a Sparse Matrix.
  10. Display the results on the console.

Program to Check Sparse Matrix in C Language:

Let’s convert the above algorithm to C Program

Program Output:

Compile and Run the Program using GCC compiler (Any compiler)

To compile the program use following command.

$ gcc check-sparse-mat.c

Run the Program.

Test Case 1: Normal Cases:

If we observe the output of the above program, The given matrix contains 9 Zeroes out of total 16 elements. The total number of zeroes are greater than the ((rows * columns) / 2). So above matrix is a Sparse Matrix.

check-sparse-matrix-in-c-program-output

Let’s check few more example.

As we can see from the above output, The program is generating the desired results.

Test 2: Negative Cases:

As excepted Program is displaying an error message( Invalid Rows or Columns size, Please Try again) if the row or column sizes goes beyond limits

Check Sparse Matrix using Functions in C Program:

In the above program, We have used only the main() function. Let’s divide the program and create a function to take the user input and check the Sparse Matrix.

Here is the program with two user defined functions isSparse to check if the given matrix is a sparse matrix.

We have created a user-defned function called isSparse().

  • Prototype of the isSparse() function – int isSparse(int rows, int columns, int X[rows][columns])
  • The isSparse() function takes three Formal Arguments they are rows , columns, and X matrix.
  • The isSparse() function takes the user input and updates the Matrix-X and check if the Matrix-X is a Sparse Matrix.
  • This function reads the user input and also counts the number of zeroes ( nZeros) in the Matrix-X, Finally checks the number of zeroes with the (nZeros > ((rows * columns) / 2)); and returns the results.
  • It returns 1, if the given Matrix-X is a Sparse Matrix, Otherwise, it returns Zero(0).

Call the isSparse() function from the main() function to check if the matrix is a Sparse matrix.

Program Output:

Compile and Run the Program and observe the output.

check-sparse-matrix-using-function-program-output

The program is properly detecting the sparse matrix.

Related Matrix Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Check a Sparse Matrix […]

Leave a Reply