-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMatrixSum.java
More file actions
101 lines (67 loc) · 2.44 KB
/
MatrixSum.java
File metadata and controls
101 lines (67 loc) · 2.44 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Created by root on 8/3/17.
*/
package EPC;
import java.util.Scanner;
public class MatrixSum
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 for one dimensional and 2 for two dimensional");
int type = in.nextInt();
if (type == 1) {
System.out.println("enter the size of your one dimensional matrix");
int size = in.nextInt();
int oneD1[] = new int[size];
int oneD2[] = new int[size];
int result[] = new int[size];
System.out.println("enter the values of matrix one");
for (int i = 0; i <size ; i++) {
oneD1[i] = in.nextInt();
}
System.out.println("enter the values of matrix 2");
for (int i = 0; i <size ; i++) {
oneD2[i] = in.nextInt();
}
for (int i = 0; i <size ; i++) {
result[i] = oneD1[i] + oneD2[i];
}
System.out.println("The sum of the matrices are");
for (int i = 0; i <size ; i++) {
System.out.println(" "+result[i]);
}
}
if(type == 2)
{
System.out.println("Enter the size of your two dimensional array");
int size = in.nextInt();
int [][]twoD1 = new int[size][size];
int[][] twoD2 = new int[size][size];
int result[][] = new int[size][size];
System.out.println("enter the values of matrix one");
for (int i = 0; i <size ; i++) {
for (int j = 0; j <size ; j++) {
twoD1[i][j] = in.nextInt();
}
}
System.out.println("enter the values of matrix 2");
for (int i = 0; i <size ; i++) {
for (int j = 0; j <size ; j++) {
twoD2[i][j] = in.nextInt();
}
}
for (int i = 0; i <size ; i++) {
for (int j = 0; j <size ; j++) {
result[i][j] = twoD1[i][j] + twoD2[i][j];
}
}
System.out.println("The sum of the matrices are");
for (int i = 0; i <size ; i++) {
for (int j = 0; j <size ; j++) {
System.out.print(" "+ result[i][j]);
}
System.out.println();
}
}
}
}