C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to determine whether a given matrix is a sparse matrixExplanationIn this program, we need to check whether the given matrix is the sparse matrix. Sparse MatrixA matrix is said to be sparse matrix if most of the elements of that matrix are 0. It implies that it contains very less non-zero elements. To check whether the given matrix is the sparse matrix or not, we first count the number of zero elements present in the matrix. Then calculate the size of the matrix. For the matrix to be sparse, count of zero elements present in an array must be greater than size/2. Number of zeroes present in above matrix is 6 and size of the matrix is 3 * 3 = 9. Since, 6 > 4.5 that means, most elements of given array are zero. Hence, the above matrix is a sparse matrix. Algorithm
SolutionPython#Initialize matrix a a = [ [4, 0, 0], [0, 5, 0], [0, 0, 6] ]; count = 0; #Calculates number of rows and columns present in given matrix rows = len(a); cols = len(a[0]); #Calculates the size of the array size = rows * cols; #Count all zero element present in matrix for i in range(0, rows): for j in range(0, cols): if(a[i][j] == 0): count = count + 1; if(count > (size/2)): print("Given matrix is a sparse matrix"); else: print("Given matrix is not a sparse matrix"); Output: Given matrix is a sparse matrix C#include <stdio.h> int main() { int rows, cols, size, count = 0; //Initialize matrix a int a[][3] = { {4, 0, 0}, {0, 5, 0}, {0, 0, 6} }; //Calculates number of rows and columns present in given matrix rows = (sizeof(a)/sizeof(a[0])); cols = (sizeof(a)/sizeof(a[0][0]))/rows; //Calculates the size of array size = rows * cols; //Count all zero element present in matrix for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(a[i][j] == 0) count++; } } if(count > (size/2)) printf("Given matrix is a sparse matrix"); else printf("Given matrix is not a sparse matrix"); return 0; } Output: Given matrix is a sparse matrix JAVApublic class SparseMatrix { public static void main(String[] args) { int rows, cols, size, count = 0; //Initialize matrix a int a[][] = { {4, 0, 0}, {0, 5, 0}, {0, 0, 6} }; //Calculates number of rows and columns present in given matrix rows = a.length; cols = a[0].length; //Calculates the size of array size = rows * cols; //Count all zero element present in matrix for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(a[i][j] == 0) count++; } } if(count > (size/2)) System.out.println("Given matrix is a sparse matrix"); else System.out.println("Given matrix is not a sparse matrix"); } } Output: Given matrix is a sparse matrix C#using System; public class SparseMatrix { public static void Main() { int rows, cols, size, count = 0; //Initialize matrix a int[,] a = { {4, 0, 0}, {0, 5, 0}, {0, 0, 6} }; //Calculates number of rows and columns present in given matrix rows = a.GetLength(0); cols = a.GetLength(1); //Calculates the size of array size = rows * cols; //Count all zero element present in matrix for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(a[i,j] == 0) count++; } } if(count > (size/2)) Console.WriteLine("Given matrix is a sparse matrix"); else Console.WriteLine("Given matrix is not a sparse matrix"); } } Output: Given matrix is a sparse matrix PHP<!DOCTYPE html> <html> <body> <?php //Initialize matrix a $a = array( array(4, 0, 0), array(0, 5, 0), array(0, 0, 6) ); $count = 0; //Calculates number of rows and columns present in given matrix $rows = count($a); $cols = count($a[0]); //Calculates the size of array $size = $rows * $cols; //Count all zero element present in matrix for($i = 0; $i < $rows; $i++){ for($j = 0; $j < $cols; $j++){ if($a[$i][$j] == 0) $count++; } } if($count > ($size/2)) print("Given matrix is a sparse matrix"); else print("Given matrix is not a sparse matrix"); ?> </body> </html> Output: Given matrix is a sparse matrix
Next Topic#
|