Using three dimensional array, create a program that will enter table, row, and column together with the total number of Elements then display the Biggest number, Smallest number, and Second to the biggest number.
Sample input and output:
Input:
Enter number of Tables: 2
Enter number of Rows: 2
Enter number of Columns: 2
Enter 8 number of Elements
2
6
34
76
34
98
56
45
The biggest number among them is: 98
The smallest number among them is: 2
The second to the biggest number is: 76
import java.util.Scanner;
class Main {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter number of Tables: ");
int tables = scanner.nextInt();
System.out.print("Enter number of Rows: ");
int rows = scanner.nextInt();
System.out.print("Enter number of Columns: ");
int columns = scanner.nextInt();
int[][][] numbers = new int[tables][rows][columns];
System.out.println("Enter " + tables * rows * columns + " number of Elements");
for (int t = 0; t < tables; t++)
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
numbers[t][r][c] = scanner.nextInt();
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int secondMax = Integer.MIN_VALUE;
for (int t = 0; t < tables; t++)
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++) {
if (numbers[t][r][c] > max) {
secondMax = max;
max = numbers[t][r][c];
}
if (numbers[t][r][c] < min)
min = numbers[t][r][c];
}
System.out.println("The biggest number among them is: " + max);
System.out.println("The smallest number among them is: " + min);
System.out.println("The second to the biggest number is: " + secondMax);
}
}
}
Comments
Leave a comment