Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: 2D array problem

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
    		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
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D array problem

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

    A suggestion for testing. Make the changes you see here on three lines:
    		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...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D array problem

    Quote Originally Posted by Norm View Post
    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:
    		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?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 2D array problem

    nbr stands for number. It is used to generate numbers for the array from 0 to n*n-1
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  2. array problem
    By u-will-neva-no in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 11:36 AM
  3. [SOLVED] Array Problem
    By KevinGreen in forum Object Oriented Programming
    Replies: 2
    Last Post: January 24th, 2010, 09:07 PM
  4. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM