Results 1 to 6 of 6
  1. #1
    BlueBird is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default Print Left Diagonal

    I've been trying to print the left diagonal of a matrix. But my inner j loop seems to be stuck at the value of (n-1) in my debugger.

    System.out.println("\nLeft Diagonal:");
    for(int i=0;i<m;i++)
    {
    for(int j=n-1;j>=0;j--)
    {
    System.out.print(x[i][j]+" ");
    break;
    }
    }

    If someone can tell me what's wrong it'd be great.
    Last edited by BlueBird; 02-05-2012 at 01:00 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Print Left Diagonal

    What is a 'left diagonal' of a matrix? Does the matrix need to be a square matrix? Do you mean a lower/upper triangular matrix?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Print Left Diagonal

    What is the value of n? Does it allow the loop to iterate?

  4. #4
    BlueBird is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default Re: Print Left Diagonal

    For example my matrix is say,
    1 2 3
    4 5 6
    7 8 9
    I want to print "3 5 7" which is the left diagonal of the matrix. Yes, the matrix has to be a square matrix.
    m is the number of rows while n is the number of columns. Since the left diagonal has the positions "02 11 20" in a 3 into 3 matrix i was trying to get the
    loop controlling the row index(outer i loop) to go on increasing from 0 to m-1 and the loop controlling the column index(inner j loop) to go on decreasing
    from n-1 to 0. And since its a square matrix the value of n and m will be equal. Sorry for the ambiguity.
    Last edited by BlueBird; 02-06-2012 at 10:29 AM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Print Left Diagonal

    Because your matrix is square we can forget about m (it equals n); for an element in row i, we have to print M[ i ][ m-1-i ] (check this).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    BlueBird is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default Re: Print Left Diagonal

    Oooh. Yess. :) Got that. Thanks a lot.

Similar Threads

  1. Reversing dojo slider to make it slide from right to left rather left to right as giv
    By gurpreet.singh in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-05-2011, 01:49 PM
  2. Multiple keys pressed, diagonal movement
    By AndroidAppNewbie in forum New To Java
    Replies: 2
    Last Post: 03-02-2011, 09:30 AM
  3. square moves left and down but not up or left
    By natdizzle in forum AWT / Swing
    Replies: 3
    Last Post: 02-04-2011, 05:20 PM
  4. mp5 left!!.. anyOne know it?.
    By jhon123 in forum New To Java
    Replies: 0
    Last Post: 01-31-2011, 05:50 AM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •