0% found this document useful (0 votes)
11 views7 pages

Omoops 3

The document outlines an experiment focused on programming with 1D and 2D arrays in Java, including searching, merging arrays, checking matrix symmetry, and performing matrix multiplication. It aims to teach students the basics of arrays in Java compared to C, covering syntax, initialization methods, and array properties. The conclusion highlights the ArrayIndexOutOfBoundsException as a key concept.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Omoops 3

The document outlines an experiment focused on programming with 1D and 2D arrays in Java, including searching, merging arrays, checking matrix symmetry, and performing matrix multiplication. It aims to teach students the basics of arrays in Java compared to C, covering syntax, initialization methods, and array properties. The conclusion highlights the ArrayIndexOutOfBoundsException as a key concept.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment : 3

AIM : Program on Arrays


1D Arrays
To search an given element in the Array. To merge two arrays.
2D arrays
To check if the entered matrix is symmetric or not. To perform Matrix Multiplication
To find out trace and Norm of a particular Matrix.

OBJECTIVE : To make students understand the basics of Arrays in Java and their major difference with
Arrays in C

Description :

Write the following about both 1D and 2D arrays : What are arrays in General.
Syntax of array declaration, creation and initialization
Static and dynamic initialization methods
Default initialisation
Write about arraycopy method(refer book of authorThampi) and Length Atribute
Difference between arrays in c and java

CONCLUSION: Mention about ArrayIndexOutOfBoundsException.


REFERENCES :
1D Arrays

To search an given element in the Array:


To search an given element in the Array:
import java.util.Scanner;

public class ArraySearch { public


static void main(String[] args) { int[]
array = {1, 2, 3, 4, 5, 6, 7, 8, 9};

System.out.println(“Om Gaikwad”);
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the element to search: ");
int element = scanner.nextInt(); int index =
searchArray(array, element); if (index != -1) {
System.out.println("Element found at index " + index);
} else {
System.out.println("Element not found in the array");
}
scanner.close();
}

public static int searchArray(int[] array, int element) {


for (int i = 0; i < array.length; i++) { if (array[i] ==
element) { return i;
}
} return -
1;
}
}

Output:

To merge two arrays: import

java.util.Scanner;

public class ArrayMerge { public


static void main(String[] args) {
System.out.println(“Om Gaikwad”);
int[] array1 = {10,20,30,40};
int[] array2 = {50, 60,70,80};
int[] mergedArray = mergeArrays(array1, array2);
System.out.println("Merged array: "); for (int i =
0; i < mergedArray.length; i++) {
System.out.print(mergedArray[i] + " ");
}
}

public static int[] mergeArrays(int[] array1, int[] array2) {


int[] mergedArray = new int[array1.length + array2.length];
System.arraycopy(array1, 0, mergedArray, 0, array1.length);
System.arraycopy(array2, 0, mergedArray, array1.length, array2.length);
return mergedArray;
}
}
Output:

2D arrays

To check if the entered matrix is symmetric or not

import java.util.Scanner;

public class MatrixSymmetry { public


static void main(String[] args) {

System.out.println(“Om Gaikwad”);

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt(); int[][] matrix =
new int[rows][columns]; System.out.println("Enter
the matrix elements: "); for (int i = 0; i < rows; i++)
{ for (int j = 0; j < columns; j++) {
matrix[i][j] = scanner.nextInt();
}
}
if (isSymmetric(matrix)) {
System.out.println("The matrix is symmetric");
} else {
System.out.println("The matrix is not symmetric");
}
scanner.close();
}

public static boolean isSymmetric(int[][] matrix) {


int rows = matrix.length; int columns =
matrix[0].length; if (rows != columns) {
return false;
} for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < columns; j++) {
if (matrix[i][j] != matrix[j][i]) {
return false;
}
}
}
return true;
}
}
Output:
To perform Matrix Multiplication:
import java.util.Scanner;

public class MatrixMultiplication {


public static void main(String[] args) {

System.out.println(“Om Gaikwad”);

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of rows in the first matrix: ");


int rows1 = scanner.nextInt();
System.out.print("Enter the number of columns in the first matrix: ");
int columns1 = scanner.nextInt();
System.out.print("Enter the number of rows in the second matrix: ");
int rows2 = scanner.nextInt();
System.out.print("Enter the number of columns in the second matrix: ");
int columns2 = scanner.nextInt(); if (columns1 != rows2) {
System.out.println("Matrix multiplication is not possible");
return;
}
int[][] matrix1 = new int[rows1][columns1];
int[][] matrix2 = new int[rows2][columns2];
System.out.println("Enter the elements of the first matrix: ");
for (int i = 0; i < rows1; i++) { for (int j = 0; j < columns1;
j++) { matrix1[i][j] = scanner.nextInt();
}
}
System.out.println("Enter the elements of the second matrix: ");
for (int i = 0; i < rows2; i++) { for (int j = 0; j < columns2; j++)
{ matrix2[i][j] = scanner.nextInt();
}
}
int[][] result = multiplyMatrices(matrix1, matrix2);
System.out.println("Result of matrix multiplication: ");
for (int i = 0; i < rows1; i++) { for (int j = 0; j <
columns2; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
scanner.close();
}

public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {


int rows1 = matrix1.length; int columns1 = matrix1[0].length;
int rows2 = matrix2.length; int columns2 = matrix2[0].length;
int[][] result = new int[rows1][columns2]; for (int i = 0; i <
rows1; i++) { for (int j = 0; j < columns2; j++) {

for (int k = 0; k < columns1; k++) {


result[i][j] += matrix1[i][k] * matrix2[k][j]; }
}
} return
result;
}
}
Output:

You might also like