-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkingWith_2D_Array.java
More file actions
144 lines (135 loc) · 5.16 KB
/
WorkingWith_2D_Array.java
File metadata and controls
144 lines (135 loc) · 5.16 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.util.Scanner;
public class WorkingWith_2D_Array {
public static int[][] createArray() {
Scanner input = new Scanner(System.in);
System.out.print("Enter Rows : ");
int row = input.nextInt();
System.out.print("Enter Columns : ");
int column = input.nextInt();
int[][] myList = new int[row][column];
return myList;
}
public static void inputArray(int[][] x) {
Scanner input = new Scanner(System.in);
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[0].length; column++) {
System.out.print("Enter value at (ROW : " + row + " COLUMN : " + column + ") : ");
x[row][column] = input.nextInt();
}
}
}
public static void displayArray(int[][] x) {
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[0].length; column++) {
System.out.printf("%-5d", x[row][column]);
}
System.out.println();
}
}
public static int Sum(int[][] x) {
int sum = 0;
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[0].length; column++) {
sum += x[row][column];
}
}
return sum;
}
public static void rowWiseSum(int[][] x) {
for (int row = 0; row < x.length; row++) {
int sum = 0;
for (int column = 0; column < x[0].length; column++) {
sum += x[row][column];
}
System.out.println("Sum of Row(" + row + ") = " + sum);
}
}
public static void columnWiseSum(int[][] x) {
for (int column = 0; column < x[0].length; column++) {
int sum = 0;
for (int row = 0; row < x.length; row++) {
sum += x[row][column];
}
System.out.println("Sum of Column(" + column + ") = " + sum);
}
}
public static void swapArray(int[][] a, int[][] b) {
int[][] c = new int[a.length][a[0].length];
int[][] d = new int[b.length][b[0].length];
for (int i = 0; i < a.length; i++) c[i] = a[i];
for (int i = 0; i < a.length; i++) d[i] = b[i];
for (int i = 0; i < a.length; i++) a[i] = d[i];
for (int i = 0; i < a.length; i++) b[i] = c[i];
}
public static void main(String[] args) {
System.out.println("-------------------------------------------");
System.out.println("| HERE THE 2D MENU BEGINS |");
System.out.println("-------------------------------------------");
int[][] myList = new int[1][1];
while (true) {
Scanner input = new Scanner(System.in);
System.out.println("1 : To create a 2D Array");
System.out.println("2 : To input Array values");
System.out.println("3 : To display Array values");
System.out.println("4 : To display Sum of Array");
System.out.println("5 : To display Row wise Sum");
System.out.println("6 : To display Column wise Sum");
System.out.println("7 : To Swap Two Array");
System.out.println("0 : To Exit");
System.out.print("Enter a number from Menu: ");
int option = input.nextInt();
if (option == 0) break;
switch (option) {
case 1 -> {
myList = createArray();
System.out.println();
System.out.println();
}
case 2 -> {
inputArray(myList);
System.out.println();
System.out.println();
}
case 3 -> {
displayArray(myList);
System.out.println();
System.out.println();
}
case 4 -> {
int sum = Sum(myList);
System.out.println(sum);
System.out.println();
System.out.println();
}
case 5 -> {
rowWiseSum(myList);
System.out.println();
System.out.println();
}
case 6 -> {
columnWiseSum(myList);
System.out.println();
System.out.println();
}
case 7 -> {
int[][] a1 = createArray();
inputArray(a1);
int[][] a2 = createArray();
inputArray(a2);
System.out.println("Array 1: ");
displayArray(a1);
System.out.println("Array 2: ");
displayArray(a2);
swapArray(a1, a2);
System.out.println();
System.out.println("Array 1: ");
displayArray(a1);
System.out.println("Array 2: ");
displayArray(a2);
System.out.println();
System.out.println();
}
}
}
}
}