Question

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.

Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top