Home » Matrix-Backtrack » Matrix Program

Matrix Program

```
import java.io.*;

class Matrix_RowCol
{
public void take()throws Exception
{
int arr[][],m,n;
int g,h,i,j,max,min,maxr,maxc,minr,minc,srr,src;
int search=5;

while(true)
{
System.out.println("\nEnter the number of rows :");
System.out.println("\nEnter the number of columns:");
if(m<2 || n<2 || m>20 || n>20)
System.out.println("\nEnter the number of rows and columns");
else
break;
}

arr=new int[m][n];
for(i=0;i<m; i++)
{
for(j=0;j<n;j++)
{
System.out.println("\nEnter Value:");
}
}

max=arr[0][0];
min=arr[0][0];
maxr=0;
minr=0;
maxc=0;
minc=0;
srr=0;
src=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;
}
if(arr[i][j]< min)
{
minr=i;
minc=j;
min=arr[i][j];
}

if(arr[i][j]== search)
{
srr=i;
src=j;
}
}
}
System.out.println("\nOriginal Matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}

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);
System.out.println("\nSerach value present at row="+srr);
System.out.println("\nSerach value present at col ="+src);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

&nbsp;

if(arr[i][j]== search)
{
srr=i;
src=j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
arr[srr][src] = 1 ; // make serach value location ==1
}
}

&nbsp;

}
}
}

&nbsp;

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

&nbsp;

&nbsp;

&nbsp;

}
public static void main(String args[]) throws Exception
{
Matrix_RowCol ob=new Matrix_RowCol();
ob.take();
}
}

&nbsp;

```