How to write a C Program to check whether Matrix is a Sparse Matrix or Not with example. Any matrix is called a Sparse Matrix in C if it contains a large number of zeros. The mathematical formula behind this Sparse Matrix is T >= (m * n )/2, where T is the total number of zeros.
C Program to Check Matrix is a Sparse Matrix Example
This program for the sparse matrix in c allows the user to enter a Matrix’s number of rows and columns. Next, we will check whether the given matrix is sparse or not using For Loop.
#include<stdio.h> int main() { int i, j, rows, columns, a[10][10], Total = 0; printf("\n Please Enter Number of rows and columns : "); scanf("%d %d", &i, &j); printf("\n Please Enter the Matrix Elements \n"); for(rows = 0; rows < i; rows++) { for(columns = 0;columns < j;columns++) { scanf("%d", &a[rows][columns]); } } for(rows = 0; rows < i; rows++) { for(columns = 0; columns < j; columns++) { if(a[rows][columns] == 0) { Total++; } } } if(Total > (rows * columns)/2) { printf("\n The Matrix that you entered is a Sparse Matrix "); } else { printf("\n The Matrix that you entered is Not a Sparse Matrix "); } return 0; }
In this sparse matrix in the C Program, We declared single Two dimensional arrays Multiplication of size of 10 * 10.
Below C Programming statements asks the User to enter the Matrix size (Number of rows and columns. For instance, 2 Rows, 2 Columns = a[2][2] )
printf("\n Please Enter Number of rows and columns : "); scanf("%d %d", &i, &j);
Next, we used for loop to iterate every cell present in a[2][2] matrix. Conditions inside for loop ((rows < i) and (columns < j)) will ensure the compiler, not to exceed the matrix limit. Otherwise, the matrix will overflow
scanf statement inside the for loop will store the user entered values in every individual array element such as a[0][0], a[0][1], …..
for(rows = 0; rows < i; rows++). { for(columns = 0; columns < j; columns++) { scanf("%d", &a[rows][columns]); } }
Next, we are trying to check whether each and every element in a matrix is equal to the transposed matrix or not. If True, then it is a sparse matrix.
for(rows = 0; rows < i; rows++) { for(columns = 0; columns < j; columns++) { if(a[rows][columns] == 0) { Total++; } } }
User inserted values in this C sparse matrix example are: a[3][3] = {{10, 20, 0}, { 0, 0, 0},{0, 0, 70}}
Row First Iteration: for(rows = 0; rows < 3; 0++)
The condition (0 < 3) is True.
Column First Iteration: for(columns = 0; 0 < 3; 0++)
The condition (columns < 3) is True.
if(a[rows][columns] == 0) => if(a[0][0] == 0) – This condition is False. So, it will exit from If statement
Column Second Iteration: for(columns = 1; 1 < 3; 1++)
The condition (columns < 3) is True.
if(a[0][1] == 0) – This condition is False.
Column Third Iteration: for(columns = 2; 2 < 3; 1++)
The condition (columns < 3) is True.
if(a[0][2] == 0) – This condition is True. So, the Total will increment to 1.
Do the same for the remaining iterations of this sparse matrix program.
Next, we are using the If Statement to check whether the Total Number of Zeros is greater than or equal to (rows * columns) /2
Let me try the sparse matrix program with other values.
Please Enter Number of rows and columns : 3 3
Please Enter the Matrix Elements
10 20 30
0 0 0
25 59 0
The Matrix that you entered is Not a Sparse Matrix
Comments are closed.