ADVERTISEMENT
ADVERTISEMENT

Kotlin program to check whether two matrices are identical or not

Kotlin | Checking Identical Matrices: Here, we are going to learn to check whether two given matrices are identical or not in Kotlin programming language?
Submitted by IncludeHelp, on May 06, 2020

Given two matrices, we have to check them they are identical or not?

Example:

    Input:
    matrix 1:
    [2, 3, 5]
    [0, 5, 4]
    [2, 1, 2]

    matrix 2:
    [2, 3, 5]
    [0, 5, 4]
    [2, 1, 2]

    Output:
    Matrices are Equal !! 

Program to check whether two matrices are identical or not in Kotlin

/*Kotlin Program to check Two matrices are equal or not
* 1. no. of rows and col for both matrices should be equal
* 2. corresponding elements from both elements should be same e.g. a[0][0]=b[0][0]
*/

package com.includehelp

import java.util.*

// Main function, Entry Point of Program
fun main(args: Array<String>) {
    //variable of rows and col
    val rows: Int
    val column: Int

    var isEqual =  true

    //Input Stream
    val scanner = Scanner(System.`in`)

    //Input no of rows and column
    print("Enter the number of rows and columns of matrix : ")
    rows   = scanner.nextInt()
    column = scanner.nextInt()

    //Create Array
    val matrixA     = Array(rows) { IntArray(column) }
    val matrixB     = Array(rows) { IntArray(column) }
    val matrixSum   = Array(rows) { IntArray(column) }


    //Input Matrix
    println("Enter the Elements of First Matrix ($rows X $column} ): ")
    for(i in matrixA.indices){
        for(j in matrixA[i].indices){
            print("matrixA[$i][$j]: ")
            matrixA[i][j]=scanner.nextInt()
        }
    }

    //Input Matrix
    println("Enter the Elements of Second Matrix ($rows X $column} ): ")
    for(i in matrixB.indices){
        for(j in matrixB[i].indices){
            print("matrixB[$i][$j]: ")
            matrixB[i][j]=scanner.nextInt()
        }
    }

    //print Matrix A
    println("Matrix A : ")
    for(i in matrixA.indices){
        println("${matrixA[i].contentToString()} ")
    }

    //print Matrix B
    println("Matrix B : ")
    for(i in matrixB.indices){
        println("${matrixB[i].contentToString()} ")
    }

    //Check Equality of Corresponding Elements
    for(i in 0 until rows){
        for(j in 0 until column){
            if(matrixA[i][j]!=matrixB[i][j]){
                isEqual=false
                break
            }
        }
    }

    if(isEqual) println("Matrices are Equal !! ") else println("Matrices are not equal !! ")
}

Output

Run 1:
Enter the number of rows and columns of matrix : 2
2
Enter the Elements of First Matrix (2 X 2} ):
matrixA[0][0]: 1
matrixA[0][1]: 3
matrixA[1][0]: 5
matrixA[1][1]: 7
Enter the Elements of Second Matrix (2 X 2} ):
matrixB[0][0]: 1
matrixB[0][1]: 3
matrixB[1][0]: 5
matrixB[1][1]: 7
Matrix A :
[1, 3]
[5, 7]
Matrix B :
[1, 3]
[5, 7]
Matrices are Equal !!
----------
Run 2:
Enter the number of rows and columns of matrix : 3
3
Enter the Elements of First Matrix (3 X 3} ):
matrixA[0][0]: 1
matrixA[0][1]: 2
matrixA[0][2]: 3
matrixA[1][0]: 4
matrixA[1][1]: 5
matrixA[1][2]: 6
matrixA[2][0]: 7
matrixA[2][1]: 8
matrixA[2][2]: 9
Enter the Elements of Second Matrix (3 X 3} ):
matrixB[0][0]: 1
matrixB[0][1]: 2
matrixB[0][2]: 3
matrixB[1][0]: 4
matrixB[1][1]: 5
matrixB[1][2]: 7
matrixB[2][0]: 8
matrixB[2][1]: 9
matrixB[2][2]: 0
Matrix A :
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Matrix B :
[1, 2, 3]
[4, 5, 7]
[8, 9, 0]
Matrices are not equal !!
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.