In: Computer Science
Java.
Write 3 methods named addArrays, subArrays, and mulArrays each.
addArrays gets two matrices(int [][] array1, int [][] array2) as parameters and add them and returns the result (matrix).
subArrays gets two matrices(int [][] array1, int [][] array2) as parameters and subtract array2 from array1 and returns the result (matrix).
mulArrays gets two matrices(int [][] array3, int [][] array4) as parameters, multiplying them and returning the result(matrix).
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
/* Name of the class has to be "Main" only if the class is public.
*/
class Codechef
{
public static int [][]addArrays(int [][]arr1, int [][]arr2,int
rows,int col)
{
int [][]c = new int[rows][col];
for(int i = 0; i < rows ; i++)
for(int j = 0; j < col; j++)
{
c[i][j] = arr1[i][j] + arr2[i][j];
}
return c;
}
public static int [][]subArrays(int [][]arr1, int [][]arr2,int
rows,int col)
{
int [][]c = new int[rows][col];
for(int i = 0; i < rows ; i++)
for(int j = 0; j < col; j++)
{
c[i][j] = arr1[i][j] - arr2[i][j];
}
return c;
}
//m is rows of first matrix, p is rows and q is col of second
matrix
public static int [][]mulArrays(int [][]arr1, int [][]arr2,int
m,int p,int q)
{
int [][]multiply = new int[m][q];
int sum = 0;
for(int c=0; c<m; c++)
{
for(int d=0; d<q; d++)
{
for(int k=0; k<p; k++)
{
sum = sum + arr1[c][k]*arr2[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
return multiply;
}
public static void main (String[] args) throws
java.lang.Exception
{
// your code goes here
Scanner in = new Scanner(System.in);
System.out.print("Enter Number of Rows and Columns of
First Matrix : ");
int m = in.nextInt();
int n = in.nextInt();
int arr1[][] = new int[m][n];
System.out.print("Enter First Matrix Elements : ");
for(int c=0 ; c<m; c++)
{
for(int d=0; d<n; d++)
{
arr1[c][d] = in.nextInt();
}
}
System.out.print("Enter Number of Rows and Columns of Second Matrix
: ");
int p = in.nextInt();
int q = in.nextInt();
int arr2[][] = new int[p][q];
System.out.print("Enter Second Matrix Elements :\n");
for(int c=0; c<p; c++)
{
for(int d=0; d<q; d++)
{
arr2[c][d] = in.nextInt();
}
}
//check matrix addition
if(m == p && n == q)
{
int add[][] = new int[p][q];
int sub[][] = new int[p][q];
add = addArrays(arr1,arr2,m,n);
System.out.print("Addition of two Matrices is :\n");
for(int c=0; c<m; c++)
{
for(int d=0; d<n; d++)
{
System.out.print(add[c][d] + "\t");
}
System.out.print("\n");
}
System.out.print("Subtraction of two Matrices is :\n");
sub = subArrays(arr1,arr2,m,n);
for(int c=0; c<m; c++)
{
for(int d=0; d<n; d++)
{
System.out.print(sub[c][d] + "\t");
}
System.out.print("\n");
}
}
else
System.out.print("two Matrices can not be added or
subtracted\n");
if ( n != p )
{
System.out.print("Matrix of the entered order can't be
Multiplied..!!");
}
else
{
int mul[][] = new int[p][q];
mul = mulArrays(arr1,arr2,m,p,q);
System.out.print("Multiplication of two Matrices is :\n");
for(int c=0; c<m; c++)
{
for(int d=0; d<q; d++)
{
System.out.print(mul[c][d] + "\t");
}
System.out.print("\n");
}
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
//input:
2 3
1 2 3
7 8 9
2 3
5 6 7
3 4 5
//output
Addition of two Matrices is :
6 8 10
10 12 14
Subtraction of two Matrices is :
-4 -4 -4
4 4 4
//input for multiplication
2 3
1 2 3
4 5 6
3 2
7 8
9 10
11 12
//output
Multiplication of two Matrices is :
58 64
139 154