Array-promo
Matrix-promo

Question

Program to determine whether a given matrix is a sparse matrix.

A matrix is said to be a sparse matrix if most of the elements of that matrix are 0. It implies that it contains very few non-zero elements.
For the matrix to be sparse, more than half of the elements present in the matrix must be zero.

Enter number of rows in Matrix:4
Enter number of columns in Matrix:3
Enter elements in matrix:
( 1 , 1 ) :1
( 1 , 2 ) :0
( 1 , 3 ) :1
( 2 , 1 ) :1
( 2 , 2 ) :1
( 2 , 3 ) :0
( 3 , 1 ) :0
( 3 , 2 ) :1
( 3 , 3 ) :1
( 4 , 1 ) :0
( 4 , 2 ) :0
( 4 , 3 ) :1
Matrix:
[1 0 1]
[1 1 0]
[0 1 1]
[0 0 1]
Given Matrix is not a Sparse Matrix



Enter number of rows in Matrix:3
Enter number of columns in Matrix:3
Enter elements in matrix:
( 1 , 1 ) :0
( 1 , 2 ) :0
( 1 , 3 ) :1
( 2 , 1 ) :0
( 2 , 2 ) :9
( 2 , 3 ) :0
( 3 , 1 ) :0
( 3 , 2 ) :0
( 3 , 3 ) :0
Matrix:
[0 0 1]
[0 9 0]
[0 0 0]
Given Matrix is Sparse Matrix

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

import java.util.Scanner;
public class sparseMatrix
{
    public static void main(String[] args)
    {
        int row=0, col=0,count=0,size=0,i=0,j=0;
        int a[][];

        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE ROW OF MATRIX");
        row=sc.nextInt();
        System.out.println("ENTER THE COLUMN OF MATRIX");
        col=sc.nextInt();

        a=new int[row][col];
        System.out.println("ENTER THE ELEMENTS IN MATRIX");
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                a[i][j]=sc.nextInt();
                //counting total number of zeroes
                if(a[i][j]==0)
                {
                    count++;
                }
            }
        }

        System.out.println("MATRIX:");
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
        size=row*col;

        if(count>(size/2))
        {
            System.out.println("MATRIX IS A SPARSE MATRIX");
        }
        else
        {
            System.out.println("MATRIX IS NOT A SPARSE MATRIX");
        }

    }
}

Coding Store

Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹250.Current price is: ₹150.
Sale

Original price was: ₹350.Current price is: ₹200.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale
Mastering Array

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹250.Current price is: ₹150.
Sale

Original price was: ₹350.Current price is: ₹200.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale
Mastering Array

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹250.Current price is: ₹150.
Sale

Original price was: ₹350.Current price is: ₹200.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale
Mastering Array

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.
Sale

Original price was: ₹80.Current price is: ₹50.

Leave a Reply

Your email address will not be published. Required fields are marked *