Hi there.

I wonder if anyone can help me.

I have this code but it only works for a 3 by 3 matrix. I was wondering if anyone could help me change it to a for loop so you can work out different matrixes like a 4 by 30 matrix.

#include <iostream>

#include <cmath>

using namespace std;

void gauss(int N, // number of unknowns

float A [20] [21], // coefficients and constants

float result[20],

bool& err)

// Solve system of N linear equations with N unknowns

// using Gaussian elimination with scaled partial pivoting

// First N rows and N+1 columns of A contain the system

// with right-hand sides of equations in column N+1

// err returns true if process fails; false if it is successful

// original contents of A are destroyed

// solution appears in column N

{

int indx[20];

float scale[20];

float maxRatio;

int maxIndx;

int tmpIndx;

float ratio;

float sum;

for (int i = 0; i < N; i++) indx* = i; // index array initialization
// determine scale factors
for (int row = 0; row < N; row++)
{
scale[row] = abs(A[row][0]);
for (int col = 1; col < N; col++)
{
if (abs(A[row][col]) > scale[row]) scale[row] = abs(A[row][col]);
}
}
// forward elimination
for (int k = 0; k < N; k++)
{
// determine index of pivot row
maxRatio = abs(A[indx[k]] [k])/scale[indx[k]];
maxIndx = k;
for (int i = k+1; i < N; i++)
{
if (abs(A[indx*

*] [k])/scale[indx*

*] > maxRatio) { maxRatio = abs(A[indx**] [k])/scale[indx**]; maxIndx = i; } } if (maxRatio == 0) // no pivot available { err = true; return; } tmpIndx =indx[k]; indx[k]=indx[maxIndx]; indx[maxIndx] = tmpIndx; // use pivot row to eliminate kth variable in "lower" rows for (int i = k+1; i < N; i++) { ratio = -A[indx**] [k]/A[indx[k]] [k]; for (int col = k; col <= N; col++) { A[indx**] [col] += ratio*A[indx[k]] [col]; } } } // back substitution for (int k = N-1; k >= 0; k--) { sum = 0; for (int col = k+1; col < N; col++) { sum += A[indx[k]] [col] * A[indx[col]] [N]; } A[indx[k]] [N] = (A[indx[k]] [N] - sum)/A[indx[k]] [k]; }*
for (int k = 0; k < N; k++) result[k] = A[indx[k]] [N];

}

int main()

{

float A[20][21];

float X[20];

bool err;

int N;

float x1,x2,x3,y1,y2,y3,z1,z2,z3,b1,b2,b3;

cout << "Enter X1: ";

cin>> x1;

A[0][0] = x1;

cout << "Enter Y1: ";

cin>> y1;

A[0][1] = y1;

cout << "Enter Z1: ";

cin>> z1;

A[0][2] = z1;

cout << "Enter B1: ";

cin>> b1;

A[0][3] = b1;

//----------

cout << "Enter X2: ";

cin>> x2;

A[1][0] = x2;

cout << "Enter Y2: ";

cin>> y2;

A[1][1] = y2;

cout << "Enter Z2: ";

cin>> z2;

A[1][2] = z2;

cout << "Enter B2: ";

cin>> b2;

A[1][3] = b2;

//-----------

cout << "Enter X3: ";

cin>> x3;

A[2][0] = x3;

cout << "Enter Y3: ";

cin>> y3;

A[2][1] = y3;

cout << "Enter Z3: ";

cin>> z3;

A[2][2] = z3;

cout << "Enter B3: ";

cin>> b3;

A[2][3] = b3;

gauss(3, A, X, err);

for (int i=0; i<3; i++) cout << X* << " ";
std::cin.get();
return 0;
}*