Geen.Kl
Geen.Kl

Reputation: 3

Pascal Triangle recursive program not working

Here is my code... I am a beginner and appreciate any feed back. My problem I am trying to solve is to print the pascal triangle up to the user imputed row.

public class Pascal111 {

  int max;
  int r,c;
  public int pascal(int maxm){
  max=maxm;

  for(int r=0; r<max; r++){
    for (int c=0; c<r; c++){
     populate();}
 }  
  return max;
 }



  public void populate(){
   int[][] array=new int[max][max];
   if (r==0){
     array[0][0]=1;
   }
   else if(c==0){
     array[0][0]=1;
}
else if(c==r){
  array[r][c]=1;;
}
else
  array[r][c]=(array[r-1]+array[c-1]+array[r-1]+array[c]);
 }

}

I get the following error....

Error: bad operand types for binary operator '+'
  first type:  int[]
  second type: int[]

Upvotes: 0

Views: 88

Answers (2)

ajb
ajb

Reputation: 31689

Although I haven't looked at this closely, one mistake stands out: you have two different variables named r and two different variables named c.

Here's the relevant parts of your code:

public class Pascal111 {

    int r,c;  // (A)

    public int pascal(int maxm) {
        for (int r=0; r<max; r++){    // (B)
            for (int c=0; c<r; c++){  // (B)
                populate();
            }
        }  
        return max;
    }

    public void populate() {
        if (r==0) {
            ... more code that accesses r and c
        }
    }

The declaration at (A) declares instance variables, because the declaration isn't inside a method. This means that whenever you create a Pascal111 object, this object will have fields named r and c. They are initialized to 0.

The declarations at (B), since they have int keywords on them, declare new variables. These variables are local to the pascal method, which means they can be accessed only inside that method. (Actually, they can be accessed only inside the for loops.) These variables have no connection to the instance variables. They are totally separate. As you increment these local r and c variables, the instance variables remain unchanged. That is, they are still 0.

The populate method can't access local variables declared in pascal. So when populate refers to r and c, they are referring to the instance variables. These are not the variables that get incremented in the loop, but rather the instance variables that remain 0.

One way to fix this would be to remove the int on the declarations in (B); that would cause pascal to increment the instance variables, and then populate would use those. However, that is the wrong approach.

The best way to fix these is not to use the instance variables to communicate between pascal and populate--but rather, to use parameters. That is:

    for (int r=0; r<max; r++){    // (B)
        for (int c=0; c<r; c++){  // (B)
            populate(r, c);
        }
    }  

and later:

public void populate(int r, int c) {

Then, when populate refers to r and c, it will refer to the parameters, which are in essence local variables. The parameters will be the r and c from pascal. (But you don't have to use the same names for the pascal variables and the populate parameters.) After you do this, you'll find that the instance variables are no longer needed at all. It's always, or almost always, wrong to have instance variables used as loop indexes anyway.

A similar error: You have array as a local variable inside populate. This means that you're creating a brand new array every time you call it to set up one array element. Then, when populate returns, array can no longer be accessed, so whatever work you've done is lost.

Upvotes: 1

Saurav Sahu
Saurav Sahu

Reputation: 13924

array[r][c]=(array[r-1]+array[c-1]+array[r-1]+array[c]);

is wrong.

array is a 2-D array, thus array[X] represents a complete row (1-D array). You just can't do binary operation on 1-D arrays and put it on a single element array[r][c].

Instead replace it with

array[r][c]=(array[r-1][c-1]+array[r-1][c]);

Upvotes: 1

Related Questions