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

[ August 03, 2004: Message edited by: Darin Niard ]

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:

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

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 ]

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 ]

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

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

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

**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

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...

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

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

Tony Morris

Java Q&A (FAQ, Trivia)

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

It is sorta covered in the JavaRanch Style Guide. |