Answer to Question #348802 in Java | JSP | JSF for Joy

Question #348802

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

1
Expert's answer
2022-06-07T17:43:36-0400
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);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS