....................1
.................1....1
..............1....2....1
...........1....3.....3...1
and so on to nth index.
In my snippet given below, it is printing good for 2 and 7 and giving errors for any other values. And all errors occur due to index out of bound.
public class Pascal
{
public static void main(int n)
{
int[ ] [ ] grid = new int [ n ] [ n + ( n - 1 ) ];
int help = 6;
int as = 1;
for ( int i = 0; i < n; i ++ )
{
int insertOne = n - ( i + 1 );
grid [ i ] [ insertOne ] = 1;
int inOne = n + i;
grid [ i ] [ inOne - 1 ] = 1;
}
for ( int i = 2; i < n; i ++ )
{
for ( int j = 1; j < i; j ++ )
{
grid [ i ] [ help ] = grid [ i - 1 ] [ help - 1 ] + grid [ i - 1 ] [help + 1 ]; //I think here is the problem
if ( i > 2 && j < ( i - 1))
{
help += 2;
}
}
help -= as;
as += 2;
}
for(int i = 0; i < n; i ++ )
{
for ( int j = 0; j < (n + ( n -1 )); j ++ )
{
if(grid[i][j] == 0 )
{
System.out.print(" ");
}
else
{
if(grid[i][j] < 10 )
System.out.print(" "+grid[i][j]);
else
System.out.print(grid[i][j]);
}
}
System.out.println();
}
}
}

New Topic/Question
Reply



MultiQuote





|