forked from Arunvijay28/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumArrays3524.java
88 lines (81 loc) · 2.96 KB
/
sumArrays3524.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
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
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Scanner;
public class sumArrays3524 {
static void arraySum(int[][] a, int[][] s, int rows, int cols) {
// Calculate the sum of corresponding elements in two arrays
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] += s[i][j];
}
}
System.out.println("Sum of two arrays is:");
// Display the sum of arrays
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
static void colSumMax(int[][] x, int[][] y, int rows, int cols) {
// Calculate the sum of arrays and sort based on column sum
arraySum(x, y, rows, cols);
int[] colsum = new int[cols];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
colsum[i] += x[j][i];
}
}
int[] csindex = new int[cols];
for (int j = 0; j < cols; j++) {
csindex[j] = j;
}
for (int i = 0; i < cols - 1; i++) {
for (int j = 0; j < cols - i - 1; j++) {
if (colsum[j] > colsum[j + 1]) {
int temp = colsum[j];
colsum[j] = colsum[j + 1];
colsum[j + 1] = temp;
int temp1 = csindex[j];
csindex[j] = csindex[j + 1];
csindex[j + 1] = temp1;
}
}
}
System.out.println("The array after sorting based on column sum is:");
// Display the sorted array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(x[i][csindex[j]] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
System.out.println("2021503524 - Mugundh J B");
System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
Scanner t = new Scanner(System.in);
System.out.print("Enter the no. of rows: ");
int r = t.nextInt();
System.out.print("Enter the no. of columns: ");
int c = t.nextInt();
int[][] a1 = new int[r][c];
int[][] a2 = new int[r][c];
System.out.println("Enter the values of matrix 1:");
// Input values for matrix 1
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
a1[i][j] = t.nextInt();
}
}
System.out.println("Enter the values of matrix 2:");
// Input values for matrix 2
for (int k = 0; k < r; k++) {
for (int l = 0; l < c; l++) {
a2[k][l] = t.nextInt();
}
}
colSumMax(a1, a2, r, c);
}
}