0

Row is user-inputted.

cout << "Input the number of rows: ";
cin >> row;
column=row;

int triangle[row][column];

for (i=0;i<=row;i++){
    for (j=0;j<=column;j++){
           triangle[i][j]=0;
    }
}

for (i=0;i<=row;i++){
    for (j=0;j<=i;j++){
           if (j==0 || j==i){
           triangle[i][j]=1;
           } else {
           triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1];
           }
    }
}

cout << "Pascals triangle with " << row << " rows.";

for (i=0;i<=row;i++){
    for (j=0;j<=i;j++){
        cout << triangle[i][j] << "\t";
    }
    cout << endl;
}

It does give out proper results when the row is seven, but it somehow crashes when the inputted row is greater than 8.

user1118321
  • 25,567
  • 4
  • 55
  • 86
user2027369
  • 63
  • 1
  • 7

1 Answers1

2

Most likely triangle is not declared with enough memory for the indices you use. If row==column==8 then you need to declare it like this:

double triangle[9][9];

Because C++ uses zero-based indices this allows for indices in the range 0 to 8 inclusive. Most likely your declaration is like this:

double triangle[8][8];
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490