7. Write a program to delete an element at desired position from an array.
PROGRAM:
import [Link];
class lab6ha
{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
int array[]=new int[100];
int pos,c,n;
[Link]("Enter number of elements in array");
n=[Link]();
[Link]("Enter "+n+" Element:");
for(c=0;c<n;c++)
array[c]=[Link]();
[Link]("Enter the desired position to delete the element in an array");
pos=[Link]();
if(pos>=n+1)
[Link]("Deletion not possible.");
else
{
for(c=pos-1;c<n-1;c++)
array[c]=array[c+1];
[Link]("Resultant array is");
for(c=0;c<n-1;c++)
[Link](array[c]);
}
}}
OUTPUT:
8. Write a program to find the second largest element in an array
PROGRAM:
import [Link];
public class lab6ia{
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of elements you want in array(Minimum 2):");
n = [Link]();
int a[] = new int[n];
[Link]("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = [Link]();}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;}}}
[Link]("Second Largest:"+a[n-2]);}}
OUTPUT:
9. Write a program for a 2D array of size MxN and print the matrix and perform
multiplication of two Matrices
PROGRAM:
import [Link];
public class lab6ja
{ public static void main(String[] args){
int[][] matrix1,matrix2;
int mat1r=0,mat1c=0,mat2r=0,mat2c=0;
Scanner input = new Scanner([Link]);
for(int i=1;i<=2;i++)
{ [Link]("\n Enter the order for matrix %d : ",i);
if(i==1){
mat1r=[Link]();
mat1c=[Link]();}
else
{ mat2r=[Link]();
mat2c=[Link]();}}
matrix1 = new int[mat1r][mat1c];
matrix2 = new int[mat2r][mat2c];
int[][] resultantMatrix= new int[mat1r][mat2c];
if( mat1c !=mat2r)
{ [Link]("\n This order calculation is not possible "); }
else{
[Link]("\n matrix 1 ");
for(int i=0 ;i<mat1r ;i++)
{ for(int j=0;j<mat1c;j++)
{ [Link]("\n Enter the value for matrix 1 [%d][%d] : ",i+1,j+1);
matrix1[i][j]=[Link]();}}
[Link]("\n Matrix 2 ");
for(int i=0;i<mat2r;i++)
{ for(int j=0;j<mat2c;j++)
{ [Link]("\n Enter the value for matrix 2 [%d][%d] : ",i+1,j+1);
matrix2[i][j]=[Link](); }}
[Link]("\n MATRIX 1");
for(int i=0 ;i<mat1r;i++)
{ for(int j=0;j<mat1c;j++)
{ [Link](matrix1[i][j]+" ");}
[Link]();}
[Link]("\n MATRIX 2");
for(int i=0 ;i<mat2r;i++)
{ for(int j=0;j<mat2c;j++)
{ [Link](matrix2[i][j]+" ");}
[Link](); }
for(int i=0;i<mat1r;i++)
{ for(int j=0;j<mat2c;j++)
{ for(int k=0;k<mat2r;k++)
{
resultantMatrix[i][j]+=matrix1[i][k]*matrix2[k][j]; }}} [Link]("\n
RESULTANT MATRIX ");
for(int i=0;i<mat1r;i++)
{
for(int j=0;j<mat2c;j++)
{
[Link](resultantMatrix[i][j]+" ");
} [Link]();}} }}
OUTPUT:
CONCLUSION:
In this lab we learn how to use arrays in java and we also learn different task
that how to insert elemnt in array,find the largest no.,matrix multiplication
and many more.