0% found this document useful (0 votes)
6 views

Day3 FlowControl

The document contains two Java programming questions and their solutions. The first question involves writing a method to swap the primary and secondary diagonals of a 2D array. The second question involves writing a method to find the sum of the left, top, right and down elements of a given target element in a 2D array.

Uploaded by

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

Day3 FlowControl

The document contains two Java programming questions and their solutions. The first question involves writing a method to swap the primary and secondary diagonals of a 2D array. The second question involves writing a method to find the sum of the left, top, right and down elements of a given target element in a 2D array.

Uploaded by

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

que: Write a Java Program to swap primary with secondary Diagonal elements and

vice versa from the given

2-d array using static method

i/p: Enter row size:

Enter column size:

Enter 9 elements into 2-d array:

1 2 3

4 5 6

7 8 9

o/p: 3 2 1

4 5 6

9 8 7

Note: classname: Class2dArray

methodname: swapDiagonals()

ANS::
import java.util.Scanner;

public class Class2dArray {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter row size:");


int rows = scanner.nextInt();

System.out.println("Enter column size:");


int cols = scanner.nextInt();

int[][] array = new int[rows][cols];

System.out.println("Enter " + (rows * cols) + " elements into 2-d array:");


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

System.out.println("Original Array:");
printArray(array);

swapDiagonals(array);

System.out.println("Array after swapping diagonals:");


printArray(array);
}

public static void swapDiagonals(int[][] array) {


int rows = array.length;
int cols = array[0].length;

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


int temp = array[i][i];
array[i][i] = array[i][cols - 1 - i];
array[i][cols - 1 - i] = temp;
}
}

public static void printArray(int[][] array) {


for (int[] row : array) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}

question:: Write a Java Program to find sum of left,top,right,down element for


the given array element

2-d array using static method

i/p: Enter row size:

Enter column size:

Enter 9 elements into 2-d array:

1 2 3

4 5 6

7 8 9

Enter the element to find sum:

o/p: sum of left,top,right,down for 5 iss 20


Note: classname: Class2dArraySum

methodname: sumOfNeighbourElements()

ANSWER:: import java.util.Scanner;

public class Class2dArraySum {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter row size:");


int rows = scanner.nextInt();

System.out.println("Enter column size:");


int cols = scanner.nextInt();

int[][] array = new int[rows][cols];

System.out.println("Enter " + (rows * cols) + " elements into 2-d array:");


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

System.out.println("Enter the element to find sum:");


int target = scanner.nextInt();

int sum = sumOfNeighbourElements(array, target);


System.out.println("Sum of left, top, right, down elements for " + target +
" is " + sum);
}

public static int sumOfNeighbourElements(int[][] array, int target) {


int sum = 0;
int rows = array.length;
int cols = array[0].length;

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


for (int j = 0; j < cols; j++) {
if (array[i][j] == target) {
// Sum of left element
if (j - 1 >= 0) {
sum += array[i][j - 1];
}
// Sum of top element
if (i - 1 >= 0) {
sum += array[i - 1][j];
}
// Sum of right element
if (j + 1 < cols) {
sum += array[i][j + 1];
}
// Sum of down element
if (i + 1 < rows) {
sum += array[i + 1][j];
}
return sum;
}
}
}
return sum;
}
}

You might also like