-
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.
Naive approch to find transpose of matrix
- Loading branch information
1 parent
eb6fbf4
commit 3ba0abd
Showing
1 changed file
with
40 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,40 @@ | ||
package matrix; | ||
|
||
import java.util.Scanner; | ||
// Naive approch to transpose the matrix | ||
public class TransposeMtr { | ||
static void transpose(int arr[][]){ | ||
int n = arr.length; | ||
int m = arr.length; | ||
int temp[][] = new int[n][m]; | ||
for(int i=0;i<n;i++){ | ||
for(int j=0;j<m;j++){ | ||
temp[i][j]=arr[j][i]; | ||
} | ||
} | ||
for(int i=0;i<n;i++){ | ||
for(int j=0;j<m;j++){ | ||
arr[i][j]=temp[i][j]; | ||
} | ||
} | ||
} | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int R = sc.nextInt(); | ||
int C = sc.nextInt(); | ||
int arr[][] = new int[R][C]; | ||
for(int i=0;i<R; i++){ | ||
for(int j=0;j<C;j++){ | ||
arr[i][j]=sc.nextInt(); | ||
} | ||
} | ||
transpose(arr); | ||
for(int i=0;i<R;i++){ | ||
for(int j=0;j<C;j++){ | ||
System.out.print(arr[i][j]+" "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} | ||
|