Lab16 || Java Find Maximum and Minimum of an 2-D array

 In this lab, we will create a two-dimensional array and fill it with random values. We will then find minimum and maximum values in the array.
In your main method do the following:

•    Ask the user how many rows and columns they want in the array.
•    Read in the number of rows and number of columns.
•    Declare and create an array of ints called numbers. This array should have the number of rows and columns indicated.
•    Fill the entire array, generating random integers in the range [-50,50]. Fill the array row by row.
•    Print out the array. For this lab, you do not need to worry about having an exact alignment of the columns. Just make sure you can read the results and tell which number belongs in which column.
•    Test your code.

•    Now write code that searches the entire array to find the number with the smallest value and then prints out this value. Again, test this code before you continue.

•    Write code that finds and prints out the largest number in each row. Test your code.

•    Finally, write code that finds the largest number in each column. Test your code again.

Here is some sample output:

  -35  -36  -15  -12    4  -15  -11  -41  -28   26
   44   40   46    8  -31    6  -45   45   15  -23
   38  -22    5   -6    0    8  -20   11  -42    3
   -5  -15   24  -45   23   34   23   -4   10    3
    2  -29   48   42  -33   11   35   43   50  -25
   26  -35  -35  -36   -6  -38  -15  -19   32   -1
   27  -16  -24    2   -6   30   -1  -15   32    1
   45   33   -5  -24  -21   35   20  -24    3  -46
  -34   49   43   19  -17  -14   34   29    3   35
   18   27   13  -20  -12  -10  -50  -28   32   16
  -22   20   -8   42  -24  -43   15   20    4  -41
    5  -18   22  -49   -6   22   43   -5  -10  -17
   -2  -47  -40    1   35  -22   32  -35  -10   20
   47  -46  -39  -46    1   15    5    1  -34  -45
   47   30   -5   46  -18  -20   25   15  -13   49
   16  -29    2   28   42   32   37  -27  -38    0
  -36  -27  -47   27   35  -30  -46   48   41   23
   34   35   28   38  -22  -18   -8  -36  -47  -50
    5    8  -34   22   26  -26   26   25   18   -4
   37   21    9   -6   43  -13  -44  -31   26  -34

Minimum of all numbers: -50

Maximum of row 0: 26
Maximum of row 1: 46
Maximum of row 2: 38
Maximum of row 3: 34
Maximum of row 4: 50
Maximum of row 5: 32
Maximum of row 6: 32
Maximum of row 7: 45
Maximum of row 8: 49
Maximum of row 9: 32
Maximum of row 10: 42
Maximum of row 11: 43
Maximum of row 12: 35
Maximum of row 13: 47
Maximum of row 14: 49
Maximum of row 15: 42
Maximum of row 16: 48
Maximum of row 17: 38
Maximum of row 18: 26
Maximum of row 19: 43

Maximum of column 0: 47
Maximum of column 1: 49
Maximum of column 2: 48
Maximum of column 3: 46
Maximum of column 4: 43
Maximum of column 5: 35
Maximum of column 6: 43
Maximum of column 7: 48
Maximum of column 8: 50
Maximum of column 9: 49

SOLOUTION:

import java.util.Random;
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter number of row and number of column:");
        int row = input.nextInt();
        int col = input.nextInt();
        int[][] numbers = new int[row][col];

        Random random = new Random();

        for(int i=0; i<row; ++i) {
            for(int j=0; j<col; ++j) {
                numbers[i][j] = random.nextInt(101) - 50;
            }
        }

        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) {
                System.out.print(numbers[i][j] + " ");
            }
            System.out.println();
        }

        int smallestValue = 51;

        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) {
                smallestValue = Math.min(smallestValue, numbers[i][j]);
            }
        }

        System.out.println("Minimum of all numbers: "+ smallestValue);
        System.out.println();

        for(int i = 0; i < row; ++i) {
            int maxOfRow = -51;
            for(int j = 0; j < col; ++j) {
                maxOfRow = Math.max(maxOfRow, numbers[i][j]);
            }
            System.out.println("Maximum of row "+ i +": " + maxOfRow);
        }

        System.out.println();

        for(int i=0; i<col; ++i) {
            int maxOfCol = -51;
            for(int j=0; j<row; ++j) {
                maxOfCol = Math.max(maxOfCol, numbers[j][i]);
            }
            System.out.println("Maximum of column " + i + ": " + maxOfCol);
        }

        input.close();
    }
}

SNAPSHOT: