Skip to content
Open

An #570

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions java/matrix/MatrixAddition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class MatrixAdditionExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};

//creating another matrix to store the sum of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns

//adding and printing addition of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j]; //use - for subtraction
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}}
36 changes: 36 additions & 0 deletions java/sorting/matrixSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.*;

public class Main {
public static void main(String[] args)
{
// Initialize the 2D vector with some values
List<List<Integer> > v
= new ArrayList<>(Arrays.asList(
new ArrayList<>(Arrays.asList(5, 4, 7)),
new ArrayList<>(Arrays.asList(1, 3, 8)),
new ArrayList<>(Arrays.asList(2, 9, 6))));

int n = v.size();
List<Integer> x = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x.add(v.get(i).get(j));
}
}
Collections.sort(x);
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
v.get(i).set(j, x.get(k++));
}
}

System.out.println("Sorted Matrix Will be:");
for (List<Integer> row : v) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}