Jump to content

Java Help


DrAwesomePhD
 Share

Recommended Posts

Hi. I was just working on a java program (pascals triangle) and my theory was that i wanted to create a new array for every row of the triangle. So, since there is a keyboard input for how long the triangle will be in rows... i was going to create a for loop that went from 0 to n (n being the number specified by user) and WANTED to name the arrays like: array0, array1, array2... arrayn. However, im not sure how to declare the name of the array in the for loop so that it recognizes that im naming the array based on the number of the for loop, if that makes any sense... heres my "code"

 

for(int i = 0; i < n; i++){
int[] arrayi = new int[i]; 
...}

 

but as you can see, thats just calling the array 'arrayi' instead of having the name of the array dynamic so it would change for every operation of the loop... does anyone know how to make it so the array DID have a different name for every time the loop was run -- thereby enabling me to have a different array for every row of the triangle...

Share this post


Link to post
Share on other sites

can you give me some code help for that? i cant quite figure it out >.< still new...

 

int[] array = new int[n]

for(int j = 0; j < n; j++){
   array[j] = new .... .name the array?}

 

how does that work >.<

 

or for lego's suggestion...

 

  for(int k = 0; k < n; k++){
  int[] "array" + k = new int[n]; 
 }

 

is that right?

Edited by DrAwesomePhD

Share this post


Link to post
Share on other sites

You know how to do arrays...that != n00b :lol:

 

umm ill have to go actually code someting...unless i can find some old code...

 

EDIT: ok im installing the SDK now, so ill be back to typing useless code soon :lol:

Edited by cchalogamer

Share this post


Link to post
Share on other sites

OK so after some time i figured out a way to make 2D arrays, which are simple enough and accomplish waht im going for. So anyway, heres my code, im getting a FRIGGIN out of bounds error EVERY SINGLE ARRAY I DO gets this stupid out of bounds error! >.< can anyone tell me whats wrong its really aggitating me!

 

public class Pascal {
 int n = 0;
 public Pascal(int o){
   n = o;} 

int[][] array;
 
 public void createArray(){
   for(int i = 0; i < n; i++){
   array = new int[i][i + 1];} // Creates a 2D array
 }
 
 public void createTriangle(){
 for(int i = 0; i < n; i++){
   array[i][0] = 1;   // First number is 1
   array[i][i] = 1;  // Last number is 1
   if(i > 2){  // Compensates for error of trying to access a block that doesnt exist
   for(int j = 1; j < i - 1; j++){  // Calculate all variables in the given array except at position 0 & last
     array[i][j] = array[i-1][j] + array[i-1][j-1];}  // Calculates the next array by querying the two previous arrays positions just as a human would
   }
   }
 }
 
 public void showScoreboard(){
   for(int i = 0; i < n; i++){
     for(int p = 0; p < n; p++){
     System.out.println(array[i][p]);}  // Prints all the arrays at all the points
 }
 }
 }

 

I guess it says its on line 15 if im reading it correctly....

Edited by DrAwesomePhD

Share this post


Link to post
Share on other sites

public class PascalsTriangle {
   private int[][] array;

   public PascalsTriangle(int rows) {
       createArray(rows);
   }

   private void createArray(int rows) {
       /* create a 2D array */
       this.array = new int[rows][rows];

       /* set all elements in array to 0 to start with */
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < rows; j++) {
               this.array[i][j] = 0;
           }
       }
   }

   public void createTriangle() {
       /* top of the triangle is always:
            1
           1 1
        */
       this.array[0][0] = 1;
       this.array[1][0] = 1;
       this.array[1][1] = 1;

       /* go through array and calculate values, crappy algorithm though! :-) */
       for(int i = 2; i < this.array.length; i++){
           /* don't try and calculate the value for every element on the row, only up to the current row number (i + 1)! */
           /* e.g, row 1 will only have 1 value, row 2 will have 2 values etc:
                1
               1 1
           */
           for (int j = 0; j < (i + 1); j++) {
               /* first element in the row is always 1 */
               if (j == 0) {
                   this.array[i][j] = 1;
               } else {
                   /* calculate current element by looking at two previous rows elements above and adding them */
                   this.array[i][j] = this.array[i - 1][j - 1] + this.array[i - 1][j];
               }
           }
       }
   }

   public void showScoreboard() {
       /* cycle through the entire array and print the values out */
       for(int i = 0; i < this.array.length; i++){
           for(int p = 0; p < this.array[0].length; p++){
               /* only print the value if it contains a number greater than 0 */
               if (array[i][p] > 0) {
                   System.out.print(array[i][p] + " ");
               }
           }

           System.out.println();
       }
   }

   public static void main(String args[]) {
       PascalsTriangle p = new PascalsTriangle(10);
       p.createTriangle();
       p.showScoreboard();
   }
}

 

Crappy algorithm, but works at least :P..... i think

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...