Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Printing multi-dimesonial arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Printing multi-dimesonial arrays" Watch "Printing multi-dimesonial arrays" New topic
Author

Printing multi-dimesonial arrays

Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
I am trying to print a 2d array as a right-angle triangle e.g

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Pretty hard when i dont even know how to print one as a rectangle i am trying to do it this way I am completely lost on this i have been staring at it for a good hour and nothing has come to me the code that i have got only prints the first line:



Any help would be greatly apecaited, ty
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
It's not just printing the first line -- it's printing everything on one line. Think about when you want a new line to be printed. After a single row is printed, right? If you have a 5x5 array, you're going to print a new line only 5 times... think about it.
[ August 03, 2004: Message edited by: Darin Niard ]
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
So if i wanted a square of 5 by 5

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

I would have to use a counter until it reaches 5 or n, but the problem i have now is that i cant see how to make it come down to the next line, by using println its now going

0
0
0
0
0
0
etc.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Peter,

Darin is right. Your code as given, assuming you declare "int n = 5;" will print a row of 25 zeroes, i.e. the default values for each of the elements of a 5x5 two-dimensional array.

To achieve your goal of the right-angled triangle you need to consider the following:

  • where to put the newline, i.e. System.out.println(); - this will give you a square of zeroes
  • How to initialize your array so it only contains the numbers you want for your triangle, i.e. first row has 1 column, second two, and so on
  • How to modify your loop conditions to avoid exceeding the array bounds
  • Where to put the single space that you want after each number to achieve the format in your example


  • These are fairly minor changes to your code, so you can take some comfort in the knowledge that you're on the right track.

    Hope this helps.

    Regards

    Jules
    Julian Kennedy
    Ranch Hand

    Joined: Aug 02, 2004
    Posts: 823
    You need to spend a little more time experimenting. You're on the right track. What do you think you need to do to make the newline appear after every fifth zero, instead of after every one?

    Jules
    Darin Niard
    Ranch Hand

    Joined: Jun 08, 2004
    Posts: 118
    Originally posted by Peter Shipway:
    I would have to use a counter until it reaches 5 or n

    Hint: You already have a counter, there's no reason to add another.
    0
    0
    0
    0
    0
    0
    etc.

    Notice how I pointed out that there will only be 5 new lines, not 25. So, printing a new line with each value is not what you want to do.
    [ August 03, 2004: Message edited by: Darin Niard ]
    Peter Shipway
    Ranch Hand

    Joined: Mar 25, 2004
    Posts: 71
    Got It :>



    Now for a completely differnt problem thou, (this is a step by step problem i am getting out of a book) Its to create a Pascals Triangle it quotes the following

    (Don't worry about lining numbers up.) The rule for constructing the elements of this triangle t is as follows:
    t[n][0] = 1, n >= 0
    t[n][k] = t[n-1][k-1] + t[n-1][k], n >= 1, 1 <= k < n
    t[n][n] = 1, n >= 1

    Note that t[n][k] is the binomial coefficient C(n,k). What is the the value of C(20,10)?


    I dont understand if the statments after the , is an if statment because on the second line its well screwed up if anyone has a good link to how to construct pascals triangles (all i have been able to find so far is how to destruct) i would greatly appreciate it.

    Ty for all the help so far guys, if it wasnt for you all I would have given up for sure

    Oh and i forgot to mention its only the right angle part of the triangle eg.

    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    1 5 10 10 5 1
    1 6 15 20 15 6 1

    [ August 04, 2004: Message edited by: Peter Shipway ]
    [ August 04, 2004: Message edited by: Peter Shipway ]
    Julian Kennedy
    Ranch Hand

    Joined: Aug 02, 2004
    Posts: 823
    Well done, Peter. Credit where it's due.

    Your solution relies on the array being pre-populated as follows:

    What if you were to populate it as shown below? It's more memory-efficient, removing all those redundant zeroes...

    How would you modify your code to avoid an ArrayIndexOutOfBoundsException at runtime? It's a very small adjustment and removes the need for your "if" statement.

    It looks like the main trick for your second assignment is to populate the data with the right values programmatically. The code for printing them out can be identical to what you have now.

    Regards

    Jules
    Julian Kennedy
    Ranch Hand

    Joined: Aug 02, 2004
    Posts: 823

    t[n][0] = 1, n >= 0
    t[n][k] = t[n-1][k-1] + t[n-1][k], n >= 1, 1 <= k < n
    t[n][n] = 1, n >= 1

    The above is a very mathematical way of stating the problem and I can see why you find it confusing. Let me try and clarify:

    I know it's not code, but there's no other obvious way of preserving layout.

    How's that? Clear as mud? Well, don't give up too easily; it looks a bit daunting but it's really not that tricky.

    Jules
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11480
        
      16

    here's another way of explaing pascals triangle...

    the first and last element of each row is 1. there is no exception to this rule.

    to get any other value, find it's position in it's row, call it k. Then, find the two values in the row above at positions k-1 and k, and add them together.



    to get the value at the question mark, we need to find it's position...well, it's position 2 (remember, we start at 0). And, it's in row 5. so, i need to look at row 4, and find the values at 1 and 2. these happen to both be 3. so, the question mark should be 6.

    now, you're supposed to write it so it tackles the problem in an orderly fassion. maybe one loop for keeping track of what row you're on, and another for keeping track of which element in the row you're on.

    here's another hint: your counter for which row you are on can start at 1 if you want. then, each row will have the same number of elements as the row it is... i.e. row 1 will have 1 element, row 2 will have 2 elements, etc. maybe you can use this to your advantage.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Julian Kennedy
    Ranch Hand

    Joined: Aug 02, 2004
    Posts: 823
    Fred said:
    (remember, we start at 0). And, it's in row 5.

    So it's actually in row 4 and we need to look at row 3 to calculate the value. I'm sure you all spotted the deliberate mistake. Fred's last suggestion might prevent this sort of confusion, but it may be better to practice getting used to the fact that Java's array indexes start at zero.

    Jules
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11480
        
      16

    errr... yeah... deliberate mistake... yeah that's what it was...

    ok, actually, i forgot we're storing the whole thing in a 2d array - i was just thinking about printing it out on the fly...

    but yes, since we need to store everything in the array, it would be best to think of the first row as the 0'th.

    sorry...
    Max Rahder
    Ranch Hand

    Joined: Nov 06, 2000
    Posts: 177

    [ August 05, 2004: Message edited by: Max Rahder ]
    Julian Kennedy
    Ranch Hand

    Joined: Aug 02, 2004
    Posts: 823
    Very neat, Max. Bet you can't make it produce Fred's pretty pattern!

    Jules
    Tony Morris
    Ranch Hand

    Joined: Sep 24, 2003
    Posts: 1608
    Your problem may be a result of the disillusion that Java supports multi-dimensional arrays. Often, a student resolves an issue once belief of this fallacy has been rectified.

    http://www.xdweb.net/~dibblego/java/faq/answers.html#q45


    Tony Morris
    Java Q&A (FAQ, Trivia)
    Julian Kennedy
    Ranch Hand

    Joined: Aug 02, 2004
    Posts: 823
    Hi Tony,

    Personally I understand the difference. I think your article does a good job of describing how arrays are implemented in Java, but doesn't clearly say how a two-dimensional array is different, e.g. that all rows have the same number of columns, to use a metaphor.

    Perhaps you could elaborate?

    Jules
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Printing multi-dimesonial arrays