
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program for sparse matrix
In a given matrix, when most of the elements are zero then, we call it as sparse matrix. Example − 3 x3 matrix
1 1 0 0 0 2 0 0 0
In this matrix, most of the elements are zero, so it is sparse matrix.
Problem
Check whether a matrix is a sparse matrix or not.
Solution
Let us assume ZERO in the matrix is greater than (row * column)/2.
Then, the matrix is a sparse matrix otherwise not.
Program
Following is the program to check whether the given matrix is sparse matrix or not −
#include<stdio.h> #include<stdlib.h> int main(){ int row,col,i,j,a[10][10],count = 0; printf("Enter row
"); scanf("%d",&row); printf("Enter Column
"); scanf("%d",&col); printf("Enter Element of Matrix1
"); for(i = 0; i < row; i++){ for(j = 0; j < col; j++){ scanf("%d",&a[i][j]); } } printf("Elements are:
"); for(i = 0; i < row; i++){ for(j = 0; j < col; j++){ printf("%d\t",a[i][j]); } printf("
"); } /*checking sparse of matrix*/ for(i = 0; i < row; i++){ for(j = 0; j < col; j++){ if(a[i][j] == 0) count++; } } if(count > ((row * col)/2)) printf("Matrix is a sparse matrix
"); else printf("Matrix is not sparse matrix
"); }
Output
When the above program is executed, it produces the following result −
Run 1: Enter row 3 Enter Column 2 Enter Element of Matrix1 1 0 2 0 2 0 Elements are: 1 0 2 0 2 0 Matrix is not sparse matrix Run 2: Enter row 3 Enter Column 2 Enter Element of Matrix1 1 0 0 0 0 0 Elements are: 1 0 0 0 0 0 Matrix is a sparse matrix
- Related Articles
- C++ Program to Implement Sparse Matrix
- Sparse Matrix Multiplication in C++
- C++ Program to Check if it is a Sparse Matrix
- Java Program To Determine If a Given Matrix is a Sparse Matrix
- Golang Program To Determine If a Given Matrix is a Sparse Matrix
- C++ Program to Implement Sparse Array
- Check if a given matrix is sparse or not in C++
- How to convert a sparse matrix into a matrix in R?
- How to create a sparse matrix in R?
- How to create a sparse Matrix in Python?
- Program for Identity Matrix in C
- Program for Markov matrix in C++
- C Program for Matrix Chain Multiplication
- Find Next Sparse Number in C++
- C++ Range Sum Query Using Sparse Table

Advertisements