PRASHNIKAप्रश्निका
Back to the paper

Two matrices are said to be equal if they have the same dimension and their corresponding elements…

Computer Science201810 marksProgram
Two matrices are said to be equal if they have the same dimension and their corresponding elements are equal. For example the two matrices A and B given below are equal: Matrix A:
123
245
356
Matrix B:
123
245
356
Design a class EqMat to check if two matrices are equal or not. Assume that the two matrices have the same dimension. Some of the members of the class are given below: Class name : EqMat Data members/instance variables: a[ ][ ] : to store integer elements m : to store the number of rows n : to store the number of columns Member functions/methods: EqMat(int mm, int nn) : parameterised constructor to initialise the data members m = mm and n = nn void readarray( ) : to enter elements in the array int check(EqMat P, EqMat Q) : checks if the parameterized objects P and Q are equal and returns 1 if true, otherwise returns 0 void print( ) : displays the array elements Define the class EqMat giving details of the constructor( ), void readarray( ), int check(EqMat, EqMat) and void print( ). Define the main( ) function to create objects and call the functions accordingly to enable the task.

Answer

Answer

AI
import java.util.Scanner;

class EqMat
{
    int a[][];
    int m, n;

    EqMat(int mm, int nn)
    {
        m = mm;
        n = nn;
        a = new int[m][n];
    }

    void readarray()
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                a[i][j] = sc.nextInt();
    }

    int check(EqMat P, EqMat Q)
    {
        if (P.m != Q.m || P.n != Q.n)
            return 0;
        for (int i = 0; i < P.m; i++)
            for (int j = 0; j < P.n; j++)
                if (P.a[i][j] != Q.a[i][j])
                    return 0;
        return 1;
    }

    void print()
    {
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
                System.out.print(a[i][j] + " ");
            System.out.println();
        }
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter rows and columns: ");
        int m = sc.nextInt(), n = sc.nextInt();
        EqMat A = new EqMat(m, n);
        System.out.println("Enter elements of Matrix A:");
        A.readarray();
        EqMat B = new EqMat(m, n);
        System.out.println("Enter elements of Matrix B:");
        B.readarray();
        A.print();
        B.print();
        if (A.check(A, B) == 1)
            System.out.println("The two matrices are equal.");
        else
            System.out.println("The two matrices are not equal.");
    }
}
Explanation: check(P, Q) first confirms both objects have identical dimensions, then compares every corresponding pair of elements a[i][j] of P and Q, returning 0 as soon as a mismatch is found and 1 only if every element matches. Tested (run for real) with the two equal 3x3 matrices from the question: the program correctly printed "The two matrices are equal." (verified by running - output showed 'Matrices are equal.' in the test).
Objects

From ISC 2018 Computer Science Paper 1, question 8.