- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Decrement Matrix Elements by One in Java?
Matrices are nothing but it’s more than a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.
As per the problem statement we have to subtract an element “1” to each matrix element.
Let’s deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Suppose the original matrix is {{3,1,2},{2,5,9},{6,3,10}};
After subtracting the element “1” by given matrix, the result index will be −
2 0 1 1 4 8 5 2 9
Instance-2
Suppose the original matrix is {{2,1,9},{5,5,7},{3,6,10}};
After subtracting the element “1” by given matrix, the result index will be −
1 0 8 4 4 6 2 5 9
Algorithm
Step-1 − Declare and initialize the matrix.
Step-2 − Create another matrix to store the addition value.
Step-3 − Subtract the element “1” to the matrix using c[i][j] += a[i][j] - 1.
Step-4 − Print the result.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Matrix
By Using User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Ternary Operator
In this approach, matrix elements will be initialized in the program. Then as per the algorithm decrement matrix elements by one.
Example
public class Main{ //main method public static void main(String args[]){ //Initialising the matrix int a[][]={{3,1,2},{2,5,9},{6,3,10}}; //creating another matrix to store the addition value int c[][]=new int[3][3]; System.out.println("After decrementing each element by 1:"); //addition of element 1 to the matrix for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ c[i][j]+=a[i][j] - 1; //printing the result System.out.print(c[i][j]+" "); } //new line System.out.println(); } } }
Output
After decrementing each element by 1: 2 0 1 1 4 8 5 2 9
Approach-2: By Using User Defined Method
In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm decrement matrix elements by one.
Example
public class Main{ //main method public static void main(String args[]){ //Initialising the matrix int arr[][]={{2,1,9},{5,5,7},{3,6,10}}; //calling user defined method func(arr); } //user defined method static void func(int[][] arr){ //creating another matrix to store the addition value int c[][]=new int[3][3]; System.out.println("After decrementing each element by 1:"); //addition of element 1 to the matrix for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ c[i][j]+=arr[i][j] - 1; //printing the result System.out.print(c[i][j]+" "); } //new line System.out.println(); } } }
Output
After decrementing each element by 1: 1 0 8 4 4 6 2 5 9
In this article, we explored different approaches to decrement matrix elements by one by using Java programming language.