Big-Small

Write a program to declare a matrix A [][] of order (MXN) where ‘M’ is the number of rows and ‘N’ is the number of columns such that both M and N must be greater than 2 and less than 20. Allow the user to input integers into this matrix. Perform the following tasks on the matrix:
Display the input matrix
Find the maximum and minimum value in the matrix and display them along with their position.
Test your program with the sample data and some random data:
Example 1.
INPUT:
M=3
N=4
Entered values: 8,7,9,3,-2,0,4,5,1,3,6,-4
OUTPUT:
Original matrix:
8 7 9 3
-2 0 4 5
1 3 6 -4
Largest Number: 9 Row: 0 Column: 2
Smallest Number: -4 Row=2 Column=3

import java.util.*;
class Matrix
  {
        int m;
        int n;
        int arr[][];
    Matrix()
    {
    }
    public Matrix( int mm, int nn)
    {
        m=mm;
        n=nn;
        arr=new int[m][n];
    }
   public void accept()
    {
        Scanner sc=new Scanner(System.in);
        {
            System.out.println("Enter the number of rows :");
            m= sc.nextInt();
            System.out.println("Enter the number of columns:");
            n= sc.nextInt();
            arr=new int[m][n];
            for(int i=0;i<m; i++){
                for(int j=0;j<n;j++)
                 {
                    System.out.println("Enter Value: ");
                    arr[i][j]= sc.nextInt();
                 }
                }
        }
   }
         void display()
    {
        System.out.println(" Original Matrix");
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }
    public void bigSmall()
    {
    int i,j,max,min,maxr,maxc,minr,minc;
    max=arr[0][0];
    min=arr[0][0];
    maxr=0;
    minr=0;
    maxc=0;
    minc=0;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
            {
                if(arr[i][j]>max)
                {
                max=arr[i][j];
                maxr=i;
                maxc=j;
                }
                else if(arr[i][j]< min)
                    {
                        minr=i;
                        minc=j;
                        min=arr[i][j];
                    }
            }
        }
        System.out.println("\nMaximum Value="+max);
        System.out.println("\nRow="+maxr);
        System.out.println("\nColumn="+maxc);
        System.out.println("\nMinimum Value="+min);
        System.out.println("\nRow= "+minr);
        System.out.println("\nColumn= "+minc);
    }
    public static void main(String args[])
    {
        Matrix ob=new Matrix();
        ob.accept();
        ob.display();
        ob.bigSmall();
    }
}
This entry was posted in Matrix. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *