# 2D array problem

• December 9th, 2013, 02:03 PM
namenamename
2D array problem
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:
Code :

```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(); } } }```
The problem is that it does nothing for Transpose 2, it somehow copies the same values of Transpose 1. How do I fix this?
• December 9th, 2013, 02:32 PM
Norm
Re: 2D array problem
Please post the program's output and add some comments saying what is wrong and show what the output should be.
• December 9th, 2013, 02:48 PM
namenamename
Re: 2D array problem
Output:
Enter the number of the matrix:
4
Original:
2 9 0 4
2 6 2 5
4 8 6 3
8 8 9 3
Transpose 1:
2 2 4 8
9 6 8 8
0 2 6 9
4 5 3 3
Transpose 2:
3 2 4 8
3 6 8 8
5 2 6 9
4 0 9 2
Press any key to continue . . .

The output should be like this:
Enter the number of the matrix:
4
Original:
2 9 0 4
2 6 2 5
4 8 6 3
8 8 9 3
Transpose 1:
2 2 4 8
9 6 8 8
0 2 6 9
4 5 3 3
Transpose 2:
3 3 5 4
9 6 2 0
8 8 6 9
8 4 2 2
• December 9th, 2013, 02:58 PM
Norm
Re: 2D array problem
Can you explain what is wrong with the output?

A suggestion for testing. Make the changes you see here on the three lines with comments:
Code :

``` int n = 4; // input.nextInt(); int nbr = 0; // to generate contents a = new int [n][n]; for (i=0;i<n;i++){ for (j=0;j<n;j++){ a[i][j] = nbr++; //(int)(Math.random()*10); // sequential nbrs```
• December 9th, 2013, 07:32 PM
namenamename
Re: 2D array problem
Quote:

Originally Posted by Norm
Can you explain what is wrong with the output?

A suggestion for testing. Make the changes you see here on three lines:
Code :

``` int n = 4; // input.nextInt(); int nbr = 0; // to generate contents a = new int [n][n]; for (i=0;i<n;i++){ for (j=0;j<n;j++){ a[i][j] = nbr++; //(int)(Math.random()*10); // sequential nbrs```

You misunderstand me. Look at this original set of numbers:
4 5 2 0
7 2 1 4
9 4 2 0
7 8 9 3

Transpose 1:
2 2 4 8
9 6 8 8
0 2 6 9
4 5 3 3

You simply switch 2 and 9, 4 and 0, and so on..

Transpose 2 (Here is where I'm stuck at):
3 0 4 0
9 2 1 2
8 4 2 5
7 9 7 4

This time you switch 2 and 4, 5 and 0 and so on...
• December 9th, 2013, 07:46 PM
Norm
Re: 2D array problem
For testing it will be easier to talk about the elements in the array if they were all unique. The code I posted in post#4 will generate all unique numbers.

To see what the code is doing add a println statement inside the inner for loop that prints out all the values involved: both indexes for the source element, the content of the source element, both indexes for the target element and the content of the target element. Seeing those 6 values will help you understand what the code is doing.
• December 9th, 2013, 08:11 PM
namenamename
Re: 2D array problem
Quote:

Originally Posted by Norm
Can you explain what is wrong with the output?

A suggestion for testing. Make the changes you see here on the three lines with comments:
Code :

``` int n = 4; // input.nextInt(); int nbr = 0; // to generate contents a = new int [n][n]; for (i=0;i<n;i++){ for (j=0;j<n;j++){ a[i][j] = nbr++; //(int)(Math.random()*10); // sequential nbrs```

Can you tell me what "nbr" stand for?
• December 9th, 2013, 08:26 PM
Norm
Re: 2D array problem
nbr stands for number. It is used to generate numbers for the array from 0 to n*n-1