Results 1 to 4 of 4
 11152012, 06:32 PM #1Member
 Join Date
 Nov 2012
 Posts
 2
 Rep Power
 0
Error in inverted Pascal triangle program
Hi everyone. I donīt know why the following error appears when I try to execute my program, so if anyone can help me please... xD
Source code:
import java.util.*;
public class pascal2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = 0;
do {
System.out.print("How many values (1...13) ?");
x = in.nextInt();
} while ( x<1  x>13 );
int y = (x*2)1;
int [] a = new int [y];
int [][] b = new int [y][x];
int j = (x1);
int k = y;
for ( x = 0 ; x <= j ; x++ ) {
for ( y = 0 ; y < k ; y++ ) {
if ( x == 0 ) {
if ( y == (((k+1)/2)1) ) b[y][x] = 1;
else b[y][x] = 0;
}
else {
if ( y == 0 ) b[y][x] = b[y+1][x1];
else {
if ( y == (k1) ) b[y][x] = b[y1][x1];
else b[y][x] = b[y1][x1] + b[y+1][x1];
}
}
}
}
for ( x = j ; x >= 0 ; x ) {
for ( y = 0 ; y <= k ; y++ ) {
if ( y == (k1) ) {
if ( b[y][x] == 0 ) System.out.println(" ");
else System.out.println( b[y][x] );
}
else {
if ( b[y][x] == 0 ) System.out.print(" "); < line 43
else System.out.print( b[y][x] );
}
}
}
}
}
Error in cmd:
D:\>java pascal2
How many values (1...13) ?7
1 6 15 20 15 6 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at pascal2.main(pascal2.java:43)Last edited by darkwolf1345; 11152012 at 06:41 PM.
 11152012, 07:06 PM #2Moderator
 Join Date
 Apr 2009
 Posts
 13,222
 Rep Power
 23
Re: Error in inverted Pascal triangle program
Please use [code] tags [/code] when posting code.
I'm guessing here (as I don't read unformatted code), but you are accessing someArray[13] where someArray is only 13 elements long.
Arrays go from 0 to (arrayLength  1).Please do not ask for code as refusal often offends.
** This space for rent **
 11152012, 07:27 PM #3Member
 Join Date
 Nov 2012
 Posts
 2
 Rep Power
 0
Re: Error in inverted Pascal triangle program
I fixed it, thanks a lot (I knew arrays go from 0 to lenght1, but you helped me remind it xD )
I didnt understand "Java Code:tags
 11152012, 08:28 PM #4
Re: Error in inverted Pascal triangle program
Guide For New Members
Please see that link for info on using code tags.
Similar Threads

pascal's triangle like problem
By kulangotski in forum New To JavaReplies: 4Last Post: 12052010, 09:15 AM 
a question about pascal triangle
By rambo126 in forum New To JavaReplies: 7Last Post: 10302010, 07:17 PM 
Inverted Right Triangle help
By aspire007 in forum New To JavaReplies: 6Last Post: 07302010, 06:10 PM 
Pascal Triangle help
By Magic101 in forum New To JavaReplies: 4Last Post: 05012008, 07:51 PM
Bookmarks