C程序,用命令行参数计算矩阵的行列式

C程序,用命令行参数计算矩阵的行列式,c,matrix,C,Matrix,所以我有这样的任务: “编写一个计算矩阵行列式的程序。矩阵的大小应作为程序的命令行参数键入。程序执行后,用户应从键盘键入矩阵元素。行列式应根据不同的方阵大小计算。”(由于不允许使用动态内存分配,因此必须在主函数中创建矩阵,并将其作为参数传递给其他函数。这使用了C的一个称为可变长度数组的功能: int matrix[size*size]; inputMatrix(size, matrix); 如果也不允许使用可变长度数组,只需将其设置为矩阵[9]-任务中的大小最多为3答案解决了主要问题,但OP的

所以我有这样的任务:
“编写一个计算矩阵行列式的程序。矩阵的大小应作为程序的命令行参数键入。程序执行后,用户应从键盘键入矩阵元素。行列式应根据不同的方阵大小计算。”(由于不允许使用动态内存分配,因此必须在主函数中创建矩阵,并将其作为参数传递给其他函数。这使用了C的一个称为可变长度数组的功能:

int matrix[size*size];
inputMatrix(size, matrix);
如果也不允许使用可变长度数组,只需将其设置为
矩阵[9]
-任务中的大小最多为3

答案解决了主要问题,但OP的代码中还有其他问题需要解决

首先,我们必须决定是使用数组数组(如
int-mat[3][3];
)还是简单数组(如
int-mat[9];
),而在OP的代码中,对它有一些混淆:

int main(int argc, char *argv[])
{
    // ...
    int *matrix;        // <-- This pointer is uninitialized, its value is indeterminated
    // ...
    printMatrix(*matrix, size);
    //          ^ dereferencing it, you are passing an 'int'
}
// ...
void printMatrix(int *matrix, int size)
{   //               ^ a pointer to an int is expected
    int i, j;
    int *matrix;  // <-- This is a local variable that will shadow the parameter
                  //     with the same name and it is also uninitialized.

    for(i=0; i<size; i++)
    {
        for(j=0; j<size; j++)
        {
            printf("%d ", *(*(matrix +i) + j));
            //            ^^^^  this is equivalent to 'matrix[i][j]',
            //                  but 'matrix' is only a pointer to 'int'
        }
        printf("\n");
    }
}
intmain(intargc,char*argv[])
{
// ...

int*matrix;//通过动态内存分配,查找n阶方阵的行列式(n是有限的)非常容易。但是,在这种情况下,重要的是在程序结束时释放内存


您可以查看我的代码!

您缺少为矩阵在堆上分配内存的部分。如果它不是编译的,请自上而下查看错误并尝试找出它们的含义。然后您可以尝试更正它们。如果您不理解某个特定的错误消息,请询问此问题。同时打开额外的警告并更正代码中对变量的范围有一些混淆,尤其是
矩阵
“您将使用的数组大小由用户在程序执行期间确定,因此不需要使用动态内存分配。“听起来有点违反直觉,但考虑到前面提到的限制”不同的方阵大小(虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效
int main(int argc, char *argv[])
{
    // ...
    int *matrix;        // <-- This pointer is uninitialized, its value is indeterminated
    // ...
    printMatrix(*matrix, size);
    //          ^ dereferencing it, you are passing an 'int'
}
// ...
void printMatrix(int *matrix, int size)
{   //               ^ a pointer to an int is expected
    int i, j;
    int *matrix;  // <-- This is a local variable that will shadow the parameter
                  //     with the same name and it is also uninitialized.

    for(i=0; i<size; i++)
    {
        for(j=0; j<size; j++)
        {
            printf("%d ", *(*(matrix +i) + j));
            //            ^^^^  this is equivalent to 'matrix[i][j]',
            //                  but 'matrix' is only a pointer to 'int'
        }
        printf("\n");
    }
}
// ...
#define MAX_SIZE 3

int main(int argc, char *argv[])
{
    int matrix[MAX_SIZE][MAX_SIZE];
    // read 'size' from command line arguments, then
    inputMatrix(size, matrix);   // <-- Note that I'm passing 'matrix' here too.
    printMatrix(size, matrix);
    // ...
}

// The functions must be modified accordingly, e.g.:
void printMatrix(int size, int matrix[][MAX_SIZE])
// The inner dimension must be specified ^^^^^^
{   
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            printf("%d ", *(*(matrix + i) + j)); // <-- "Use pointers." they said
        }
        printf("\n");
    }
}
// ...
#define MAX_SIZE 9    // <-- the total size: 3*3

int main(int argc, char *argv[])
{
    int matrix[MAX_SIZE];
    // ...
    printMatrix(size, matrix);
    // ...
}

void printMatrix(int size, int matrix[])
{   
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            printf("%d ", *(matrix + (i * size + j));
            // Note the math      ^^^^^^^^^^^^^^^^^ 
            // In this simple case of row wise traversal, it could be as simple as
            //            *(matrix++) 
        }
        printf("\n");
    }
}