0% found this document useful (0 votes)
5 views11 pages

2d Array

The document contains Java code snippets for various operations on 2D arrays (matrices), including finding the biggest element, transposing the matrix, and identifying row-wise, column-wise, and diagonal biggest elements. It also includes methods for swapping diagonal elements, adding two matrices, reversing elements row-wise and column-wise, rotating the matrix, and printing elements in spiral order both clockwise and anti-clockwise. Each operation is accompanied by a main method to read matrix input and display the results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

2d Array

The document contains Java code snippets for various operations on 2D arrays (matrices), including finding the biggest element, transposing the matrix, and identifying row-wise, column-wise, and diagonal biggest elements. It also includes methods for swapping diagonal elements, adding two matrices, reversing elements row-wise and column-wise, rotating the matrix, and printing elements in spiral order both clockwise and anti-clockwise. Each operation is accompanied by a main method to read matrix input and display the results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2d Array

1. DAMT return the biggest element from the matrix ?

Ans

public class p191


{
public static void main(String[] args)
{
Matrix mt=new Matrix();
[Link]("Read matrix:= ");
int x[][]=[Link]();
[Link]("Enter the matrix elements:= ");
[Link](x);
int bg=[Link](x);
[Link]("Biggest is:="+bg);
}
}
class Matrix
{
public int[][] readmat()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the order of the matrix:= ");
int row=[Link]();
int col=[Link]();
int mat[][]=new int[row][col];
[Link]("Enter the "+row*col+" elements:= ");
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
mat[i][j]=[Link]();
}
}
return mat;
}

void display(int[][] mat)


{
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
[Link](mat[i][j]+" ");
}
[Link]();
}

2d Array 1
}
public int getbiggest(int mat[][])
{
int big=mat[0][0];
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
if(big<mat[i][j])
big=mat[i][j];
}
}
return big;
}

92. DAMT transpose the matrix ?

Ans

public static void main(String[] args)


{
Scanner sc=new Scanner([Link]);
[Link]("Enter the order of the matrix:= ");
int row=[Link]();
int col=[Link]();
int mat[][]=new int[row][col];
[Link]("Enter the "+row*col+" elements:= ");
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
mat[i][j]=[Link]();
}
}
int tra[][]=new int[mat[0].length][[Link]];
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
tra[j][i]=mat[i][j];
}
}
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
[Link](tra[i][j]+" ");
}
[Link]();

2d Array 2
}
}

93. WAJP to Row wise biggest and Column wise biggest and Diagonal biggest in matrix
?

Ans

static int[] diagonalwisebiggest(int mat[][])


{
int pbig=mat[0][0];
int sbig=mat[0][[Link]-1];
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
if(i==j)
{
if(pbig<mat[i][j])
pbig=mat[i][j];
}
if(i+j==[Link]-1)
{
if(sbig<mat[i][j])
sbig=mat[i][j];
}
}
}
int big[]= {pbig,sbig};
return big;
}
static int[] rowwisebiggest(int mat[][])
{
int big[]=new int[[Link]];
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
if(mat[i][j]>big[i])
big[i]=mat[i][j];
}
}
return big;
}
static int[] columnwisebiggest(int mat[][])
{
int big[]=new int[mat[0].length];
for(int i=0; i<mat[0].length; i++)
{
for(int j=0; j<[Link]; j++)

2d Array 3
{
if(mat[j][i]>big[i])
big[i]=mat[j][i];
}
}
return big;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the order of the matrix:= ");
int row=[Link]();
int col=[Link]();
int mat[][]=new int[row][col];
[Link]("Enter the "+row*col+" elements:= ");
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
mat[i][j]=[Link]();
}
}
int rbig[]=rowwisebiggest(mat);
for(int i=0; i<[Link]; i++)
{
[Link](i+1+" row biggest is "+rbig[i]);
}
int cbig[]=columnwisebiggest(mat);
for(int i=0; i<[Link]; i++)
{
[Link](i+1+" column biggest is "+cbig[i]);
}
int dbig[]=diagonalwisebiggest(mat);
for(int i=0; i<[Link]; i++)
{
[Link](i+1+" Diagonal biggest is "+dbig[i]);
}
}

94. WAJP Swap the diagonal elements of a Matrix ?

Ans

public static void main(String[] arg)


{
Scanner sc=new Scanner([Link]);
int r=[Link](),c=[Link]();
int mat[][]=new int[r][c];
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[0].length; j++)

2d Array 4
{
mat[i][j]=[Link]();
}
}
[Link]();
for(int i=0; i<[Link]/2; i++)
{
for(int j=0; j<mat[i].length; j++)
{
if(i==j)
{
int temp=mat[i][j];
mat[i][j]=mat[[Link]-1-i][mat[i].length-1-j];
mat[[Link]-1-i][mat[i].length-1-j]=temp;
}
if(i+j==[Link]-1)
{
int temp=mat[i][j];
mat[i][j]=mat[j][i];
mat[j][i]=temp;
}
}
}
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[0].length; j++)
{
[Link](mat[i][j]+" ");
}
[Link]();
}
}

95. DAMT add two Matrix ?

Ans

static int[][] readmat()


{
Scanner sc=new Scanner([Link]);
[Link]("Enter the order of the matrix:= ");
int row=[Link]();
int col=[Link]();
int mat[][]=new int[row][col];
[Link]("Enter the "+row*col+" elements:= ");
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
mat[i][j]=[Link]();
}

2d Array 5
}
return mat;
}
static int[][] addmatrix(int a[][],int b[][])
{
if([Link]!=[Link] || a[0].length!=b[0].length)
{
[Link]("Not possible to Add");
return null;
}
int c[][]=new int[[Link]][a[0].length];
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<c[i].length; j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
return c;
}
public static void main(String[] args)
{
int a[][]=readmat();
int b[][]=readmat();
int rs[][]=addmatrix(a,b);

for(int i=0; i<[Link]; i++)


{
for(int j=0; j<rs[i].length; j++)
{
[Link](rs[i][j]+" ");
}
[Link]();
}
}

96. DAMT Reverse or swap the Matrix elements row wise ?

Ans

public static void main(String[] args)


{
Scanner sc=new Scanner([Link]);
[Link]("Enter the order of the matrix:= ");
int row=[Link]();
int col=[Link]();
int mat[][]=new int[row][col];
[Link]("Enter the "+row*col+" elements:= ");
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)

2d Array 6
{
mat[i][j]=[Link]();
}
}
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length/2; j++)
{
int temp=mat[i][j];
mat[i][j]=mat[i][mat[i].length-1-j];
mat[i][mat[i].length-1-j]=temp;
}
}
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
[Link](mat[i][j]+" ");
}
[Link]();
}

97. DAMT Reverse or swap the matrix element column wise ?

Ans

public static void main(String[] args)


{
Scanner sc=new Scanner([Link]);
[Link]("Enter the order of the matrix:= ");
int row=[Link]();
int col=[Link]();
int mat[][]=new int[row][col];
[Link]("Enter the "+row*col+" elements:= ");
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
mat[i][j]=[Link]();
}
}
for(int i=0; i<[Link]/2; i++)
{
for(int j=0; j<mat[i].length; j++)
{
int temp=mat[i][j];
mat[i][j]=mat[[Link]-1-i][j];
mat[[Link]-1-i][j]=temp;
}

2d Array 7
}
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
[Link](mat[i][j]+" ");
}
[Link]();
}

98. DAMT Rotate the matrix element into 90’ Left and 90’ Right ?

Ans

static void displayArray(int mat[][])


{
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
[Link](mat[i][j]+" ");
}
[Link]();
}
}
static int[][] columnwiseriverse(int[][] mat)
{
for(int i=0; i<[Link]/2; i++)
{
for(int j=0; j<mat[i].length; j++)
{
int temp=mat[i][j];
mat[i][j]=mat[[Link]-1-i][j];
mat[[Link]-1-i][j]=temp;
}
}
return mat;
}
static int[][] rowwisewiseriverse(int[][] mat)
{
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length/2; j++)
{
int temp=mat[i][j];
mat[i][j]=mat[i][mat[i].length-1-j];
mat[i][mat[i].length-1-j]=temp;
}
}

2d Array 8
return mat;
}
static int[][] transpose(int[][] x)
{
int y[][]=new int[x[0].length][[Link]];
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<x[i].length; j++)
{
y[j][i]=x[i][j];
}
}
return y;
}
static int[][] rotate90right(int x[][])
{
x=transpose(x);
rowwisewiseriverse(x);
return x;
}
static int[][] rotate90left(int x[][])
{
x=transpose(x);
columnwiseriverse(x);
return x;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the order of the matrix:= ");
int row=[Link]();
int col=[Link]();
int mat[][]=new int[row][col];
[Link]("Enter the "+row*col+" elements:= ");
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
mat[i][j]=[Link]();
}
}

int[][] left=rotate90left(mat);
int[][] right=rotate90right(mat);
[Link]("90 degree Left:= ");
displayArray(left);
[Link]("90 degree Right:= ");
displayArray(right);
}

99. DAMT print a matrix element in Spiral order ?(clock wise) ?

2d Array 9
Ans

static void printSpiralorder(int x[][])


{
int n=[Link];
for(int i=0,j=n-1;i<j; i++,j--)
{
for(int k=i; k<j; k++)
[Link](x[i][k]+" ");
for(int k=i; k<j; k++)
[Link](x[k][j]+" ");
for(int k=j; k>i; k--)
[Link](x[j][k]+" ");
for(int k=j; k>i; k--)
[Link](x[k][i]+" ");
}
if(n%2==1)
[Link](x[n/2][n/2]);
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the order of the matrix:= ");
int row=[Link]();
int col=[Link]();
int mat[][]=new int[row][col];
[Link]("Enter the "+row*col+" elements:= ");
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
mat[i][j]=[Link]();
}
}

printSpiralorder(mat);
}

00. DAMT print a matrix element in Spiral order ?(Anti-clock wise) ?

Ans

static void printSpiralorder(int x[][])


{
int n=[Link];
for(int i=0,j=n-1;i<j; i++,j--)
{
for(int k=i; k<j; k++)
[Link](x[k][i]+" ");

2d Array 10
for(int k=i; k<j; k++)
[Link](x[j][k]+" ");
for(int k=j; k>i; k--)
[Link](x[k][j]+" ");
for(int k=j; k>i; k--)
[Link](x[i][k]+" ");
}
if(n%2==1)
[Link](x[n/2][n/2]);
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the order of the matrix:= ");
int row=[Link]();
int col=[Link]();
int mat[][]=new int[row][col];
[Link]("Enter the "+row*col+" elements:= ");
for(int i=0; i<[Link]; i++)
{
for(int j=0; j<mat[i].length; j++)
{
mat[i][j]=[Link]();
}
}

printSpiralorder(mat);
}

2d Array 11

You might also like