What the program is supposed to do:
Write a program which builds two dimensional array ( n x n matrix ) with the dimentions entered by the user, populates the array with random numbers from 0 to 9, builds 2 transpose matrixes (using both diagonals), and displays Them all.
For example,
Enter a matrix size: 4
Original:
4 5 2 0
7 2 1 4
9 4 2 0
7 8 9 3
Transpose 1:
4 7 9 7
5 2 4 8
2 1 2 9
0 4 0 3
Transpose 2:
3 0 4 0
9 2 1 2
8 4 2 5
7 9 7 4
My code so far:
The problem is that it does nothing for Transpose 2, it somehow copies the same values of Transpose 1. How do I fix this?import java.util.Scanner; class Ex2DArray { static int i,j,n,k; static int a[][]; public static void main (String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter the number of the matrix:"); int n = input.nextInt(); a = new int [n][n]; for (i=0;i<n;i++){ for (j=0;j<n;j++){ a[i][j] = (int)(Math.random()*10); } } System.out.println("Original:"); //printArray(); for (i=0;i<n;i++){ for (j=0;j<n;j++){ System.out.print(a[i][j]+" "); } System.out.println(); } for (i=0;i<n;i++){ for (j=0;j<i;j++){ k = a[j][i]; a[j][i] = a[i][j]; a[i][j] = k; } } System.out.println("Transpose 1:"); //printArray(); for (i=0;i<n;i++){ for (j=0;j<n;j++){ System.out.print(a[i][j]+" "); } System.out.println(); } for (i=0;i<n;i++){ for (j=1;j<n;j++){ k = a[n-j-1][n-i-1]; a[n-j-1][n-i-1] = a[i][j]; a[i][j] = k; } } System.out.println("Transpose 2:"); //printArray(); for (i=0;i<n;i++){ for (j=0;j<n;j++){ System.out.print(a[i][j]+" "); } System.out.println(); } } public static void printArray() { for (i=0;i<n;i++){ for (j=0;j<n;j++){ System.out.print(a[i][j]+" "); } System.out.println(); } } }