Tag: 2d array
Two matrices are equal( by object) – ISC 2018
Two matrices are said to be equal if they have the same dimension and their corresponding elements are equal.
For example , the two matrices A and B given below are equal:
Matrix A Matrix B
1 2 3 1 2 3
2 4 5 2 4 5
3 5 6 3 5 6
Design a class EqMat to check if tow matrices are equal or not. Assume that the two matrices have the same dimension.
Class name : EqMat
Data members:
a[][] : to store integer elements
m, n : to store the number of rows and columns
Member functions:
EqMat(int mm, int nn) : initialize the data members m=mm and n=nn
void readarray() : to enter the elements in the array
int check(EqMat P, EqMat Q) : checks if the parameterized objects P and Q are equal and returns 1 if true,otherwise returns 0.
void print() : displays the array elements
Define the class and define main() to create objects and call the functions accordingly to enable the task.
Program:
import java.util.*;
class EqMat{
int a[][], m,n;
EqMat( int mm, int nn)
{
m=mm;
n=nn;
a=new int[m][n];
}
public void readarray()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the elements for the array:”);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
}
public int check(EqMat P, EqMat Q)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(P.a[i][j]!=Q.a[i][j])
return 0;
}
}
return 1;
}
public void print()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+”\t”);
}
System.out.println();
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the row and column size for the array:”);
int a=sc.nextInt();
int b=sc.nextInt();
EqMat A =new EqMat(a,b);
EqMat B=new EqMat(a,b);
A.readarray();
B.readarray();
A.print();
System.out.println();
B.print();
if(A.check(A,B)==1)
System.out.println(“Both are equal matrix”);
else
System.out.println(“Both are unequal matrix”);
}
}
Output:
Enter the row and column size for the array:
3
3
Enter the elements for the array:
2
3
4
5
6
7
8
9
1
Enter the elements for the array:
2
3
4
5
6
7
8
9
2
First matrix
2 3 4
5 6 7
8 9 1
Second matrix
2 3 4
5 6 7
8 9 2
Both are unequal matrix
Anti clockwise spiral matrix / anti clockwise circular matrix – java
import java.util.*;
class anticlockwise
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements : ");
int n = sc.nextInt();
int A[][] = new int[n][n];
int k=n*n, c1=0, c2=n-1, r1=0, r2=n-1;
while(k>=1)
{
for(int i=c1;i<=c2;i++)
{
A[r1][i]=k--;
}
for(int j=r1+1;j<=r2;j++)
{
A[j][c2]=k--;
}
for(int i=c2-1;i>=c1;i--)
{
A[r2][i]=k--;
}
for(int j=r2-1;j>=r1+1;j--)
{
A[j][c1]=k--;
}
c1++;
c2--;
r1++;
r2--;
}
/* Printing the Circular matrix */
System.out.println("The Circular Matrix is:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();
}
}
}
Output:
Enter the number of elements : 4
The Circular Matrix is:
16 15 14 13
5 4 3 12
6 1 2 11
7 8 9 10
Shuffle the matrix(2D)(1st row becomes the last, 2nd row becomes the 1st & so on..) — by object
A class Shift contains a 2D array of order (m*n) where the maximum values of both m and n is 5. Design the class Shift to shuffle the matrix (the first row becomes the last, the second row becomes the first and so on.)
Class name :Shift
Data members
mat[][], m,n;
Member functions:
Shift(int mm, int nn) : constructor to initialize the data members m=mm and n=nn
void input() : enters the elements of the array
void cyclic(Shift P) : enables the matrix of the object(P) to shift each row upwards in a cyclic manner and store the resultant matrix in the current object.
void display() : display the matrix elements.
Define the main() to create an object and call the methods accordingly to enable the task of shifting the array elements.
Program:
import java.util.Scanner;
public class Shift {
static Scanner sc=new Scanner(System.in);
int mat[][];
int m,n;
Shift(int mm,int nn)
{ m=mm;
n=nn;
mat=new int[m][n];
}
void input()
{ System.out.println("Enter elements");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]=sc.nextInt();
}
void display()
{
for(int i=0;i<m;i++)
{ System.out.println();
for(int j=0;j<n;j++)
System.out.print(mat[i][j] +"\t");
} }
void cyclic(Shift P)
{ for(int i=0;i<m;i++)
for(int j =0;j<n;j++)
{ if(i!=0)
mat[i-1][j]=P.mat[i][j];
else
mat[m-1][j]=P.mat[0][j];
}
}
static void main()
{ Shift x=new Shift(2,3);
Shift y=new Shift(2,3);
x.input();
y.cyclic(x);
System.out.println("Initial array is ");
x.display();
System.out.println();
System.out.print("Array after the shift ");
y.display();
} }
Output:

Clockwise Spiral matrix/circular matrix in java
import java.util.*;
class Circular_Matrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements : ");
int n = sc.nextInt();
int A[][] = new int[n][n];
int k=1, c1=0, c2=n-1, r1=0, r2=n-1;
while(k<=n*n)
{
for(int i=c1;i<=c2;i++)
{
A[r1][i]=k++;
}
for(int j=r1+1;j<=r2;j++)
{
A[j][c2]=k++;
}
for(int i=c2-1;i>=c1;i--)
{
A[r2][i]=k++;
}
for(int j=r2-1;j>=r1+1;j--)
{
A[j][c1]=k++;
}
c1++;
c2--;
r1++;
r2--;
}
/* Printing the Circular matrix */
System.out.println("The Circular Matrix is:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();
}
}
}
Output:
Enter the number of elements : 3
The Circular Matrix is:
1 2 3
8 9 4
7 6 5