-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoD_Array.java
More file actions
52 lines (43 loc) · 1.73 KB
/
TwoD_Array.java
File metadata and controls
52 lines (43 loc) · 1.73 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
import java.util.Scanner;
public class TwoD_Array {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter dimensions for the first array (rows columns):");
int rows1 = scanner.nextInt();
int columns1 = scanner.nextInt();
int[][] array1 = new int[rows1][columns1];
System.out.println("Enter elements for the first array:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns1; j++) {
array1[i][j] = scanner.nextInt();
}
}
System.out.println("Enter dimensions for the second array (rows columns):");
int rows2 = scanner.nextInt();
int columns2 = scanner.nextInt();
if (rows1 != rows2 || columns1 != columns2) {
System.out.println("Cannot add arrays with different dimensions.");
return;
}
int[][] array2 = new int[rows2][columns2];
System.out.println("Enter elements for the second array:");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < columns2; j++) {
array2[i][j] = scanner.nextInt();
}
}
int[][] sumArray = new int[rows1][columns1];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns1; j++) {
sumArray[i][j] = array1[i][j] + array2[i][j];
}
}
System.out.println("Resultant array after addition:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns1; j++) {
System.out.print(sumArray[i][j] + " ");
}
System.out.println();
}
}
}