-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMin2DAry.java
More file actions
60 lines (49 loc) · 1.94 KB
/
MaxMin2DAry.java
File metadata and controls
60 lines (49 loc) · 1.94 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
import java.util.Scanner;
public class MaxMin2DAry {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter number of columns: ");
int cols = sc.nextInt();
int[][] matrix = new int[rows][cols];
System.out.println("Enter " + (rows*cols) + " elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = sc.nextInt();
}
}
sc.close();
System.out.println("\nYour 2D Array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
int max = matrix[0][0];
int min = matrix[0][0];
int maxRow = 0, maxCol = 0;
int minRow = 0, minCol = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
maxRow = i;
maxCol = j;
}
if (matrix[i][j] < min) {
min = matrix[i][j];
minRow = i;
minCol = j;
}
}
}
System.out.println("\n Results ");
System.out.println("Maximum Element: " + max);
System.out.println(" Found at position: [" + maxRow + "][" + maxCol + "]");
System.out.println("\nMinimum Element: " + min);
System.out.println(" Found at position: [" + minRow + "][" + minCol + "]");
}
}