5 Replies - 1967 Views - Last Post: 25 April 2009 - 06:17 PM Rate Topic: -----

#1 rishabhsharma   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 342
  • Joined: 26-March 09

Pascal Triangle Problem

Post icon  Posted 23 April 2009 - 07:51 AM

I was programming to compute a pascal triangle which looks like

....................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();
						}
					}
				}



Is This A Good Question/Topic? 0
  • +

Replies To: Pascal Triangle Problem

#2 rishabhsharma   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 342
  • Joined: 26-March 09

Re: Pascal Triangle Problem

Posted 23 April 2009 - 08:00 AM

Thanks friend but I have solved my problem. Thanks for viewing my problem

here is the renewed code

public class Pascal
	{
		public static void main(int n)
			{
				int[ ] [ ] grid = new int [ n ] [ n + ( n - 1 ) ];
				int help = n-1; //here was the problem(I was taking each time 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 ];
								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();
						}
					}
				}


Was This Post Helpful? 0
  • +
  • -

#3 W3B5T4R   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 13-April 09

Re: Pascal Triangle Problem

Posted 23 April 2009 - 04:02 PM

I think you might have made it a little harder than it is suppose to be.
Try this:

1. int[ ] [ ] grid = new int [ n ] [];
2. for row = 0; row < n ; row++
3. Set grid at row to a new int at row + 1
Example: grid[row] = new int[row+1];
4. Set grid row at row and column at 0 to 1
5. Set grid row at row and column at row to 1
5a. for column = 1; column < row ; column++
5b. Set grid row at row and column at column to grid row -1
and column at column + grid at row - 1 and column at column -
1
6. for r = 0; r < n ; r++
6a. for c= 1; c < row ; c++
6b. Same as 5b (sub r for row & c for column).
7. for r= 0; r < the length of grid ; r++
7a. for c = 0; c < the length of grid at r ; c++
7b. Print triangle.

I hope this helps
Was This Post Helpful? 0
  • +
  • -

#4 rishabhsharma   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 342
  • Joined: 26-March 09

Re: Pascal Triangle Problem

Posted 25 April 2009 - 12:39 AM

View PostW3B5T4R, on 23 Apr, 2009 - 03:02 PM, said:

I think you might have made it a little harder than it is suppose to be.
Try this:

1. int[ ] [ ] grid = new int [ n ] [];
2. for row = 0; row < n ; row++
3. Set grid at row to a new int at row + 1
Example: grid[row] = new int[row+1];
4. Set grid row at row and column at 0 to 1
5. Set grid row at row and column at row to 1
5a. for column = 1; column < row ; column++
5b. Set grid row at row and column at column to grid row -1
and column at column + grid at row - 1 and column at column -
1
6. for r = 0; r < n ; r++
6a. for c= 1; c < row ; c++
6b. Same as 5b (sub r for row & c for column).
7. for r= 0; r < the length of grid ; r++
7a. for c = 0; c < the length of grid at r ; c++
7b. Print triangle.

I hope this helps


Can't you give the complete code of Pascal if you have. I mean its very difficult to speculate.
Was This Post Helpful? 0
  • +
  • -

#5 rishabhsharma   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 342
  • Joined: 26-March 09

Re: Pascal Triangle Problem

Posted 25 April 2009 - 01:06 AM

public class PascalTriangle {

public static void main(int n) {

		int [][] tri = new int[n][];

		for (int r=0; r<tri.length; r++) {
				tri[r] = new int[r+1];
				tri[r][0] = 1;
				tri[r][r] = 1;
				for (int c=1; c<r; c++) {
						tri[r][c] = tri[r-1][c]+tri[r-1][c-1];
						}
				}

		for (int r=0; r<tri.length; r++) {
				for (int c=0; c<tri[r].length; c++) {
						System.out.print("	   "+tri[r][c]);
				}
				System.out.println("");
				}
		}

}




I think you mean this. I have not used this technique 'cos it's very common and very odd..............I wanted to test my own prudence so, therefore I innovated that program........and its working very smoothly and it prints in the shape of triangle but your program did not print the triangle.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Pascal Triangle Problem

Posted 25 April 2009 - 06:17 PM

Just search this forum for Pascal you'll have all answers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1