-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06f2bbe
commit 3658662
Showing
3 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import java.util.Scanner; | ||
|
||
public class MatrixMultiplication { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
int n1 = scanner.nextInt(); | ||
int m1 = scanner.nextInt(); | ||
|
||
int [][] a1 = new int [n1][m1]; | ||
|
||
for(int i =0; i<n1; i++) | ||
{ | ||
for(int j =0; j<m1; j++) | ||
{ | ||
a1[i][j]= scanner.nextInt(); | ||
} | ||
} | ||
|
||
int n2 = scanner.nextInt(); | ||
int m2 = scanner.nextInt(); | ||
|
||
int [][] a2 = new int [n2][m2]; | ||
|
||
for(int i =0; i<n2; i++) | ||
{ | ||
for(int j =0; j<m2; j++) | ||
{ | ||
a2[i][j]= scanner.nextInt(); | ||
} | ||
} | ||
|
||
if(m1==n2) | ||
{ | ||
int [][] num = new int [n1][m2]; | ||
int sum; | ||
for(int i =0; i<n1; i++) | ||
{ | ||
for(int j =0; j<m2; j++) | ||
{ | ||
sum=0; | ||
for(int k=0; k<m1; k++) | ||
{ | ||
sum+= a1[i][k]* a2[k][j]; | ||
} | ||
|
||
num[i][j]= sum; | ||
} | ||
} | ||
|
||
|
||
// System.out.println("printing array"); | ||
for(int i =0; i<n1; i++) | ||
{ | ||
for(int j =0; j<m2; j++) | ||
{ | ||
System.out.print(num[i][j]+" "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
else | ||
{ | ||
System.out.println("Invalid input"); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import java.util.Scanner; | ||
|
||
public class MatrixSpiralTraversal { | ||
|
||
public static void main(String [] args) | ||
{ | ||
Scanner scanner = new Scanner(System.in); | ||
int n = scanner.nextInt(); | ||
int m = scanner.nextInt(); | ||
|
||
int [][] arr = new int [n][m]; | ||
|
||
for(int i =0; i<n; i++) | ||
{ | ||
for(int j =0; j<m; j++) | ||
{ | ||
arr[i][j]= scanner.nextInt(); | ||
} | ||
} | ||
|
||
System.out.println("PRINTING ARRAY"); | ||
int leastRow = 0; | ||
int leastCol =0; | ||
int maxRow = n-1; | ||
int maxCol = m-1; | ||
int entries = n*m; | ||
int count =0; | ||
|
||
while(count<entries) | ||
{ | ||
//Print left column | ||
for(int i =leastRow; i<=maxRow && count<entries; i++)// 0-4 | ||
{ | ||
System.out.println(arr[i][leastCol]); | ||
count++; | ||
} | ||
leastCol++; | ||
|
||
//Print bottom row | ||
for(int i =leastCol; i<=maxCol && count<entries ; i++) | ||
{ | ||
System.out.println(arr[maxRow][i]); | ||
count++; | ||
} | ||
maxRow--; | ||
|
||
//Print right column | ||
for(int i =maxRow; i>=leastRow && count<entries; i--) | ||
{ | ||
System.out.println(arr[i][maxCol]); | ||
count++; | ||
} | ||
maxCol--; | ||
|
||
//Print top row | ||
for(int i =maxCol; i>=leastCol && count<entries ; i--) | ||
{ | ||
System.out.println(arr[leastRow][i]); | ||
count++; | ||
} | ||
leastRow++; | ||
|
||
// System.out.println("count after box is: "+count); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.util.Scanner; | ||
|
||
public class MatrixWaveTraversal { | ||
public static void main(String [] args) | ||
{ | ||
Scanner scanner = new Scanner(System.in); | ||
int n = scanner.nextInt(); | ||
int m = scanner.nextInt(); | ||
|
||
int [][] arr = new int [n][m]; | ||
|
||
for(int i =0; i<n; i++) | ||
{ | ||
for(int j =0; j<m; j++) | ||
{ | ||
arr[i][j]= scanner.nextInt(); | ||
} | ||
} | ||
|
||
// System.out.println("PRINTING ARRAY"); | ||
int j =0; | ||
for(int i =0; i<m ; i++) | ||
{ | ||
|
||
if(i%2!=0) | ||
{ | ||
while(j>0) | ||
{ | ||
System.out.println(arr[--j][i]); | ||
} | ||
} | ||
else | ||
{ | ||
while(j<n) | ||
{ | ||
System.out.println(arr[j++][i]); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |