Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Array Java 13

The document discusses programs to perform operations on arrays in Java like deleting an element from an array, finding the second largest element, and multiplying two matrices. It includes the code to implement these operations and sample outputs.

Uploaded by

Anas Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Array Java 13

The document discusses programs to perform operations on arrays in Java like deleting an element from an array, finding the second largest element, and multiplying two matrices. It includes the code to implement these operations and sample outputs.

Uploaded by

Anas Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

7. Write a program to delete an element at desired position from an array.

PROGRAM:
import java.util.Scanner;
class lab6ha
{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int array[]=new int[100];
int pos,c,n;
System.out.println("Enter number of elements in array");
n=sc.nextInt();
System.out.println("Enter "+n+" Element:");
for(c=0;c<n;c++)
array[c]=sc.nextInt();
System.out.println("Enter the desired position to delete the element in an array");
pos=sc.nextInt();
if(pos>=n+1)
System.out.println("Deletion not possible.");
else
{
for(c=pos-1;c<n-1;c++)
array[c]=array[c+1];
System.out.println("Resultant array is");
for(c=0;c<n-1;c++)
System.out.println(array[c]);
}
}}

OUTPUT:

8. Write a program to find the second largest element in an array


PROGRAM:
import java.util.Scanner;
public class lab6ia{
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array(Minimum 2):");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();}
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;}}}
System.out.println("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 java.util.Scanner;
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(System.in);
for(int i=1;i<=2;i++)
{ System.out.printf("\n Enter the order for matrix %d : ",i);
if(i==1){
mat1r=input.nextInt();
mat1c=input.nextInt();}
else
{ mat2r=input.nextInt();
mat2c=input.nextInt();}}
matrix1 = new int[mat1r][mat1c];
matrix2 = new int[mat2r][mat2c];
int[][] resultantMatrix= new int[mat1r][mat2c];
if( mat1c !=mat2r)
{ System.out.print("\n This order calculation is not possible "); }
else{
System.out.println("\n matrix 1 ");
for(int i=0 ;i<mat1r ;i++)
{ for(int j=0;j<mat1c;j++)
{ System.out.printf("\n Enter the value for matrix 1 [%d][%d] : ",i+1,j+1);
matrix1[i][j]=input.nextInt();}}
System.out.println("\n Matrix 2 ");
for(int i=0;i<mat2r;i++)
{ for(int j=0;j<mat2c;j++)
{ System.out.printf("\n Enter the value for matrix 2 [%d][%d] : ",i+1,j+1);
matrix2[i][j]=input.nextInt(); }}
System.out.println("\n MATRIX 1");
for(int i=0 ;i<mat1r;i++)
{ for(int j=0;j<mat1c;j++)
{ System.out.print(matrix1[i][j]+" ");}
System.out.println();}
System.out.println("\n MATRIX 2");
for(int i=0 ;i<mat2r;i++)
{ for(int j=0;j<mat2c;j++)
{ System.out.print(matrix2[i][j]+" ");}
System.out.println(); }
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]; }}} System.out.println("\n
RESULTANT MATRIX ");
for(int i=0;i<mat1r;i++)
{
for(int j=0;j<mat2c;j++)
{
System.out.print(resultantMatrix[i][j]+" ");
} System.out.println();}} }}

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.

You might also like