JAVA PROGRAM-
Find the smallest and largest number in m*n matrix and sort them in ascending order also print its row and column value.
SOLUTION:
import java.util.*;
class matrix
{
public static void main(String args[])
{ int p=0,smallest=4999,larger=0,maxr=0,maxc=0,minr=0,minc=0; ////global varibles//////
Scanner sc=new Scanner(System.in); ////input through 'util' package////
System.out.println("enter m*n");
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n]; ///creating m*n matrix///
int b[]=new int[1000];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt(); ///input elements in matrix////
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]<smallest) ////for checking the number is smaller or not/////
{
smallest=a[i][j]; ////storing the smaller number in new array////
maxr=i; ////storing the row of smaller number////
maxc=j; ////same as storing the column of smaller number////
}
if(a[i][j]>larger) ////for checking the number is larger or not/////
{
larger=a[i][j]; ////storing the larger number in new array///
minr=i; ////storing the row of larger number////
minc=j; ////same as storing the column of column number////
}
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[p]=a[i][j]; /////storing all the elements in single dimensional array///
p++;
}
}
for(int i=0;i<p;i++)
{
for(int j=0;j<p-i-1;j++)
{
if(b[j]>b[j+1]) ////sort the elements in ascending order////
{
int temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
p=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(b[p]+"\t");
p++;
}
System.out.println();
}
System.out.println("largest"+"="+larger); /////for printing///
System.out.println("Row : "+minr);
System.out.println("Column : "+minc);
System.out.println("smallest"+"="+smallest);
System.out.println("Row : "+maxr);
System.out.println("Column : "+maxc);
}
}
--------------------end--------------------------
class matrix
{
public static void main(String args[])
{ int p=0,smallest=4999,larger=0,maxr=0,maxc=0,minr=0,minc=0; ////global varibles//////
Scanner sc=new Scanner(System.in); ////input through 'util' package////
System.out.println("enter m*n");
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n]; ///creating m*n matrix///
int b[]=new int[1000];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt(); ///input elements in matrix////
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]<smallest) ////for checking the number is smaller or not/////
{
smallest=a[i][j]; ////storing the smaller number in new array////
maxr=i; ////storing the row of smaller number////
maxc=j; ////same as storing the column of smaller number////
}
if(a[i][j]>larger) ////for checking the number is larger or not/////
{
larger=a[i][j]; ////storing the larger number in new array///
minr=i; ////storing the row of larger number////
minc=j; ////same as storing the column of column number////
}
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[p]=a[i][j]; /////storing all the elements in single dimensional array///
p++;
}
}
for(int i=0;i<p;i++)
{
for(int j=0;j<p-i-1;j++)
{
if(b[j]>b[j+1]) ////sort the elements in ascending order////
{
int temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
p=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(b[p]+"\t");
p++;
}
System.out.println();
}
System.out.println("largest"+"="+larger); /////for printing///
System.out.println("Row : "+minr);
System.out.println("Column : "+minc);
System.out.println("smallest"+"="+smallest);
System.out.println("Row : "+maxr);
System.out.println("Column : "+maxc);
}
}
--------------------end--------------------------
No comments:
Post a Comment