You are on page 1of 10

PROGRAM ON ARRAYS

1) 1D Arrays
a) To search a given element in the array
CODE:

//searches for given element in 1D array


import java.util.Scanner;
class SearchElement
{

Scanner sc=new Scanner(System.in);


void accept(int a[])
{
int i;
System.out.println("Enter elements");
for(i=0;i<a.length;i++)
a[i]=sc.nextInt();
}//end of accept function
void searchArray(int a[])
{
int key,i;
System.out.println("Enter element to be searched");
key=sc.nextInt();
for(i=0;i<a.length;i++)
{
if(a[i]==key)
{
System.out.println("Element found!\nPosition: "+(i+1));
break;
}//end of if block
}//end of loop
if(i==a.length)
System.out.println("Element not present");
}//end of sear?chArray function
public static void main(String args[])
{
int size;
Scanner s=new Scanner(System.in);
System.out.println("Enter number of elements");
size=s.nextInt();
int a[]=new int[size];
SearchElement obj=new SearchElement();
obj.accept(a);

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

Enter number of elements


10
Enter elements
12 34 78 89 9 0 29 9 0 7
Enter element to be searched
789
Element not present

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

The entered matrix is symmetric

D:\ Name>java MatrixSymmetric.java


Error: Could not find or load main class MatrixSymmetric.java

D:\ Name >java MatrixSymmetric


Enter number of rows:
3
Enter number of columns:
3
Enter matrix:
123
456
789
The matrix is not symmetric!
b) To perform matrix multiplication
CODE:
//To perform Matrix multiplication
import java.util.Scanner;
class MatrixMultiplication
{

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

D:\ Name >java MatrixMultiplication


Enter number of rows for matrix A:
3
Enter number of columns for matrix A:
4
Enter number of rows for matrix B:
4
Enter number of columns for matrix B:
2
Enter matrix A:
1470
2581
3692
Enter matrix B
10
25
36
40
Following is the product of the 2 matrices:
30 62
40 73

7
50 84

D:\ Name >java MatrixMultiplication


Enter number of rows for matrix A:
4
Enter number of columns for matrix A:
5
Enter number of rows for matrix B:
3
Enter number of columns for matrix B:
4
These matrices cannot be multiplied.

c) To find out trace and norm of a particular matrix


CODE:
//To find Trace and norm of entered matrix
import java.util.Scanner;
class TraceAndNorm
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m,n,trace;
double norm;
System.out.println("Enter number of rows:");
m=sc.nextInt();
System.out.println("Enter number of columns:");
n=sc.nextInt();
int a[][]=new int[m][n];
TraceAndNorm obj=new TraceAndNorm();
System.out.println("Enter matrix:");
obj.accept(a,m,n);
trace=obj.findTrace(a,m,n);
norm=obj.findNorm(a,m,n);
System.out.println("Trace of entered matrix is:"+trace);
System.out.println("Norm of entered matrix is:"+norm);
}//end of main function
void accept(int a[][],int r,int c)
{
Scanner s=new Scanner(System.in);
int i,j;
for(i=0;i<r;i++)
{

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

double findNorm(int a[][],int r,int c)


{
double val=0;
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
val=val+(a[i][j]*a[i][j]);
}//end of inner loop
}//end of outer loop
val=Math.sqrt(val);
return val;
}//end of findNorm function
}//end of class
OUTPUT:
Enter number of rows:
4
Enter number of columns:
4
Enter matrix:
1234
5678
9876

9
5432
Trace of entered matrix is:16
Norm of entered matrix is:22.090722034374522

Enter number of rows:


3
Enter number of columns:
3
Enter matrix:
100
090
001
Trace of entered matrix is:11
Norm of entered matrix is:9.1104335791443

__________________________________________________________________________________

10

You might also like