Results 1 to 4 of 4
  1. #1
    darkwolf1345 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default 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 = (x-1);
    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][x-1];
    else {
    if ( y == (k-1) ) b[y][x] = b[y-1][x-1];
    else b[y][x] = b[y-1][x-1] + b[y+1][x-1];
    }
    }
    }
    }
    for ( x = j ; x >= 0 ; x-- ) {
    for ( y = 0 ; y <= k ; y++ ) {
    if ( y == (k-1) ) {
    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; 11-15-2012 at 05:41 PM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,443
    Rep Power
    18

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

  3. #3
    darkwolf1345 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Error in inverted Pascal triangle program

    I fixed it, thanks a lot (I knew arrays go from 0 to lenght-1, but you helped me remind it xD )

    I didnt understand "
    Java Code:
     tags
    ", can you explain it some other way? because I dont want to botter anyone else with that, and I was hoping you could help me fix the spaces, because when the user selects a number of lines that contain numbers with more than one digit, the spaces go all wrong... and for you to help me you need to read the code xDDD

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Error in inverted Pascal triangle program

    Guide For New Members

    Please see that link for info on using code tags.

Similar Threads

  1. pascal's triangle like problem
    By kulangotski in forum New To Java
    Replies: 4
    Last Post: 12-05-2010, 08:15 AM
  2. a question about pascal triangle
    By rambo126 in forum New To Java
    Replies: 7
    Last Post: 10-30-2010, 07:17 PM
  3. Inverted Right Triangle help
    By aspire007 in forum New To Java
    Replies: 6
    Last Post: 07-30-2010, 06:10 PM
  4. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •