Question:

Write a program to accept a two-dimensional integer array of order \( 4 \times 5 \) as input from the user. Check if it is a Sparse Matrix or not. A matrix is considered to be a sparse, if the total number of zero elements is greater than the total number of non-zero elements. Print appropriate messages.

Show Hint

For matrix-based questions, always use nested loops. One loop is used for rows and the other for columns. While traversing, you can perform counting, checking, or other operations easily.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Understand the concept of a sparse matrix.
A sparse matrix is a matrix in which the number of zero elements is greater than the number of non-zero elements. Here, we have to accept a \(4 \times 5\) integer matrix from the user, count the number of zero and non-zero elements, and then decide whether the matrix is sparse or not.

Step 2: Declare the required array and variables.
We need a two-dimensional integer array of size \(4 \times 5\) to store the matrix elements. We also need two counter variables: one to count zero elements and another to count non-zero elements.

Step 3: Accept the matrix elements from the user.
Using nested loops, we can input all the elements of the matrix row by row. Since the matrix has \(4\) rows and \(5\) columns, the outer loop will run \(4\) times and the inner loop will run \(5\) times.

Step 4: Count zero and non-zero elements.
While traversing the matrix, each element is checked. If the element is equal to \(0\), then the zero counter is increased. Otherwise, the non-zero counter is increased. In this way, at the end of traversal, we get the total number of zero and non-zero elements.

Step 5: Compare the two counts.
After counting, we compare the total number of zero elements with the total number of non-zero elements. If the number of zero elements is greater, then the matrix is a sparse matrix. Otherwise, it is not.

Step 6: Write the complete Java program.

import java.util.Scanner;

class SparseMatrix
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int a[][] = new int[4][5];
        int i, j;
        int zero = 0, nonzero = 0;

        System.out.println("Enter the elements of the matrix:");
        for(i = 0; i < 4; i++)
        {
            for(j = 0; j < 5; j++)
            {
                a[i][j] = sc.nextInt();
                if(a[i][j] == 0)
                    zero++;
                else
                    nonzero++;
            }
        }

        System.out.println("Number of zero elements = " + zero);
        System.out.println("Number of non-zero elements = " + nonzero);

        if(zero > nonzero)
            System.out.println("It is a Sparse Matrix.");
        else
            System.out.println("It is not a Sparse Matrix.");
    }
}

Step 7: Conclude the logic of the program.
Thus, the program first stores all elements of the \(4 \times 5\) matrix, then counts the zero and non-zero values, and finally checks whether the matrix satisfies the condition of a sparse matrix. If zero elements are more than non-zero elements, the appropriate message is displayed.

Was this answer helpful?
0
0

Questions Asked in ICSE Class X board exam

View More Questions