-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractical_19.java
More file actions
99 lines (85 loc) · 2.47 KB
/
Practical_19.java
File metadata and controls
99 lines (85 loc) · 2.47 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
/*Question := Write a program to do the addition of two matrices */
import java.util.Random;
import java.util.Scanner;
public class Practical_19 {
static void getData(int row, int col, int[][] arr1, int[][] arr2) {
Random ra = new Random();
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
arr1[i][j] = ra.nextInt(10);
}
}
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
arr2[i][j] = ra.nextInt(10);
}
}
}
static void displayData(int row, int col, int[][] arr1, int[][] arr2) {
System.out.println("The First array is: ");
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
System.out.print(arr1[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("The Second array is: ");
for (int i=0; i<row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(arr2[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
static void matrixSum(int row, int col, int[][] arr1, int[][] arr2){
int[][] sum = new int [row][col];
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
sum[i][j] = arr1[i][j] + arr2[i][j];
}
}
System.out.println("Sum of two Arrays := ");
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of Rows :=");
int row = sc.nextInt();
System.out.println("Enter Number of Columns :=");
int col = sc.nextInt();
int[][] arr1 = new int[row][col];
int[][] arr2 = new int[row][col];
getData(row, col, arr1, arr2);
displayData(row, col, arr1, arr2);
matrixSum(row, col, arr1, arr2);
}
}
/*
Output :=
Enter Number of Rows :=
4
Enter Number of Columns :=
4
The First array is:
8 4 5 1
2 3 4 6
4 2 5 6
2 1 2 0
The Second array is:
4 9 7 6
7 9 5 3
7 5 2 2
5 2 8 7
Sum of two Arrays :=
12 13 12 7
9 12 9 9
11 7 7 8
7 3 10 7
*/