Home » Matrix-Backtrack » Matrix Program

Matrix Program


import java.io.*;

class Matrix_RowCol
{
public void take()throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
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 :");
m=Integer.parseInt(br.readLine());
System.out.println("\nEnter the number of columns:");
n=Integer.parseInt(br.readLine());
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:");
arr[i][j]=Integer.parseInt(br.readLine());
}
}

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;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: