package Arrays2D;

import java.util.Scanner;

/*
==========================2026 (c) Basit Qureshi ===========================

CS102 Programming II
Dept. of Computer Sc, Prince Sultan University
August 8, 2026
https://www.ieeepsu.org/basit/cs102/

//Exercise 4
Write a program that asks the user for dimensions of a 2-D array.
It processes information randomly filled in the array by calling
methods:
-Write a fillArray() method that takes the 2-D array as parameter. 
 It fills the array with random values and returns void.
-Write a printArray() method that prints all the elements in the
 array as a matrix.
-Write a makeArray() method that takes two parameters int x and y.
 It makes an array of type double with x, y as rows and columns and
 returns the array.
-Write a method findMaxR(int r) that returns the largest value in the
 row r of the array.
-Write a method findMinC(int c) that returns the smallest value in the
 column c of the array.

*/


public class EXArray2D4 {
    public static void main(String [] s)
    {
        Scanner In = new Scanner (System.in);
        System.out.println("Enter rows and cols: ");
        int rows, cols;
        rows = In.nextInt();
        cols = In.nextInt();
        
        int [][] Arr = makeArray(rows,cols);
        
        fillArray(Arr);
        
        printArray(Arr);

        System.out.println("Enter column#: ");
        int c = In.nextInt();
        
        System.out.println("min value in col "
                + c +" is: " + findMinC(Arr,c));


        System.out.println("Enter row#: ");
        int r = In.nextInt();
        
        System.out.println("max value in row "
                + r +" is: " + findMaxR(Arr,r));
        
    }
    
    public static int [][] makeArray(int r, int c)
    {
        return new int [r][c];
    }
    
    
    public static void fillArray(int [][] A)
    {
        for(int i=0;i<A.length;i++)
        {
            for(int j=0;j<A[i].length;j++)
            {
                A[i][j]=(int)(Math.random() * 10);
            }
        }
    }

    public static void printArray(int [][] A)
    {
        for(int i=0;i<A.length;i++)
        {
            for(int j=0;j<A[i].length;j++)
            {
                System.out.print(A[i][j]+" "); 
            }
            System.out.println();
        }
    } 
    
    
    //find the max value in row r
    public static int findMaxR(int [][] A, int r) {
        int max =0;
        for(int i=0;i<A.length;i++)
        {
            if(max < A[r][i])
                max = A[r][i];
        }
        return max;
    }
    
    //find the min value in col c
    public static int findMinC(int [][] A, int c) {
        int min =A[0][c];
        for(int i=0;i<A.length;i++)
        {
            if(min > A[i][c])
                min = A[i][c];
        }
        return min;
    }    
}
