1

我正在尝试使用 C 中动态创建的数组进行高斯消除,但每次运行它时,它都会在高斯消除部分停止。我不知道出了什么问题,IDE 说没有错误,但程序在嵌套的 for 循环部分停止。我从网上得到了高斯消除代码并试图将它实现到我的程序中,但它没有任何帮助?

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

double *createDynamicArray(int n);
double **createDynamicArray2D(int n, int m);
void displayArray(double **a, int n, int m);
void inputElements(double **a, int n, int m);
double gaussianElimination(double **a, int n, int m);



int main() {
    double **matrix,x[20], c, matrix2[20][20], sum;
    int n,m,i,j;
    printf("Gaussian Elimination for Linear Equations\n\n");
    printf("Enter Number of Equations for Matrix: ");
    scanf("%d",&n);
    m = n + 1;
    
    matrix = createDynamicArray2D(n,m);
    
    inputElements(matrix,n,m);

    printf("\nMatrix:\n");
    displayArray(matrix,n,m);
    
    
    printf("\nGaussian Elimination\n");
    gaussianElimination(matrix,n,m);
    displayArray(matrix,n,m);
    
    
    for (int x=0 ; x<n; x++){
        for (int y=0; y<n; y++){
            matrix2[x][y]==matrix[x][y];
        }
    }
    
    x[n] = matrix2[n][m]/matrix2[n][n];
    
     for(i=n-1;i>=1;i--)
     {
          x[i] = matrix2[i][n+1];
          for(j=i+1;j<=n;j++)
          {
                x[i] = x[i] - matrix2[i][j]*x[j];
          }
          x[i] = x[i]/matrix2[i][i];
     }

    for(i=n-1; i>=1; i--)
    {
        sum=0;
        for(j=i+1; j<=n; j++)
        {
            sum=sum+matrix[i][j]*x[j];
        }
        x[i]=(matrix[i][n+1]-sum)/matrix[i][i];
    }
    printf("\nThe Values are: \n");
    for(i=1; i<=n; i++)
    {
        printf("\nx%d = %.0f\t",i,x[i]);
    }
}

    

void displayArray(double **a, int n, int m) {
    int i,j;
    for (i=0; i<n; i++) {
        for (j=0; j<m; j++) {
            printf("%.0f\t",a[i][j]);
        }
        printf("\n");
    }
}

double *createDynamicArray(int n) {
    return (double *) calloc(n,sizeof(double));
}


double **createDynamicArray2D(int n, int m) {
    int i;
    double **y;
    y = (double **) calloc(n,sizeof(double **));
    for (i=0; i<n; i++) y[i] = createDynamicArray(m);
    return y;
}

void inputElements(double **a, int n, int m) {
    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            printf("Element [%d][%d] : ", i,j);
            scanf("%lf",&a[i][j]);
        }
    }
}


double gaussianElimination(double **a, int m, int n){
    int i,j,k;
    double term;
    for(i=0;i<m-1;i++){
        for(k=i+1;k<m;k++){
            term=a[k][i]/a[i][i];
            for(j=0;j<n;j++){
                a[k][j]=a[k][j]-term*a[i][j];
            }
        }
    }
}

好的更新我让它运行但输出错误:

Gaussian Elimination for Linear Equations

Enter Number of Equations for Matrix: 3
Element [0][0] : 1
Element [0][1] : 2
Element [0][2] : 3
Element [0][3] : 4
Element [1][0] : 5
Element [1][1] : 6
Element [1][2] : 7
Element [1][3] : 8
Element [2][0] : 9
Element [2][1] : 10
Element [2][2] : 1
Element [2][3] : 2

Matrix:
1       2       3       4
5       6       7       8
9       10      1       2

Gaussian Elimination
1       2       3       4
0       -4      -8      -12
0       0       -10     -10

The Values are:

x1 = -57718
x2 = -57718
x3 = 57718

有什么帮助吗?

4

0 回答 0