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

Java Matrix and Array Operations

Java program class 10

Uploaded by

sidhuprasad242
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)
12 views7 pages

Java Matrix and Array Operations

Java program class 10

Uploaded by

sidhuprasad242
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

import [Link].

*;
class Transpose {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int r = [Link]();
[Link]("Enter number of columns: ");
int c = [Link]();
int arr[][] = new int[r][c];

[Link]("Enter elements:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr[i][j] = [Link]();
}
}

[Link]("Transpose:");
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++) {
[Link](arr[j][i] + " ");
}
[Link]();
}
}
}

import [Link].*;
class SymmetricCheck {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of square matrix: ");
int n = [Link]();
int arr[][] = new int[n][n];

[Link]("Enter elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = [Link]();
}
}

boolean symmetric = true;


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] != arr[j][i]) {
symmetric = false;
break;
}
}
if (!symmetric) break;
}

if (symmetric)
[Link]("The matrix is Symmetric.");
else
[Link]("The matrix is NOT Symmetric.");
}
}

import [Link].*;
class DiagonalSum {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int m[][] = new int[4][4]; // fixed 4x4 matrix like in book
[Link]("Enter 16 elements:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
m[i][j] = [Link]();
}
}

int ld = 0, rd = 0;

// Left diagonal
for (int i = 0; i < 4; i++) {
ld += m[i][i];
}

// Right diagonal with k = 3


int k = 3; // hardcoded for 4x4 matrix
for (int i = 0; i < 4; i++) {
rd += m[i][k];
k--;
}
[Link]("Left Diagonal Sum: " + ld);
[Link]("Right Diagonal Sum: " + rd);
}
}

import [Link].*;
class RowColSum {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int r = [Link]();
[Link]("Enter number of columns: ");
int c = [Link]();
int arr[][] = new int[r][c];

[Link]("Enter the elements:");


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr[i][j] = [Link]();
}
}

// Row sums
[Link]("\nSum of each row:");
for (int i = 0; i < r; i++) {
int rowSum = 0;
for (int j = 0; j < c; j++) {
rowSum += arr[i][j]; // row fixed, loop through columns
}
[Link]("Row " + (i + 1) + ": " + rowSum);
}

// Column sums (note the swapped indices)


[Link]("\nSum of each column:");
for (int i = 0; i < c; i++) { // now i = column index
int colSum = 0;
for (int j = 0; j < r; j++) { // j = row index
colSum += arr[j][i]; // accessing by [row][column]
}
[Link]("Column " + (i + 1) + ": " + colSum);
}
}
}

import [Link].*;
class ReverseArray {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements:");
int n = [Link]();
int arr[] = new int[n];
[Link]("Enter " + n + " numbers:");
for (int i = 0; i < n; i++)
arr[i] = [Link]();

[Link]("Array in reverse order:");


for (int i = n - 1; i >= 0; i--)
[Link](arr[i] + " ");
}
}

import [Link].*;
class SortArray {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements:");
int n = [Link]();
int arr[] = new int[n];
[Link]("Enter " + n + " numbers:");
for (int i = 0; i < n; i++)
arr[i] = [Link]();
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
[Link]("Sorted array:");
for (int i = 0; i < n; i++)
[Link](arr[i] + " ");
}
}

import [Link].*;
class EvenOddCount {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements:");
int n = [Link]();
int arr[] = new int[n];
int even = 0, odd = 0;
[Link]("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
if (arr[i] % 2 == 0) even++;
else odd++;
}
[Link]("Even count: " + even);
[Link]("Odd count: " + odd);
}
}

import [Link].*;
class LargestSmallest {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements:");
int n = [Link]();
int arr[] = new int[n];
[Link]("Enter " + n + " numbers:");
for (int i = 0; i < n; i++)
arr[i] = [Link]();
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
[Link]("Largest: " + max);
[Link]("Smallest: " + min);
}
}

import [Link].*;
class SearchArray {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements:");
int n = [Link]();
int arr[] = new int[n];
[Link]("Enter " + n + " numbers:");
for (int i = 0; i < n; i++)
arr[i] = [Link]();
[Link]("Enter number to search:");
int key = [Link]();
boolean found = false;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
found = true;
break;
}
}
if (found)
[Link]("Element found.");
else
[Link]("Element not found.");
}
}

import [Link].*;
class ArraySum {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements:");
int n = [Link]();
int arr[] = new int[n];
[Link]("Enter " + n + " numbers:");
int sum = 0;
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
sum += arr[i];
}
[Link]("Sum of elements: " + sum);
}
}

Common questions

Powered by AI

The algorithm iterates through each element in the array, using a simple if-else condition to check each number's evenness or oddness by applying the modulus operator (% 2). Two separate counters increment based on these conditions, storing even and odd counts respectively. This method employs a single-pass O(n) computation utilizing primitive integer operations as the primary data structure .

The steps for finding diagonal sums include iterating over indices to accumulate sums for both the primary (left) diagonal and the secondary (right) diagonal. For a fixed-size matrix, the indices are directly used, while a variable-sized matrix requires calculated indices (i.e., n-i-1 for the right diagonal). Challenges include correctly indexing elements and ensuring array-bound safety, especially on varying sizes, maintaining O(n) complexity for direct index operations .

The Java implementation reverses an array by iterating backwards from the last element to the first, directly printing each element in reverse order. This illustrates the concept of indexing, emphasizing that direct index manipulation allows efficient access to elements without altering the original array. It demonstrates how arrays provide ordered, indexed data storage, crucial in various algorithmic operations .

Transposing a matrix involves swapping its row and column indices. Programmatically, this is achieved by two nested loops: the outer loop iterates over columns, and the inner loop iterates over rows, printing or storing elements in the transposed order. The time complexity remains O(r * c) for a matrix of size r x c, as each element is accessed once .

The algorithm initializes variables to the first element of the array. It then iterates through remaining elements, comparing each to the current max and min values, updating these variables when a larger or smaller element is found, respectively. This approach involves a single pass through the array, ensuring an O(n) complexity and effectively balances simplicity with performance given its linear operation .

Hardcoding matrix sizes simplifies implementation and ensures compatibility with specific requirements, allowing manual optimization. However, it reduces flexibility, making the code less reusable and adaptable to different matrix sizes. This approach limits dynamic operations and makes the program less scalable, an essential consideration for applications requiring flexible inputs .

The implementation uses nested loops to perform a selection sort, comparing and swapping elements to achieve an ordered array. This approach has a time complexity of O(n^2), which is suboptimal for large datasets. Potential optimizations include breaking early if no swaps occur during an iteration, indicating sorted state. Furthermore, alternative algorithms with better average performance on larger datasets, like quicksort or mergesort (O(n log n)), could replace selection sort .

The program uses separate loops to calculate row and column sums. Row sums are calculated by fixing rows and iterating columns, while column sums fix columns and iterate rows. Incorrect index handling, such as mixing row and column indices, can lead to incorrect sums or out-of-bound errors. The distinct use of indices necessitates attention to avoid accessing invalid elements, illustrating key concepts of dimension handling in multi-dimensional arrays .

The simple search algorithm sequentially checks each array element against the target value. Its effectiveness is straightforward but limited, as it operates with a time complexity of O(n), where n is the number of elements. This makes it inefficient for large datasets, where more sophisticated search algorithms like binary search (with O(log n) complexity) would be preferable if data is sorted .

The symmetry check algorithm involves comparing each element in the matrix with its counterpart (i.e., checking if arr[i][j] equals arr[j][i] for all elements). The computational implication of this algorithm is that it requires O(n^2) comparisons for an n x n matrix, as each element must be checked against its transposed position. The program loops through half of the matrix (the upper triangle) efficiently avoiding redundant checks, as each element is compared once.

You might also like