Java Code On Arrays and Matrix
Java Code On Arrays and Matrix
1) 1D Arrays
a) To search a given element in the array
CODE:
1
obj.searchArray(a);
}//end of main function
}//end of class
OUTPUT:
Enter number of elements
5
Enter elements
14 17 19 20 90
Enter element to be searched
20
Element found!
Position: 4
b) To merge 2 arrays
CODE:
//This program merges 2 arrays
import java.util.Scanner;
class MergeArrays
{
Scanner sc=new Scanner(System.in);
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int m,n;
System.out.println("Enter size of first array:");
m=s.nextInt();
System.out.println("Enter size of second array:");
n=s.nextInt();
int a[]=new int[m];
int b[]=new int[n];
int c[]=new int[m+n];
MergeArrays obj=new MergeArrays();
obj.accept(a);
obj.accept(b);
2
System.arraycopy(a,0,c,0,m);//making use of arraycopy function to copy 2
arrays into a third one
System.arraycopy(b,0,c,m,n);//hence merging it
System.out.println("Here is your merged array:");
obj.display(c);
}//end of main function
void accept(int c[])
{
int i;
System.out.println("Enter elements:");
for(i=0;i<c.length;i++)
{
c[i]=sc.nextInt();
}//end of loop
}//end of accept function
void display(int c[])
{
int i;
for(i=0;i<c.length;i++)
System.out.print(c[i]+" ");
}//end of display function
}//end of class
OUTPUT:
Enter size of first array:
5
Enter size of second array:
8
Enter elements:
12 48 00 9 89
Enter elements:
67 89 12 34 45 67 18 44
Here is your merged array:
12 48 0 9 89 67 89 12 34 45 67 18 44
D:\D7A Name 64>java MergeArrays
Enter size of first array:
0
Enter size of second array:
4
Enter elements:
Enter elements:
561 67 67 16
Here is your merged array:
561 67 67 16
2) 2D Arrays
3
a) To check if the entered matrix is symmetric or not
CODE:
//This program checks whether or not entered matrix is symmetric
import java.util.Scanner;
class MatrixSymmetric
{
Scanner s=new Scanner(System.in);
static int m,n;
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int flag;
System.out.println("Enter number of rows:");
m=sc.nextInt();
System.out.println("Enter number of columns:");
n=sc.nextInt();
int matrix[][]=new int[m][n];
MatrixSymmetric obj=new MatrixSymmetric();
System.out.println("Enter matrix:");
obj.acceptMatrix(matrix);
flag=obj.checkSymmetry(matrix);
if(flag==1)
System.out.println("\nThe entered matrix is symmetric");
else
System.out.println("The matrix is not symmetric!");
}//main function ends
void acceptMatrix(int matrix[][])
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
matrix[i][j]=s.nextInt();
}//inner loop ends
}//outer loop ends
}//acceptMatrix function ends
int checkSymmetry(int matrix[][])
{
int i,j,val=1;
OUTER: for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
4
{
if(matrix[i][j]!=matrix[j][i])
{
val=0;
break OUTER;
}
}//end of inner loop
}//end of outer loop
return val;
}//end of checkSymmetry function
}//end of class
OUTPUT:
Enter number of rows:
4
Enter number of columns:
4
Enter matrix:
10 11 12 13
11 14 15 16
12 15 19 20
13 16 20 24
5
Scanner s=new Scanner(System.in);
void accept(int d[][],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=s.nextInt();
}//end of inner loop
}//end of outer loop
}//end of accept function
void multiply(int a[][],int m,int n,int q,int b[][])
{
int c[][]=new int[m][q];
int i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j] + a[i][k]*b[k][j];
}//end of innermost loop
}//end of middle loop
}//end of outer loop
System.out.println("Following is the product of the 2 matrices:");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
System.out.print(c[i][j]+" ");
}//end of inner loop
System.out.println();
}//end of outer loop
}//end of multiply function
public static void main(String args[])
{
int m,n,p,q;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows for matrix A:");
m=sc.nextInt();
System.out.println("Enter number of columns for matrix A:");
6
n=sc.nextInt();
System.out.println("Enter number of rows for matrix B:");
p=sc.nextInt();
System.out.println("Enter number of columns for matrix B:");
q=sc.nextInt();
if(n==p)
{
int a[][]=new int[m][n];
int b[][]=new int[p][q];
MatrixMultiplication obj=new MatrixMultiplication();
System.out.println("Enter matrix A:");
obj.accept(a,m,n);
System.out.println("Enter matrix B");
obj.accept(b,p,q);
obj.multiply(a,m,n,q,b);
}//end of if block
else
System.out.println("These matrices cannot be multiplied.");
}//end of main function
}//end of class
OUTPUT:
D:\ Name >javac MatrixMultiplication.java
7
50 84
8
for(j=0;j<c;j++)
{
a[i][j]=s.nextInt();
}//end of inner loop
}//end of outer loop
}//end of accept function
int findTrace(int a[][],int r, int c)
{
int i,j,val=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
val=val+a[i][j];
}//end of inner loop
}//end of outer loop
return val;
}//end of findTrace function
9
5432
Trace of entered matrix is:16
Norm of entered matrix is:22.090722034374522
__________________________________________________________________________________
10