-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsumMatrix.java
63 lines (58 loc) · 1.13 KB
/
sumMatrix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.*;
import java.util.Scanner;
class Matrix
{
int r,c;
String s; // can't do "= new int[r][c]" as r,c has no values yet
int arr[][];
Scanner sc = new Scanner(System.in);
Matrix(int r, int c)
{
this.r = r;
this.c = c;
arr = new int[r][c];
}
int[][] getMatrix()
{
System.out.println("Enter elements for first matrix");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
arr[i][j] = sc.nextInt();
}
System.out.println();
return arr; // not arr[][]
}
int[][] findSum(int a[][], int b[][])
{
int[][] temp = new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
temp[i][j] = a[i][j]+b[i][j];
}
return temp;
}
void displayMatrix(int temp[][])
{
System.out.println("The sum matrix is:");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
System.out.print(temp[i][j]+" ");
System.out.println();
}
}
}
class Methods
{
public static void main(String args[]) throws IOException
{
Matrix obj1 = new Matrix(3,3);
Matrix obj2 = new Matrix(3,3);
int x[][] = obj1.getMatrix();
int y[][] = obj2.getMatrix();
int z[][] = obj1.findSum(x,y);
obj1.displayMatrix(z);
}
}