Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Interchange the Diagonals
In this article, we will understand how to interchange the diagonal elements of a given matrix in Java. A matrix is a two-dimensional array made up of rows and columns. A matrix with m rows and n columns is called an m à n matrix.
Each element in the matrix is represented by a[i][j], which means that the particular element a is present in the i-th row and j-th column.
Problem Statement
Write a Java program to interchange the elements of the primary and secondary diagonals of a square matrix.
Example Scenario
Consider the following example scenario to understand the problem better:
Input: The matrix is defined as: 4 5 6 1 2 3 7 8 9 Output: The matrix after interchanging the elements: 6 5 4 1 2 3 9 8 7
Steps to Swap the Diagonal Elements of a Matrix
Following are the steps to achieve this:
- Define a square matrix with values.
- Iterate through each row from index
0tomatrix_size - 1. - For each row
i, check if it is not the middle row using the conditioni != matrix_size / 2. - If true, swap the primary diagonal element with the secondary diagonal element.
- Display the resulting matrix.
Example 1: Interchanging Diagonals
The following example demonstrates how to interchange the diagonal elements of a square matrix:
public class InterchangeDiagonals {
public static int matrix_size = 3;
public static void main (String[] args) {
int input_matrix[][] = {
{4, 5, 6},
{1, 2, 3},
{7, 8, 9}
};
System.out.println("The matrix is defined as: ");
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
System.out.print(input_matrix[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i < matrix_size; ++i)
if (i != matrix_size / 2) {
int temp = input_matrix[i][i];
input_matrix[i][i] = input_matrix[i][matrix_size - i - 1];
input_matrix[i][matrix_size - i - 1] = temp;
}
System.out.println("\nThe matrix after interchanging the elements: ");
for (int i = 0; i < matrix_size; ++i) {
for (int j = 0; j < matrix_size; ++j)
System.out.print(input_matrix[i][j]+" ");
System.out.println();
}
}
}
Output
The matrix is defined as: 4 5 6 1 2 3 7 8 9 The matrix after interchanging the elements: 6 5 4 1 2 3 9 8 7
Function-Based Approach
In this approach, we encapsulate the operations into a function, which also demonstrates the principles of object-oriented programming.
Example 2: Function-Based Implementation
The following example demonstrates how to interchange the diagonal elements of a square matrix using a separate method:
public class InterchangeDiagonals {
public static int matrix_size = 3;
static void interchange_diagonals(int input_matrix[][]) {
for (int i = 0; i < matrix_size; ++i)
if (i != matrix_size / 2) {
int temp = input_matrix[i][i];
input_matrix[i][i] = input_matrix[i][matrix_size - i - 1];
input_matrix[i][matrix_size - i - 1] = temp;
}
System.out.println("\nThe matrix after interchanging the elements: ");
for (int i = 0; i < matrix_size; ++i) {
for (int j = 0; j < matrix_size; ++j)
System.out.print(input_matrix[i][j]+" ");
System.out.println();
}
}
public static void main (String[] args) {
int input_matrix[][] = {
{4, 5, 6},
{1, 2, 3},
{7, 8, 9}
};
System.out.println("The matrix is defined as: ");
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
System.out.print(input_matrix[i][j] + " ");
}
System.out.println();
}
interchange_diagonals(input_matrix);
}
}
Output
The matrix is defined as: 4 5 6 1 2 3 7 8 9 The matrix after interchanging the elements: 6 5 4 1 2 3 9 8 7