Tag: matrix
Diagonal matrix program in java
A square matrix is said to be diagonal matrix if the elements of matrix except main diagonal are zero.
A square null matrix is also a diagonal matrix whose main diagonal elements are zero.
import java.util.*;
class DiagonalMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
int A[][]=new int[m][m];
/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}
System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int p=0, q=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i!=j && A[i][j]!=0) // Checking non-diagonal elements
{
p=1;
break;
}
if(i==j && A[i][j]==0) // Checking diagonal elements
{
q++;
}
}
}
if(p==0 && q<m)
System.out.println("The matrix is Diagonal");
else
System.out.println("The matrix is not Diagonal");
}
}
Output:
Enter the size of the matrix : 3
Enter an element : (0,0)1
Enter an element : (0,1)0
Enter an element : (0,2)0
Enter an element : (1,0)0
Enter an element : (1,1)3
Enter an element : (1,2)0
Enter an element : (2,0)0
Enter an element : (2,1)0
Enter an element : (2,2)45
The Matrix is :
1 0 0
0 3 0
0 0 45
The matrix is Diagonal
Scalar matrix program in java
Write a Program in Java to input a 2-D square matrix and check whether it is a Scalar Matrix or not.
Scalar Matrix : A square matrix is said to be scalar matrix if all the main diagonal elements are equal and other elements except main diagonal are zero. Scalar matrix can also be written in form of n * I, where n is any real number and I is the identity matrix.
import java.util.*;
class ScalarMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
int A[][]=new int[m][m];
/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element :("+i+","+j+") ");
A[i][j]=sc.nextInt();
}
}
/* Printing the matrix */
System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int p = 0, q = 0, x = A[0][0]; // 'x' is storing the 1st main diagonal element
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
/* Checking that the matrix is diagonal or not */
if(i!=j && A[i][j]!=0) // All non-diagonal elements must be zero
{
p=1;
break;
}
/* Checking the matrix for scalarity */
// All main diagonal elements must be equal to 'x' and non-zero
if(i==j && (A[i][j]==0 || A[i][j]!=x))
{
q=1;
break;
}
}
}
if(p==0 && q==0)
System.out.println("The matrix is scalar");
else
System.out.println("The matrix is not scalar");
}
}
Output:
Enter the size of the matrix : 3
Enter an element :(0,0) 1
Enter an element :(0,1) 2
Enter an element :(0,2) 1
Enter an element :(1,0) 3
Enter an element :(1,1) 1
Enter an element :(1,2) 3
Enter an element :(2,0) 1
Enter an element :(2,1) 1
Enter an element :(2,2) 3
The Matrix is :
1 2 1
3 1 3
1 1 3
The matrix is not scalar
Enter the size of the matrix : 3
Enter an element :(0,0) 1
Enter an element :(0,1) 0
Enter an element :(0,2) 0
Enter an element :(1,0) 0
Enter an element :(1,1) 1
Enter an element :(1,2) 0
Enter an element :(2,0) 0
Enter an element :(2,1) 0
Enter an element :(2,2) 1
The Matrix is :
1 0 0
0 1 0
0 0 1
The matrix is scalar
Perform a right circular shift and a left circular shift on alternate rows of the array — by object
A class Whirl contains a 2D array of order [m * n]. Perform a right circular shift and a left circular shift on alternate rows of the array.
For example: Input m=5, n=4
The matrix is:
20 2 24 25
48 18 13 47
43 18 30 32
22 45 30 38
48 12 18 37
Output:
Right circular shift and left circular shift on alternate rows of the matrix:
The matrix is:
25 20 2 24
18 13 47 48
32 43 18 30
45 30 38 22
37 48 12 18
Class name :Whirl
Data member:
wam[][] : to store the amtrix
int m, n : integer to store the number of rows and columns
Member functions:
Whirl(……) : initialize m, n and to allocate memory to member array
void fnGet() : to fill the member array
void fnDisplay() : to show the member matrix
Whirl fnChangeMat() : modify the current matrix as per the given criteria and returns it.Specify the class Whirl, and define main() , giving the details of the above member data and methods.
Program:
import java.util.*;
class Whirl
{
int wam[][], m,n;
Whirl(int m1, int n1)
{
m=m1; n=n1;
wam=new int[m][n];
}
void fnGet()
{
for(int r=0;r<=m-1;r++)
{
for(int c=0;c<=n-1;c++)
wam[r][c]=(int)(Math.random()*50);
}
}
void fnDisplay()
{
System.out.println(“The matrix is:”);
for(int r=0;r<=m-1;r++)
{
for(int c=0;c<=n-1;c++)
{
System.out.print(wam[r][c]+”\t”);
}
System.out.println();
}
}
Whirl fnChangeMat()
{ int tmp=0;
for(int r=0;r<=m-1;r++)
{
if(r%2==0)
{
tmp=wam[r][n-1];
for(int c=n-1;c>=1;c–)
{ wam[r][c]=wam[r][c-1];
}
wam[r][0]=tmp;
}
else
{
tmp=wam[r][0];
for(int c=0;c<=n-2;c++)
{
wam[r][c]=wam[r][c+1];
}
wam[r][n-1]=tmp;
}
}
return this;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the value of m:”);
int m1=sc.nextInt();
System.out.println(“Enter the value of n:”);
int n1=sc.nextInt();
Whirl w=new Whirl(m1,n1);
Whirl w1=new Whirl(m1,n1);
w.fnGet();
w.fnDisplay();
w1=w.fnChangeMat();
System.out.println(“Right circular shift and left circular shift on alternate rows of the matrix:”);
w1.fnDisplay();
}
}
Output:
Enter the value of m:
5
Enter the value of n:
4
The matrix is:
20 2 24 25
48 18 13 47
43 18 30 32
22 45 30 38
48 12 18 37
Right circular shift and left circular shift on alternate rows of the matrix:
The matrix is:
25 20 2 24
18 13 47 48
32 43 18 30
45 30 38 22
37 48 12 18
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
Transpose of a matrix using object
The transpose of a matrix is found by interchanging the elements of rows and columns.
Design a class matrix that contains a 2D array of order [n * n ]. The maximum value possible for n is 20. The details of some of the members of the class are given below
Class name : matrix
Data members:
int t[][] : to store the matrix
int n : integer to store the number of rows and columns
Member functions:
matrix(…..) : parameterized constructor to initialize n and to allocate memory to member array
void fnGet() : to fill the member array
void fnDisplay() : to show the member matrix
matrix fnTrans(matrix A) : to store the transpose of the argument matrix in the current object and return that.
Specify the class matrix giving the details of the above member data and methods only.
Program:
import java.util.*;
class matrix
{
int t[][];
int n;
public matrix(int n1)
{n=n1;
t= new int [n][n];
}
void get()
{
Scanner sc=new Scanner(System.in);
int i,j;
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{
t[i][j]=sc.nextInt();
}
}
}
void disp()
{
int i,j;
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{
System.out.print(t[i][j]+”\t”);
}
System.out.println();
}
}
public matrix Trans(matrix o1)
{
int i, j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
t[i][j]=o1.t[j][i];
return this;
}
public static void main(String args[])
{
matrix m1=new matrix(3);
matrix o1=new matrix(3);
matrix o2=new matrix(3);
m1.get();
m1.disp();
o2=o2.Trans(m1);
System.out.println(“Transpose of matrix”);
o2.disp();
}
}
Output:
Enter the elements:
9
8
7
6
5
4
3
2
1
9 8 7
6 5 4
3 2 1
Transpose of matrix
9 6 3
8 5 2
7 4 1
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:
