Hey all, I was given a triangulate function in order to convert a matrix into an upper-triangular matrix.

Code:

//code excerpt:
#define ROWS 4
#define COLS 4
void triangulate(double a[][COLS+2], const size_t M, const size_t N)
{
for(size_t i=1; i<=N; i++)
{
for(size_t j=i+1; j<=N; j++)
{
for(size_t k=N+1; k>=i; k--)
{
a[j][k] -= a[i][k] * a[j][i]/a[i][i];
}
}
}
}

a = array, i = rows, j = columns.

However, this code was intentionally written for specific matrices, and the triangulate function puts a[2][2] as 0.

Code:

double array1[ROWS+2][COLS+2] = {{0, 0, 0, 0, 0, 0}, {0, -1, 0, 0, 0, -0.08}, {0, 1, -1, 0, 1, 0}, {0, 0, 1, -1, 0, 0.02}, {0, 0, 0, 1, 0, 0.05}};

The assignment is to "modify your triangulate function such that, when you are updating row i, search below row i in column i to find the row j containing the diagonal element of greatest nonzero absolute value, and then swap the entire row i with row j. After this row swap, resume your normal triangulation code."

This is what I have so far, but it's not doing what I hoped it would:

Code:

void triangulate(double a[][COLS+2], const size_t M, const size_t N)
{
double swap;
for(size_t i=1; i<=N; i++)
{
if(a[i+1][i] > a[i][i]){
swap = a[i][i];
a[i+1][i] = a[i][i];
a[i+1][i] = swap;
}
for(size_t j=i+1; j<=N; j++)
{
for(size_t k=N+1; k>=i; k--)
{
a[j][k] -= a[i][k] * a[j][i]/a[i][i];
}
}
}
}

How would I go about executing this?