-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate2DArray.java
More file actions
75 lines (60 loc) · 1.97 KB
/
Copy pathCreate2DArray.java
File metadata and controls
75 lines (60 loc) · 1.97 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
package twodimensionalarray;
import java.util.*;
public class Create2DArray {
public static boolean search(int[][] matrix, int key) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if ( matrix[i][j] == key) {
System.out.println("Found at cell (" + i + "," + j + ")");
return true;
}
}
}
System.out.println("Not found");
return false;
}
public static void findLargest(int[][] matrix) {
int largest = matrix[0][0];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] > largest) {
largest = matrix[i][j];
}
}
}
System.out.println("Largest number: " + largest);
}
public static void findSmallest(int[][] matrix) {
int smallest = matrix[0][0];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] < smallest) {
smallest = matrix[i][j];
}
}
}
System.out.println("Smallest number: " + smallest);
}
public static void main(String[] args) {
int[][] matrix = new int[3][3];
// int n = 3, m = 3;
int n = matrix.length, m = matrix[0].length;
Scanner sc = new Scanner(System.in);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = sc.nextInt();
}
}
sc.close();
// Output
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
search(matrix, 5);
findLargest(matrix);
findSmallest(matrix);
}
}