не работают алгебраические преобразования в матрице

В функции gaussmethod представлен поиск определителя матрицы по методу гаусса , сначала в первом цикле с помощью алгебраических преобразованию обнуляются элементы ниже главной диагонали , затем второй цикл по обнулению элементов которые выше глав.диагонали, но цикл почему то обнуляет только элементы которые "прилегают" к ней , в выводе в консоле показано как изменяются переменные в цикле , я не нашел ошибки , подскажите пожалуйста в чем может быть причина . P.S Код без индексов , который закомментирован , работает на матрице 4*4 без проблем , на примере его я сделал цикл. И в случае если на главной диагонали встречается ноль то выводится сообщение об ошибке


#include <iostream>
#include <math.h>
#include <ctime>
#include <iomanip>
#define clean system("cls");
#define next cout<<endl;
using namespace std;
void ReverseMtx(float** arr, float** arrdoubl, int rows)//обратная матрица
{

}

int showMass(float** arr, int rows)//вывод массива
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < rows; j++)
        {
            cout << fixed << setprecision(2) << setw(6) << arr[i][j] << " ";
        }
        next;
    }
    next
        return 0;

}
int MultiMass(float** arr, float** arrdoubl, float** arrsumm, int rows)
{
    showMass(arr, rows);
    showMass(arrdoubl, rows);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < rows; j++)
        {
            arrsumm[i][j] = 0;
            for (int k = 0; k < rows; k++)
            {
                arrsumm[i][j] += arr[i][j] * arrdoubl[j][i];
            }
        }
    }
    showMass(arrsumm, rows);
    return 0;
}
int SummMass(float** arr, float** arrdoubl, float** arrsumm, int rows)//вычисление суммы матриц
{
    showMass(arr, rows);
    next
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < rows; j++)
            {
                arrdoubl[i][j] = rand() % 20;
                cout << fixed << setprecision(2) << setw(6) << arrdoubl[i][j] << " ";
            }
            next;
        }
    next
        for (int i = 0; i < rows; i++) {// результат суммирования матриц в новую матрицу
            for (int j = 0; j < rows; j++)
            {
                arrsumm[i][j] = arr[i][j] + arrdoubl[i][j]; cout << setw(6) << arrsumm[i][j] << " ";
            }
            next
        }
    next
        return 0;
}
int DeleteMass(float** arr, int rows)// удаление динамической матрицы
{
    for (int i = 0; i < rows; i++)
    {
        delete[] arr[i];
    }
    delete[] arr;
    return 0;
}
int massarr(float** arr, int rows)// вывод и заполнение рандомными числами элементов матрицы
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < rows; j++)
        {
            arr[i][j] = rand() % 20;
            cout << fixed << setprecision(2) << setw(6) << arr[i][j] << " ";
        }
        next;
    }
    return 0;
}
int Gaussmethod(float** arr, int rows)// вывод det
{
    bool isNull = false;
    for (int i = 0; i < rows - 1; i++)
    {
        if (arr[i][i] == 0)
        {
            isNull = true;
            break;
        }
    }
    if (isNull)
    {
        cout << "Problem on diagonal" << endl;//если находим нуль на главной диагонали, то выводим сообщение  об ошибке
        return 0;
    }
    for (int d = 0; d < rows - 1; d++)//прямой ход
    {
        for (int i = d + 1; i < rows; i++)
        {
            double tmp = 1.0 * arr[i][d] / arr[d][d];
            for (int j = 0; j < rows; j++)
            {
                arr[i][j] = arr[i][j] - arr[d][j] * tmp;
            }

        }
    }
    int det = 1;
    for (int i = 0; i < rows; i++)  det *= arr[i][i];
    cout << "determinant= " << det << endl;
    cout << "nuli snizu" << endl;
    showMass(arr, rows);
    int schet = rows - 1;
    for (int j = rows - 1; j > 0; j--)
    {
        int iDop = j - 1;
        for (int i = j - 1; i >= 0; i--)
        {
            cout << "i = " << i << endl;
            cout << "iDop = " << iDop << endl;
            cout << "j = " << j << endl;next
            arr[i][j] = arr[i][j] - arr[j][j] * (arr[iDop][j] / arr[j][j]);
        }
    }
    /*
arr[2][3] = arr[2][3] - arr[3][3] * (1.0 * arr[2][3] / arr[3][3]);
arr[1][3] = arr[1][3] - arr[3][3] * (1.0 * arr[1][3] / arr[3][3]);
arr[0][3] = arr[0][3] - arr[3][3] * (1.0 * arr[0][3] / arr[3][3]);

arr[1][2] = arr[1][2] - arr[2][2] * (arr[1][2] / arr[2][2]);
arr[0][2] = arr[0][2] - arr[2][2] * (arr[0][2] / arr[2][2]);

arr[0][1] = arr[0][1] - arr[1][1] * (arr[0][1] / arr[1][1]);
*/
cout << "nuli sverhu" << endl;
showMass(arr, rows); next
det = 1;
for (int i = 0; i < rows; i++)  det *= arr[i][i];
cout << "determinant= " << det << endl;
return 0;
}
int main()
{
    int rows, columns;
    int choice;
    srand(time(0));
    //input matrix
    cout << "Enter the number of the columns" << endl;
    cin >> columns;
    clean
        while (columns <= 0 || columns > 15) { cout << "Enter the number of columns(>0,<15)"; next; cin >> columns; }
    cout << "Enter the number of the rows" << endl;
    cin >> rows;
    clean
        while (rows <= 0 || rows > 15) { cout << "Enter the number of rows(>0,<15)"; next; cin >> rows; }
    float** arr = new float* [rows];
    for (int i = 0; i < rows; i++)
    {
        arr[i] = new float[columns];
    }
    cout << "1.Randomly ,2.Manually" << endl;//Рандомно или вручную
    cin >> choice;
    clean
        switch (choice)
        {
        case 1:
        {
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++)
                {
                    arr[i][j] = rand() % 20;
                }
                next;
            }
            break;
        }
        case 2:
        {
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++)
                {
                    cout << "a[ " << i << "," << j << " ]= ";
                    cin >> arr[i][j];
                }

            }
            break;
        }
        }
    if (rows == columns) cout << "All is good, your matrix is suitable for calculating the determinant." << endl;//матрица подходит
    else cout << "Your matrix is not suitable for calculating the determinant ." << endl;// матрица не подходит
    if (rows < columns) {//если кол-во строк меньше кол-ва столбцов
        cout << "Number of columns is more than number of rows , the extra columns will be removed." << endl;
        int differrow = columns - rows;
        massarr(arr, rows);
        while (columns != rows) {
            cout << "Which column you want to delete ?" << endl;
            cin >> choice;
            for (int j = choice - 1; j < columns - 1; j++)
                for (int i = 0; i < rows; i++)
                    arr[i][j] = arr[i][j + 1];
            columns--;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                    cout << setw(6) << arr[i][j] << "  ";
                clean
            }
            next;
        }
    }
    if (columns < rows) {//если кол-во столбцов меньше кол-ва строк
        cout << "Number of rows is more than number of columns, the extra rows will be removed." << endl;
        int differcolumn = rows - columns;
    }
    cout << "Show" << endl;
    massarr(arr, rows);
    while (columns != rows) {
        cout << "Which row you want to delete ?" << endl;
        cin >> choice;
        for (int i = choice - 1; i < rows - 1; i++)
            for (int j = 0; j < columns; j++)
                arr[i][j] = arr[i + 1][j];
        rows--;
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
                cout << setw(6) << arr[i][j] << "  ";
            cout << endl;
        }
    }
    //Метод гаусса , det
    next;
    cout << "Gauss Method" << endl;
    showMass(arr, rows); next;
    Gaussmethod(arr, rows);

    float** arrdoubl = new float* [rows];//1. матрица rows*rows с рандомными числами
    for (int i = 0; i < rows; i++)
    {
        arrdoubl[i] = new float[rows];
    }
    float** arrsumm = new float* [rows];//2. матрица rows*rows для записи суммы 
    for (int i = 0; i < rows; i++)
    {
        arrsumm[i] = new float[rows];
    }


    cout << "Summ" << endl;
    SummMass(arr, arrdoubl, arrsumm, rows);//сумма матриц

    cout << "Multiplication" << endl;//умножение матриц
    MultiMass(arr, arrdoubl, arrsumm, rows);

    cout << "Reverse matrix" << endl;//обратная матрица
    //ReverseMass(arr, rows);

    DeleteMass(arrsumm, rows);
    DeleteMass(arrdoubl, rows);
    DeleteMass(arr, rows);
}




Ответы (0 шт):