C程序计算NxN矩阵的行列式

时间:2016-12-29 16:58:09

标签: c arrays

我试图编写一个程序来计算我的决定因素,这是我迄今为止所做的。但是它没有工作它只是为我投入的每个矩阵打印6356918。我甚至将我的代码与互联网上的其他代码进行了比较,但这些代码无效。

我对指针一无所知,所以我不能使用它们。我尝试过调试,我也不太了解它,但是第一个' if'在第二个函数和计算行列式的代码的最后部分。我在code :: blocks中编码。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

main()
{
    int A[100][100];
    int i,j,k,n,res;
    printf("Enter the order of the matrix: \n");
    scanf("%d",&n);
    printf("\nEnter the elements of the matrix one by one: \n");
    for(i = 0 ; i < n ; i++)
    {
        for(j = 0 ; j < n ; j++)
        {
            scanf("%d",&A[i][j]);
        }
    }
    for(i = 0 ; i < n ; i++)
    {
        for(j = 0 ; j < n ; j++)
        {
            printf("%5d",A[i][j]);
        }
        printf("\n");
    }
    res = det(A,n);
    printf("%d",res);
}
int det(int A[100][100], int n)
{
    int Minor[100][100];
    int i,j,k,c1,c2;
    int determinant;
    int c[100];
    int O=1;

    if(n == 2)
    {
        determinant = 0;
        determinant = A[0][0]*A[1][1]-A[0][1]*A[1][0];
        return determinant;
    }
    else
    {
        for(i = 0 ; i < n ; i++)
        {
            c1 = 0, c2 = 0;
            for(j = 0 ; j < n ; j++)
            {
                for(k = 0 ; k < n ; k++)
                {
                    if(j != 0 && k != i)
                    {
                        Minor[c1][c2] = A[j][k];
                        c2++;
                        if(c2>n-2)
                        {
                            c1++;
                            c2=0;
                        }
                    }
                }
            }
            determinant = determinant + O*(A[0][i]*det(Minor,n-1));
            O=-1*O;
        }
    }
    return determinant;
}

2 个答案:

答案 0 :(得分:2)

在功能<?php $arr = array( 2066 => array ( 'images_id' => 2066, 'title' => 'title one' ), 2063 => array ( 'images_id' => 2063, 'title' => 'title two' ), 2022 => array ( 'images_id' => 2022, 'title' => 'title three' ) ); //Sanitizing here: array_walk($arr, function(&$value, &$key) { $value['images_id'] = esc_attr($value['images_id']); $value['title'] = esc_attr($value['title']); //--AND/OR-- $value['images_id'] = sanitize_text_field($value['images_id']); $value['title'] = sanitize_text_field($value['title']); }); //Now check the new array: echo '<pre>'; print_r($arr); echo '</pre>'; ?> 中,只有在没有必要时才初始化det()

determinant

但是在需要的时候

determinant = 0;
determinant = A[0][0]*A[1][1]-A[0][1]*A[1][0];

以前没有初始化。所以移动

determinant = determinant + O*(A[0][i]*det(Minor,n-1));

在函数开头附近的determinant = 0; 以上。

答案 1 :(得分:0)

这是一个计算任何 MxN 矩阵的行列式的 C 程序:

collapse