icse-promo

Question

Write a program to declare a matrix a[][] of order (M × N) 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.
Sort the elements of the matrix in ascending order using any standard sorting technique and rearrange them in the matrix.
Output the rearranged matrix.
Test your program with the sample data and some random data:

				
					Example 1:
INPUT:
M = 3
N = 4
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 = 3
SMALLEST NUMBER: -4
ROW=2
COLUMN=3
REARRANGED MATRIX:
-4   -2   0    1
3    3    4    5
6    7    8    9
Example 2:
INPUT:
M = 3
N = 22
OUTPUT:
SIZE OUT OF RANGE
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class SortMatrix
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int i=0,j=0,k=0,l=0,m=0,n=0,temp=0,sum=0,max=0,min=0,maxrow=0,maxcol=0,minrow=0,mincol=0;
        int arr[][];
        

        System.out.print("M = ");
        m = sc.nextInt();
        System.out.print("N = ");
        n = sc.nextInt();
        if(m < 2 || m > 20||n < 2 || n > 20)
        {
            System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
            return;
        }
        arr = new int[m][n];
        System.out.println("Enter integers in MATRIX:");
        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                arr[i][j] = sc.nextInt();
            }
        }
        System.out.println("ORIGINAL MATRIX");
        for(i = 0; i < m; i++)
        {
            for( j = 0; j < n; j++)
            {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
        /*Rearranging of matrix starts here*/

        /*IN the code we have four loops first two loops are row and columns of matrix respectively*/

        /*the other two loops inside 'j' loop do the same thing.They also represent rows and columns.*/
        /*it is done so that arr[i][j] remains constants while we check it with all the elements which is represented by arr[k][l]*/
        max=arr[0][0];
        min=arr[0][0];
        for( i = 0; i < m ; i++)   /*i loop*/
        {
            for(j = 0; j < n; j++) /*j loop*/
            {
                for(k=0;k< m;k++)/*k loop*/
                {
                    for(l=0;l< n;l++)/*l loop*/
                    {
                        if(arr[i][j]< arr[k][l])
                        {
                            temp=arr[i][j];
                            arr[i][j]=arr[k][l];
                            arr[k][l]=temp;
                        }
                    }
                    
                }
                if(arr[i][j]>max)
                {
                    max=arr[i][j];
                    maxrow=i;
                    maxcol=j;
                }
                if(arr[i][j]< min)
                {
                    min=arr[i][j];
                    minrow=i;
                    mincol=j;
                }
            }
        }
        System.out.println("LARGEST NUMBER:"+max);
        System.out.println("ROW:"+maxrow);
        System.out.println("COLUMN:"+maxcol);
        System.out.println("SMALLEST NUMBER:"+min);
        System.out.println("ROW:"+minrow);
        System.out.println("COLUMN:"+mincol);
        /*Rearranging of matrix ends here*/

        System.out.println("REARRANGED MATRIX");
        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

				
			

Coding Store

Leave a Reply

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