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:
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 ]
Joined: Mar 25, 2004
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
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.
Joined: Aug 02, 2004
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?
Joined: Jun 08, 2004
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 ]
Joined: Mar 25, 2004
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] = 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.
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
Joined: Aug 02, 2004
(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.
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.