使用多维数组的递归和动态内存分配来查找NxN矩阵的行列式 [英] Using recursion and dynamic memory allocation of multidimensional arrays to find the determinant of an NxN matrix

查看:206
本文介绍了使用多维数组的递归和动态内存分配来查找NxN矩阵的行列式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,可以计算任何NxN矩阵的行列式,而不考虑大小,但程序有问题,并且崩溃了大小大于1的矩阵。

I'm trying to write a program that can calculate the determinant of any NxN matrix regardless of the size but there's something wrong with the program and it crashes for any matrix with size greater than 1.

我非常感谢任何可以告诉我我做错了什么的人。我是c ++和动态内存的新手,所以请稍等一下(。。

I'd be very grateful to anyone who can tell me what I'm doing wrong. I'm new to c++ and dynamic memory so, take it easy on me please (:.

这是我的程序:

#include <iostream>

using namespace std;

int determinant(int *matrix[], int size);
void ijMinor(int *matrix[], int *minorMatrix[], int size, int row, int column);

int main()
{
    int size;
    cout << "What is the size of the matrix for which you want to find the determinant?:\t";
    cin >> size;

    int **matrix;
    matrix = new int*[size];
    for (int i = 0 ; i < size ; i++)
        matrix[i] = new int[size];

    cout << "\nEnter the values of the matrix seperated by spaces:\n\n";
    for(int i = 0; i < size; i++)
        for(int j = 0; j < size; j++)
            cin >> matrix[i][j];

    cout << "\nThe determinant of the matrix is:\t" << determinant(matrix, size) << endl;

    return 0;
}

int determinant(int *matrix[], int size){
    if(size==1)return matrix[0][0];
    else{
        int result=0, sign=-1;
        for(int j = 0; j < size; j++){

            int **minorMatrix;
            minorMatrix = new int*[size-1];
            for (int k = 0 ; k < size-1 ; k++)
                matrix[k] = new int[size-1];

            ijMinor(matrix, minorMatrix, size, 0, j);

            sign*=-1;
            result+=sign*matrix[0][j]*determinant(minorMatrix, size-1);
            for(int i = 0; i < size-1; i++){
                delete minorMatrix[i];
            }
        }

        return result;
    }
}

void ijMinor(int *matrix[], int *minorMatrix[], int size, int row, int column){
    for(int i = 0; i < size; i++){
        for(int j = 0; j < size; j++){
            if(i < row){
                if(j < column)minorMatrix[i][j] = matrix[i][j];
                else if(j == column)continue;
                else minorMatrix[i][j-1] = matrix[i][j];
            }
            else if(i == row)continue;
            else{
                if(j < column)minorMatrix[i-1][j] = matrix[i][j];
                else if(j == column)continue;
                else minorMatrix[i-1][j-1] = matrix[i][j];
            }
        }
    }
}


推荐答案

由于以下原因,您的 minorMatrix 由未初始化的指针组成:

Your minorMatrix consists of uninitialised pointers because of this:

minorMatrix = new int*[size-1];
for (int k = 0 ; k < size-1 ; k++)
    matrix[k] = new int[size-1];

matrix [k] 应该是code> minorMatrix [k] 。

matrix[k]should be minorMatrix[k].

这篇关于使用多维数组的递归和动态内存分配来查找NxN矩阵的行列式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆