доброго всем времени суток
Нашел прогу решение СЛАУ методом Гаусса, но чет она сложновата для понимания, особенно я не понял зачем массив
Код:
int **index = new int*[n];
for (int i = 0; i < n; i++) index[i] = new int[3];
и что он делает
А также по возможности и по коду коменты раскидайте
Код:
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;
//solve the linear system of equations A * X = rhs by using the Gauss elimination technique
// A, rhs, and n are input variables
// n is the number of equations in the system
// X is the output vector
void Gauss(double **A, double *rhs, double *X, int n)
{
double **w = new double*[n];
for (int i = 0; i < n; i++) w[i] = new double[n];
int **index = new int*[n];
for (int i = 0; i < n; i++) index[i] = new int[3];
int nv = 1;
int irow = 0;
int icol = 0;
double pivot;
for (int i = 0; i < n; i++){
w[i][0] = rhs[i];
index[i][2] = 0;
}
double determ = 1;
for (int i = 0; i < n; i++){
double big = 0;
for (int j = 0; j < n; j++){
if (index[j][2] != 1)
for (int k = 0; k < n; k++)
{
if (index[k][2] > 1)
{
printf("Error at Gauss-Jordan: Matrix is singular\n");
exit(1);
}
if (index[k][2] < 1)
if (fabs(A[j][k]) > big)
{
irow = j;
icol = k;
big = fabs(A[j][k]);
}
}
}
index[icol][2] += 1;
index[icol][0] = irow;
index[icol][1] = icol;
if (irow != icol)
{
determ = -determ;
for (int l = 0; l < n; l++)
{
double temp = A[irow][l];
A[irow][l] = A[icol][l];
A[icol][l] = temp;
}
if (nv > 0)
for (int l = 0; l < nv; l++)
{
double temp = w[irow][l];
w[irow][l] = w[icol][l];
w[icol][l] = temp;
}
}
pivot = A[icol][icol];
determ *= pivot;
A[icol][icol] = 1;
for (int l = 0; l < n; l++)
A[icol][l] /= pivot;
if (nv != 0)
for (int l = 0; l < nv; l++)
w[icol][l] /= pivot;
for (int l1 = 0; l1 < n; l1++){
if (l1 != icol)
{
double t = A[l1][icol];
A[l1][icol] = 0;
for (int l = 0; l < n; l++)
A[l1][l] -= A[icol][l]*t;
if (nv != 0)
for (int l = 0; l < nv; l++)
w[l1][l] -= w[icol][l]*t;
}
}
}
for (int i = 0; i < n; i++){
int l = n-i-1;
if (index[l][0] != index[l][1]){
irow = index[l][0];
icol = index [l][1];
for (int k = 0; k < n; k++)
{
double temp = A[k][irow];
A[k][irow] = A[k][icol];
A[k][icol] = temp;
}
}
}
for (int k = 0; k < n; k++)
if (index[k][2] != 1){
printf("Error at Gauss-Jordan: Matrix is singular\n");
exit(1);
}
for (int i = 0; i < n; i++)
X[i] = w[i][0];
for (int i = 0; i < n; i++) delete [](w[i]);
for (int i = 0; i < n; i++) delete [](index[i]);
delete []w;
delete []index;
}
/////////////////////////////////////////////////////////////////////////
// MAIN PROGRAM
/////////////////////////////////////////////////////////////////////////
//Solve the following system of equations
// x + 2*y + 3*z = 1
// 3*x + 4*y + 5*z = 2
// 5*x + 6*y + 7*z = 3
int main()
{
int n = 3;
//make some memory allocations
double **A = new double*[n];
for (int i = 0; i < n; i++) A[i] = new double[n];
double *rhs = new double[n];
double *res = new double[n];
//set the matrix
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin>>A[i][j];
cout << "\n\n";
//set the right-hand-side
for (int i = 0; i < n; i++)
cin>> rhs[i];
Gauss (A,rhs,res, n);
//print the result on the screen
for (int i = 0; i < n; i++)
printf("res[%d] = %e\n", i,res[i]);
//delete the memory allocated at the beginning
for (int i = 0; i < n; i++) delete [](A[i]);
delete []A;
delete []rhs;
delete []res;
std::cin.get();
cin.get();
}
Заранее спасибо