Results 1 to 16 of 16
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default Pascal Triangle noob help

    Alright, I have been working on this for so long and am now lost in this assignment. I've been working on it for a while so can you guys please help me with the loop to complete this?

    Here is the main
    Java Code:
    import java.util.Scanner;
    
    public class PascalDriver{
      public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of rows");
        int rowCount = scan.nextInt();
        PascalTri t = new PascalTri(rowCount);
        t.displayRows();
      }
    }

    Here is the class, I get totally confused on displayrows

    Java Code:
    public class PascalTri {
        int rowCount;
        int cnt = 0;
        public PascalTri(int n) {
        rowCount = n;   
        
        }
        
        public void displayRows() {
           int whatever=0;
           int bbb;
           int previous[] = new int[1000];
           for(int j = 0; j < 1000; j++)
           previous[j] = -1;
    
         previous[0]=1;
         int current[] = new int[previous.length];
         for(int j = 0; j < 1000; j++)
           current[j] = -1;
         System.out.println("----------------------");
         if(isdone()==false) {
          
          System.out.println(previous[0]);
          cnt++;
         } 
         if(isdone()== false) {
             System.out.println(previous[0]+"1");
             cnt++;
         }
         current[0] = 1;
         current[1] = 1;
         while(isdone()==false) {
         System.out.print("1");
             current[2] = current[1]+current[0];
             System.out.print(current[2] + "1");
              for(int ldm = 0; ldm<current.length; ldm++) {
              if(current[ldm]>-1) {
              int j = current[ldm];
              int forprevious = j-1;
                  whatever=j;
                  for(bbb=1; bbb<j; bbb++) {
                  current[0]=current[0] + 1;
                  current[bbb]=current[bbb]+current[forprevious];
                  forprevious++;
                  
                                           }
                  
                      
                    }
              
                 
             }
          //   System.out.print(current[whatever]+1);
          //       System.out.print("1");
             System.out.println("");
             cnt++;
          }
        }
        
        public boolean isdone() {
            
            boolean d = false;
            if(cnt==rowCount) {
            d =true;
            } return d;
        }
        
        
        
        
        
        
    }
    Here is what it happens when I run it


    Enter number of rows
    6
    ----------------------
    1
    11
    121
    141
    1141
    1311

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    11

    Default

    i'm really surprised no one has taken a crack at this yet. I had alot of fun messing w/ the pascal triangles.

    don't really understand your math in "displayRows" method. this is how i've written mine:

    I used a ragged array.
    set array[0][0] = 1;
    set array[*][first] = 1;
    set array[*][last] = 1;
    then just calculate for middle values based on the previous line's values.

    such as:
    n = current line (starts from 1)
    array[n][index] = array[n-1][index] + array[n-1][index-1]

    and so on....
    how for me to explain, you getting it?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Thanks for the response but I am still heavily confused I changed it to this
    Java Code:
    public void displayRows() {
           int whatever=0;
           int bbb;
           int previous[] = new int[1000];
           for(int j = 0; j < 1000; j++)
           previous[j] = -1;
           int counter=0;
           int drone;
          
    
        previous[0]=1;
         int current[] = new int[previous.length+1];
         for(int j = 0; j < 1000; j++)
           current[j] = -1;
         
         current[0]= 1;
         current[1]=1;
         System.out.println("----------------------");
       if(isdone()==false) {
             System.out.println(current[0]);
             cnt++;
       }
         if(isdone()==false) {
             System.out.print(current[1]);
             System.out.print(current[1]);
             cnt++;
         }
         
         while(isdone()==false) {
             previous=current;
                for(int ldm = 0; ldm<current.length; ldm++) {
                     if(current[ldm]>-1) {
                     int j =ldm;
                     counter=j;
                    
              }             
                  
           current = new int[current.length+1];
           current[0]=1;
           for(int i=1; i<current.length; i++) {
             current[i]=previous[i]+previous[i-1];
             System.out.print(current[i]);
           }
           
             cnt++;
         }
    But when I run it I get this

    Java Code:
    run:
    Enter number of rows
    3
    ----------------------
    1
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1001
            at PascalTri.displayRows(PascalTri.java:50)
            at PascalDriver.main(PascalDriver.java:9)
    1120-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-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2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-1Java Result: 1
    BUILD SUCCESSFUL (total time: 9 seconds)
    It points to here current[i]=previous[i]+previous[i-1];

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    11

    Default

    previous has 1000 elements
    current has 1002 elements

    why you redeclare current in while loop and not do the same thing for previous ?

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    11

    Default

    I don't think you are using the correct math to do this. maybe you should scrap the whole function and start over with a different appoach.

    what you should know:
    first line is a 1,
    second line is 1 1,
    third line is 1 X 1,
    where x = previousLine[n-1] + previousLine[n];
    and so on...

    mtyoung, welcome back. I've out posted you. hehe
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    But I thought my math function does the same thing? I am brain stumped:(

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    11

    Default

    i can't tell if your math is right or wrong, but its a mess.
    the first: if(isdone()==false) prints a 1.
    the second: if(isdone()==false) prints two 1's.
    i'll let you pass on that.

    then onto your while loop:
    Java Code:
    current = new int[current.length+1];
    current[0]=1;
    for(int i=1; i<current.length; i++) {
                   current[i]=previous[i]+previous[i-1];
                   System.out.print(current[i]);
                }
    now thats just crazy. i<current.length.
    BUT current.length is previous.length + 1
    which is 1001. then you used print on it. which prints out
    1001 int values!!!. that explains why you are getting -2-2-2-2-2-2-2-2-2-2-... and so on.

    also, before going into the for looop, you are changing the current array. why?? try to find a different idea to calculate for the middle values.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    So I changed it up a bit, man I get confused in my own code so much, I guess I am learning that lesson the hard way:(

    Java Code:
      while(isdone()==false) {
             previous=current;
                for(int ldm = 0; ldm<current.length; ldm++) {
                     if(current[ldm]>-1) {
                     int j =ldm;
                     counter=j;
    
              }
    
          // current = new int[current.length+1];
           current[0]=1;
           for(int i=0; i<ldm; i++) {
             current[i]=previous[i]+previous[i-1];
             System.out.print(current[i]);
             previous=current;
           }
    When I run it it gets out of bounds
    at the line current[i]=previous[i]+previous[i-1];

    but if I just change that to previous[i] the program does nothing? Just eats up cpu:confused:

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    11

    Default

    1) what is ldm??
    2) you have previous=current; two times, one at top, second at bottom.
    3) trying moving current = new int[current.length+1]; to bottom.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    LDM is the last known position in the array that has info in it. Project is due in 50 minutes, hopefully I can figure it out, oh well you live you learn :(

  11. #11
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Haha right after class ended it came to me I got it! I'll post the code later thank you so much

  12. #12
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    For anyone wanting the code for w/e reason
    Java Code:
    public class PascalTri {
        int rowCount;
        int cnt = 0;
        public PascalTri(int n) {
        rowCount = n;   
        
        }
        
        public void displayRows() {
           int whatever=0;
           int bbb;
           int previous[] = new int[1000];
           for(int j = 0; j < 1000; j++)
           previous[j] = -1;
           int counter=0;
           int drone;
         
        previous[0]=1;
         int current[] = new int[previous.length+1];
         for(int j = 0; j <= 1000; j++)
           current[j] = -1;
         
         current[0]= 1;
         current[1]=1;
         System.out.println("----------------------");
       if(isdone()==false) {
             System.out.println(current[0]);
             cnt++;
             //current[0]=1;
       }
         if(isdone()==false) {
             System.out.print(current[1]);
             System.out.println(current[1]);
             cnt++;
             //current[0]=1;
             //current[1]=1;
         }
         
          while(isdone()==false) {
             previous=current;
                for(int ldm = 0; ldm<current.length; ldm++) {
                     if(current[ldm]>-1) {
                        counter=ldm;
                     }
                }
          current = new int[current.length];
            for(int j = 0; j <= 1000; j++)
              current[j] = -1;
           current[0] = 1;
           System.out.print(current[0]);
           
           
           int g = 1;
         
           for(int i=1; i<=counter; i++) { //It does not go through this for loop! I don't know why EDIT: added <= and one gets printed
                current[i]=previous[i]+previous[i-1]; 
                System.out.print(current[i]);
                //System.out.println("hello"); // just to test if it's working, which it is not'
           }
           current[counter+1]=1;
           System.out.println(1);
           
            /*  while(g<counter-1) { //same loop same problem, probably with counter
                current[g]=previous[g]+previous[g-1]; 
                System.out.print(current[g]);
                System.out.println("hello");
                g++;
           }
          */
             cnt++;
         }
         
         /*
         if(isdone()==false) {
          
          System.out.println(previous[0]);
          cnt++;
         } 
         if(isdone()== false) {
             System.out.println(previous[0]+"1");
             cnt++;
         }
         current[0] = 1;
         current[1] = 1;
         while(isdone()==false) {
         System.out.print("1");
             current[2] = current[1]+current[0];
             System.out.print(current[2] + "1");
              for(int ldm = 0; ldm<current.length; ldm++) {
              if(current[ldm]>-1) {
              int j = current[ldm];
              int forprevious = j-1;
                  whatever=j;
                  for(bbb=1; bbb<j; bbb++) {
                  current[0]=current[0] + 1;
                  current[bbb]=current[bbb]+current[forprevious];
                  forprevious++;
                  
                                           }
                  
                      
                    }
              
                 
             }
          //   System.out.print(current[whatever]+1);
          //       System.out.print("1");
             System.out.println("");
             cnt++;
          }
         */
        }
        
        
        public boolean isdone() {
            
            boolean d = false;
            if(cnt>=rowCount) {
            d =true;
            } return d;
        }
        
        
        
        
        
        
    }
     
    Driver ---
    import java.util.Scanner;
    public class PascalDriver{
      public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of rows");
        int rowCount = scan.nextInt();
        PascalTri t = new PascalTri(rowCount);
        t.displayRows();
      }
    }

  13. #13
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    11

    Default

    dude, i can't believe you did it! I still can't understand your math though...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  14. #14
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    11

    Default my take

    I've decided not to post the complete code, but here's the math portion i've used.

    Java Code:
    for(int line=1; line < max; line++){
       // create individual array
       matrix[line]= new BigInteger[line+1];
             
       // set first
       matrix[line][0] = BigInteger.valueOf(1);
             
       // set mid
       for(int i=1; i<line; i++)
          matrix[line][i] = matrix[line-1][i-1].add( matrix[line-1][i] );
             
       // set last
       matrix[line][matrix[line].length -1 ] = BigInteger.valueOf(1);
    }
    where matrix is a ragged array of type BigInteger.
    if type long is used, it starts to overflow after 67th row.

    XML Code:
    S:\SVN\PascalTriangle>java PascalTriangle
    Enter number of rows (zero-based): 16
    ----------------------
    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    1 5 10 10 5 1
    1 6 15 20 15 6 1
    1 7 21 35 35 21 7 1
    1 8 28 56 70 56 28 8 1
    1 9 36 84 126 126 84 36 9 1
    1 10 45 120 210 252 210 120 45 10 1
    1 11 55 165 330 462 462 330 165 55 11 1
    1 12 66 220 495 792 924 792 495 220 66 12 1
    1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
    1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
    1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
    1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
    If the thread is solved, please mark it as solved.
    Last edited by angryboy; 01-28-2009 at 03:34 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  15. #15
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    11

    Default

    Thought I update this because I've just found a really neat math equation for this:
    Java Code:
        nRow[0] = 1;
        for(int col=1; col<nRow.length; col++){
          nRow[col] = nRow[col-1] * (ROW-col)/col;
        }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  16. #16
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    11

    Default

    What is ROW?
    EDIT: Oh figured it out its the row's length. Thanks for this formula I made my own pascal triangle generator with it :).
    Last edited by MK12; 02-17-2009 at 01:10 AM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. Noob
    By nokomis in forum Introductions
    Replies: 2
    Last Post: 03-06-2009, 06:10 PM
  2. Translating Pascal into java
    By willemjav in forum Advanced Java
    Replies: 120
    Last Post: 08-16-2008, 12:26 AM
  3. PASCAL & JAVA? for senior members
    By willemjav in forum Forum Lobby
    Replies: 2
    Last Post: 07-13-2008, 12:11 PM
  4. Please help a noob :)
    By Bays in forum New To Java
    Replies: 15
    Last Post: 06-17-2008, 07:11 AM
  5. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 08: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
  •