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.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter the number of tables: ");
int tables = scanner.nextInt();
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt();
System.out.println("Enter " + tables * rows * columns + " elements:");
int[][][] array3d = new int[tables][rows][columns];
for (int t = 0; t < tables; t++)
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
array3d[t][r][c] = scanner.nextInt();
int[] array1d = new int[tables * rows * columns];
for (int t = 0; t < tables; t++)
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
array1d[c + columns * r + columns * rows * t] = array3d[t][r][c];
Arrays.sort(array1d);
System.out.println("The biggest number among them is: " + array1d[array1d.length - 1]);
System.out.println("The smallest number among them is: " + array1d[0]);
System.out.println("The second to the biggest number is: " + array1d[array1d.length - 2]);
}
}
}
Comments
Leave a comment