0% found this document useful (0 votes)
17 views3 pages

Java 2D Array Operations Guide

The document defines methods to initialize and manipulate a 2D integer array in Java. It initializes a 5x5 array with random numbers, then prints: 1) all elements, 2) the sum of prime numbers, 3) main diagonal elements, 4) sum below diagonal, 5) sum above diagonal, 6) odd below diagonal elements, 7) even above diagonal elements. Methods are defined to initialize the array, print elements, check for primes, and calculate/print sums and elements based on array position.
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)
17 views3 pages

Java 2D Array Operations Guide

The document defines methods to initialize and manipulate a 2D integer array in Java. It initializes a 5x5 array with random numbers, then prints: 1) all elements, 2) the sum of prime numbers, 3) main diagonal elements, 4) sum below diagonal, 5) sum above diagonal, 6) odd below diagonal elements, 7) even above diagonal elements. Methods are defined to initialize the array, print elements, check for primes, and calculate/print sums and elements based on array position.
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

import [Link].

Random;
public class Surname_2Dim {

public static void main(String[] args) {

//2D array of 5*5 dimension


int[][] a=new int[5][5];
//2D array element between 10-75
initArray(a, 10, 75);

//Step 1: Output the array Elements


[Link]("1) Output the array Elements");
printElements(a);
[Link]();

//Step 2: Output the sum of prime numbers in array


[Link]("2) Output the sum of prime numbers in array");
sumPrime(a);
[Link]();

//Step 3: Output the elements in main diagonal


[Link]("3) Output the elements in main diagonal");
mainDiagonal(a);
[Link]();

//Step 4: Output the sum of the elements below diagonal


[Link]("4) Output the sum of the elements below diagonal");
sumBelowDiagonal(a);
[Link]();

//Step 5: Output the sum of the elements above diagonal


[Link]("5) Output the sum of the elements above diagonal");
sumAboveDiagonal(a);
[Link]();

//Step 6: Output the odd elements below diagonal


[Link]("6) Output the odd elements below diagonal");
oddBelowDiagonal(a);
[Link]();

//Step 7: Output the even elements above diagonal


[Link]("7) Output the even elements above diagonal");
evenAboveDiagonal(a);
[Link]();
}
//Initialize array a with random numbers between range min to max
static void initArray(int[][] a,int min,int max) {
Random rand = new Random();
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
a[i][j]=min + ([Link](Integer.MAX_VALUE) % ((max-min)+1));
}
}
//Print all the array elements in formatted manner
static void printElements(int[][] a) {
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
[Link]("%2d\t", a[i][j]);
[Link]();
}
}
//Check if num is prime or not if it is return true otherwise, it is false
static boolean isPrime(int num) {
double sqroot = [Link](num*1.0);
for(int i=2;i<=sqroot;i++) {
if(num%i==0)
return false;
}
return true;
}
//Sum up all the prime numbers in the array a
static void sumPrime(int[][] a) {
int sum=0;
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
if(isPrime(a[i][j]))
sum+=a[i][j];
}
[Link](sum);
}
//Print the main diagonal elements
static void mainDiagonal(int[][] a) {
for(int i=0;i<[Link];i++)
[Link]("%2d\t",a[i][i]);
[Link]();
}
//Calculate and print the sum of elements below diagonal
static void sumBelowDiagonal(int[][] a) {
int sum=0;
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
if(i>j) {
sum+=a[i][j];
}
}
[Link](sum);
}
//Calculate and print the sum of elements above diagonal
static void sumAboveDiagonal(int[][] a) {
int sum=0;
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
if(i<j) {
sum+=a[i][j];
}
}
[Link](sum);
}
//Print the odd elements below diagonal
static void oddBelowDiagonal(int[][] a) {
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
if(i>j && a[i][j]%2==1) {
[Link]("%2d\t",a[i][j]);
}
}
[Link]();
}
//Print the even elements above diagonal
static void evenAboveDiagonal(int[][] a) {
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
if(i<j && a[i][j]%2==0) {
[Link]("%2d\t",a[i][j]);
}
}
[Link]();
}
}

Common questions

Powered by AI

The program identifies even numbers above the main diagonal by iterating through the matrix and selecting elements where i < j. It checks if each element is even using modulus operation (% 2 == 0) and then prints these elements .

The program calculates the sum of elements below the diagonal by iterating through the matrix and summing elements where the row index is greater than the column index (i > j). Each of these elements is added to a cumulative sum, which is then printed .

The sum of prime numbers in a 5x5 matrix is calculated by iterating over each element in the matrix and checking if it is prime using the isPrime method. If an element is prime, it is added to a running sum. The isPrime method checks divisibility from 2 up to the square root of the number. Finally, the sum is printed .

The program initializes the 5x5 matrix using the initArray method, which assigns each element a random integer between specified minimum and maximum values (10 and 75). It uses the Random class to generate such numbers utilizing rand.nextInt with a range derived from the difference between max and min .

The program prints the main diagonal elements of the 2D array by iterating through indices where row and column are equal (i.e., a[i][i] for i ranging from 0 to array length). It accesses these diagonally aligned elements and prints each one in sequence .

The method checks primality by iterating from 2 up to the square root of the number, which is efficient for small numbers but could be optimized for larger matrices and values by incorporating checks for known small prime divisors or by using the Sieve of Eratosthenes for range pre-checks. This improved method would reduce redundant checks and provide scalability for larger datasets .

The Java program illustrates matrix manipulation and traversal by demonstrating several operations: initializing elements, iterating over elements to sum primes, accessing specific parts like the diagonal, and selectively printing based on conditions (prime, odd, even). It highlights how nested loops facilitate detailed control over matrix operations .

The program is fairly rigid in structure due to hard-coded dimensions and element properties scattered across multiple methods. To modify the matrix size or element properties, you would need to adjust several method definitions and loops (e.g., changing array length checks and index logic), meaning it isn't highly modular or scalable without significant modifications in logic .

The algorithm uses a method named isPrime that determines if a number is prime by checking divisibility from 2 to the square root of the number. For each number in the 2D array, it checks divisibility and returns false if the number is divisible by any number in this range; otherwise, returns true. This method is applied to each element to identify prime numbers .

The program identifies odd numbers below the diagonal by iterating through each element (i, j) where i is greater than j. It checks if each element is odd (using modulus operation % 2) and if so, prints the element. This captures all odd numbers below the diagonal .

You might also like