Problem Statement:- Program to check whether a matrix is sparse or not.
Definition of sparse Matrix:- Sparse matrices are those matrices that have the majority of their elements equal to zero.
Example:-
Sample Input/Output:-
Data requirement:-
Input Data:- row_size, col_size, matrix[][]
Output Data:-String output
Additional Data:- i, j
Program in C
Here is the source code of the C Program to check whether a matrix is sparse or not.
Code:
#include<stdio.h>
int main()
{
int row_size,col_size;
//Get size of matrix
printf("Enter the row Size Of the Matrix:");
scanf("%d",&row_size);
printf("Enter the columns Size Of the Matrix:");
scanf("%d",&col_size);
int matrix[row_size][col_size];
//Taking input of the matrix
int i,j;
printf("Enter the Matrix Element:\n");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
scanf("%d",&matrix[i][j]);
}
}
int count_zero=0;
//Count number of zeros present in the given Matrix
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
if(matrix[i][j]==0)
{
count_zero++;
}
}
}
//check if zeros present in the given Matrix>(row*column)/2
if(count_zero>(row_size*col_size)/2)
printf("Given Matrix is a Sparse Matrix.");
else
printf("Given Matrix is not a Sparse Matrix.");
}
Input/Output:
Enter the row Size Of the Matrix:2
Enter the columns Size Of the Matrix:2
Enter the Matrix Element:
5 0
0 0
Given Matrix is a Sparse Matrix.
Program in C++
#include<stdio.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the matrix int i,j; printf("Enter the Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } int count_zero=0; //Count number of zeros present in the given Matrix for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]==0) { count_zero++; } } } //check if zeros present in the given Matrix>(row*column)/2 if(count_zero>(row_size*col_size)/2) printf("Given Matrix is a Sparse Matrix."); else printf("Given Matrix is not a Sparse Matrix."); }
Here is the source code of the C++ Program to check whether a matrix is sparse or not.
Code:
#include<iostream>
using namespace std;
int main()
{
int row_size,col_size;
//Get size of matrix
cout<<"Enter the row Size Of the Matrix:";
cin>>row_size;
cout<<"Enter the columns Size Of the Matrix:";
cin>>col_size;
int matrix[row_size][col_size];
//Taking input of the matrix
int i,j;
cout<<"Enter the Matrix Element:\n";
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
cin>>matrix[i][j];
}
}
int count_zero=0;
//Count number of zeros present in the given Matrix
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
if(matrix[i][j]==0)
{
count_zero++;
}
}
}
//check if zeros present in the given Matrix>(row*column)/2
if(count_zero>(row_size*col_size)/2)
cout<<"Given Matrix is a Sparse Matrix.";
else
cout<<"Given Matrix is not a Sparse Matrix.";
}
Input/Output:
#include<iostream> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the matrix int i,j; cout<<"Enter the Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } int count_zero=0; //Count number of zeros present in the given Matrix for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]==0) { count_zero++; } } } //check if zeros present in the given Matrix>(row*column)/2 if(count_zero>(row_size*col_size)/2) cout<<"Given Matrix is a Sparse Matrix."; else cout<<"Given Matrix is not a Sparse Matrix."; }
Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 4 5 0 0 6 7 8 9 0 Given Matrix is not a Sparse Matrix.
Program in Java
Here is the source code of the Java Program to check whether a matrix is sparse or not.
Code:
import java.util.Scanner;
public class CheckSparseMatrix {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int row_size,col_size;
//Get size of matrix
System.out.print("Enter the row Size Of the Matrix:");
row_size=cs.nextInt();
System.out.print("Enter the columns Size Of the Matrix:");
col_size=cs.nextInt();
int matrix[][] =new int[row_size][col_size];
//Taking input of the matrix
int i,j;
System.out.println("Enter the Matrix Element:");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
matrix[i][j]=cs.nextInt();
}
}
int count_zero=0;
//Count number of zeros present in the given Matrix
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
if(matrix[i][j]==0)
{
count_zero++;
}
}
}
//check if zeros present in the given Matrix>(row*column)/2
if(count_zero>(row_size*col_size)/2)
System.out.print("Given Matrix is a sparse Matrix.");
else
System.out.print("Given Matrix is not a sparse Matrix.");
cs.close();
}
}
Input/Output:
Enter the row Size Of the Matrix:3
Enter the columns Size Of the Matrix:3
Enter the Matrix Element:
1 0 0
0 0 0
6 7 0
Given Matrix is a sparse Matrix.
Program in Python
import java.util.Scanner; public class CheckSparseMatrix { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the matrix int i,j; System.out.println("Enter the Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } int count_zero=0; //Count number of zeros present in the given Matrix for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]==0) { count_zero++; } } } //check if zeros present in the given Matrix>(row*column)/2 if(count_zero>(row_size*col_size)/2) System.out.print("Given Matrix is a sparse Matrix."); else System.out.print("Given Matrix is not a sparse Matrix."); cs.close(); } }
Here is the source code of the Python Program to check whether a matrix is sparse or not.
Code:
# Get size of matrix
row_size=int(input("Enter the row Size Of the Matrix:"))
col_size=int(input("Enter the columns Size Of the Matrix:"))
matrix=[]
# Taking input of the 1st matrix
print("Enter the Matrix Element:")
for i in range(row_size):
matrix.append([int(j) for j in input().split()])
count_zero=0
#Count number of zeros present in the given Matrix
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j]==0:
count_zero+=1
#check if zeros present in the given Matrix>(row*column)/2
if count_zero>(row_size*col_size)//2:
print("Given Matrix is a sparse Matrix.")
else:
print("Given Matrix is not a sparse Matrix.")
Input/Output:
Enter the row Size Of the Matrix:3
Enter the columns Size Of the Matrix:3
Enter the Matrix Element:
3 0 3
0 6 0
0 4 0
Given Matrix is a sparse Matrix.
# Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the 1st matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) count_zero=0 #Count number of zeros present in the given Matrix for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j]==0: count_zero+=1 #check if zeros present in the given Matrix>(row*column)/2 if count_zero>(row_size*col_size)//2: print("Given Matrix is a sparse Matrix.") else: print("Given Matrix is not a sparse Matrix.")
0 Comments
Please do not Enter any spam link in the comment box