[go: up one dir, main page]

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

Experiment 3 Java Prachi

The document outlines an experiment focused on programming with arrays in Java, covering both 1D and 2D arrays. It includes objectives such as searching for elements, merging arrays, checking for symmetry in matrices, performing matrix multiplication, and calculating the trace and norm of matrices. The document also discusses array initialization methods, differences between Java and C arrays, and concludes with a mention of ArrayIndexOutOfBoundsException.

Uploaded by

Prachi Thakare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Experiment 3 Java Prachi

The document outlines an experiment focused on programming with arrays in Java, covering both 1D and 2D arrays. It includes objectives such as searching for elements, merging arrays, checking for symmetry in matrices, performing matrix multiplication, and calculating the trace and norm of matrices. The document also discusses array initialization methods, differences between Java and C arrays, and concludes with a mention of ArrayIndexOutOfBoundsException.

Uploaded by

Prachi Thakare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment 3

AIM:Program in Arrays
1D array
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.

import java.util.Scanner;

public class ArraySearch {


public static void main(String[] args) {
System.out.println("Prachi Thakare Roll no. 100");

int[] numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

Scanner scanner = new Scanner(System.in);


System.out.print("Enter an element to search: ");
int searchElement = scanner.nextInt();

boolean found = false;

for (int i = 0; i < numbers.length; i++) {


if (numbers[i] == searchElement) {
System.out.println("Element " + searchElement + " found at index: " + i);
found = true;
break;
}
}
if (!found) {
System.out.println("Element " + searchElement + " not found in the array.");
}

scanner.close();
}
}

To merge two arrays

import java.util.Arrays;
public class MergeArrays {
public static void main(String[] args) {
System.out.println("Prachi Thakare Roll no. 100");

int[] array1 = {1, 2, 3, 4, 5};


int[] array2 = {6, 7, 8, 9, 10};
int[] mergedArray = new int[array1.length + array2.length];

for (int i = 0; i < array1.length; i++) {


mergedArray[i] = array1[i];
}

for (int i = 0; i < array2.length; i++) {


mergedArray[array1.length + i] = array2[i];
}
System.out.println("Merged Array: " + Arrays.toString(mergedArray));
}
}
2D arrays
To check if the entered matrix is symmetric or not.
import java.util.Scanner;

public class SymmetricMatrixCheck {

public static void main(String[] args) {


System.out.println("Prachi Thakare Roll No. 100");

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of rows and columns (n for n x n matrix): ");


int n = scanner.nextInt();

int[][] matrix = new int[n][n];

System.out.println("Enter the elements of the matrix:");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextInt();
}
}

if (isSymmetric(matrix, n)) {
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 n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] != matrix[j][i]) {
return false;
}
}
}
return true;
}
}
To perform Matrix Multiplication

import java.util.Scanner;

public class MatrixMultiplication {

public static void main(String[] args) {


System.out.println("Prachi Thakare Roll No. 100");

Scanner scanner = new Scanner(System.in);

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


int rowsA = scanner.nextInt();
System.out.print("Enter the number of columns for the first matrix: ");
int colsA = scanner.nextInt();

int[][] matrixA = new int[rowsA][colsA];

System.out.println("Enter the elements of the first matrix:");


for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
matrixA[i][j] = scanner.nextInt();
}
}

System.out.print("Enter the number of rows for the second matrix: ");


int rowsB = scanner.nextInt();
System.out.print("Enter the number of columns for the second matrix: ");
int colsB = scanner.nextInt();

if (colsA != rowsB) {
System.out.println("Matrix multiplication is not possible.");
return;
}

int[][] matrixB = new int[rowsB][colsB];

System.out.println("Enter the elements of the second matrix:");


for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
matrixB[i][j] = scanner.nextInt();
}
}

int[][] resultMatrix = new int[rowsA][colsB];

for (int i = 0; i < rowsA; i++) {


for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
resultMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}

System.out.println("The resulting matrix is:");


for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
System.out.print(resultMatrix[i][j] + " ");
}
System.out.println();
}

scanner.close();
}
}
To find out trace and Norm of a particular Matrix.
import java.util.Scanner;

public class MatrixOperations {


public static void main(String[] args) {
System.out.println("Prachi Thakare Roll number 100");

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 cols = scanner.nextInt();

double[][] matrix = new double[rows][cols];

System.out.println("Enter the elements of the matrix:");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = scanner.nextDouble();
}
}

double trace = calculateTrace(matrix, rows, cols);


System.out.println("Trace of the matrix: " + trace);

double norm = calculateNorm(matrix, rows, cols);


System.out.println("Frobenius norm of the matrix: " + norm);

scanner.close();
}

public static double calculateTrace(double[][] matrix, int rows, int cols) {


double trace = 0.0;
if (rows == cols) {
for (int i = 0; i < rows; i++) {
trace += matrix[i][i];
}
} else {
System.out.println("Trace is only defined for square matrices.");
}
return trace;
}

public static double calculateNorm(double[][] matrix, int rows, int cols) {


double sum = 0.0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum += matrix[i][j] * matrix[i][j];
}
}
return Math.sqrt(sum);
}
}

You might also like