数组由malloc on function lu分解

时间:2015-04-28 05:14:58

标签: c matrix

我尝试使用scanf键入的nxn矩阵进行lu分解,但是对于高斯函数使用nxn矩阵时出错有什么知道如何通过malloc函数使用数组。 在高斯函数中使用矩阵[x] [y]的方法是

#include <stdio.h> 
#include <stdlib.h>
#pragma warning(disable:4996)

void gauss(int n,double **matrix,double **L,double **U,double **ans);

int main(void)

{

    int i, n;//
    int x, y;//line x,row y
    double **matrix; //define matrix[x][y]
    double **L;
    double **U;
    double **ans;

    printf("nxn matrix type n.\n");

    scanf("%d", &n);



    matrix = malloc(sizeof(float *) * n); // int* number x primary structure
    if (matrix == NULL){ printf("malloc failed\n"); exit(1); }
    for (i = 0; i<n; i++)
    {
        matrix[i] = malloc(sizeof(float) * n);
        if (matrix[i] == NULL){ printf("malloc failed\n"); exit(1); }
    } //build matrix[x][y(size of x)] structure

    L = malloc(sizeof(float *) * n); // int* number x primary structure
    if (L == NULL){ printf("malloc failed\n"); exit(1); }
    for (i = 0; i<n; i++)
    {
        L[i] = malloc(sizeof(float) * n);
        if (L[i] == NULL){ printf("malloc failed\n"); exit(1); }
    } //build L[x][y(size of x)] structure

    U = malloc(sizeof(float *) * n); // int* number x primary structure
   if (U == NULL){ printf("malloc failed\n"); exit(1); }
   for (i = 0; i<n; i++)
   {
       U[i] = malloc(sizeof(float) * n);
       if (U[i] == NULL){ printf("malloc failed\n"); exit(1); }

   } //build U[x][y(size of x)] structure

   ans = malloc(sizeof(float *) * n); // int* number x primary structure
   if (ans == NULL){ printf("malloc failed\n"); exit(1); }
   for (i = 0; i<n; i++)
   {
       ans[i] = malloc(sizeof(float) * n);
       if (ans[i] == NULL){ printf("malloc failed\n"); exit(1); }

   } //build ans[x][y(size of x)] structure

    printf("type the number of matrix \n");
    for (x = 0; x < n; x++){
        for (y = 0; y < n; y++){
            printf("line %d  x%d number : ", x + 1, y + 1);
            scanf("%lf", &matrix[x][y]);
        }
    }
/*
for (x = 0; x < n; x++){
    for (y = 0; y < n; y++){
        printf("line %d  x%d number : %.2lf \n", x + 1, y + 1,matrix[x][y]);

    }
}
*/
gauss(n,matrix,L,U,ans);

/*

for (i = 0; i<n; i++)
{
    free(matrix[i]);
}
free(matrix);//free matrix

for (i = 0; i<n; i++)
{
   free(L[i]);
}
free(L);//free L

for (i = 0; i<n; i++)
{
free(U[i]);
}
free(U);//free U
*/

return 0;

}

void gauss(int n,double **matrix,double **L,double **U,double **ans){
int x,y;
for(x=0;x<=n;x++){
    if(matrix[x][0]!=0){
        for(y=0;y<=n;y++){
            matrix[x][y]=matrix[x][y]/matrix[x][0];
            L[x][0]=matrix[x][0];
        }
    }
}
}

2 个答案:

答案 0 :(得分:2)

  1. 您为float矩阵分配内存,但随后将其用作double,因为floatdouble的大小不同,您会收到错误。

  2. 通过矩阵行和列的正确方法就是这样

    for(x=0; x<n; x++){
        ...
    }
    

    但你写了

    for(x=0;x<=n;x++){
        ....
    }
    

    因此您尝试访问索引为n的不存在的行(但最后一行的索引为n-1)。

  3. 更正后的代码:

    #include <stdio.h> 
    #include <stdlib.h>
    
    void gauss(int n, float **matrix, float **L, float **U, float **ans);
    
    int main(void)
    {
    
        int i, n;//
        int x, y;//line x,row y
        float **matrix; //define matrix[x][y]
        float **L;
        float **U;
        float **ans;
    
        printf("nxn matrix type n.\n");
    
        scanf("%d", &n);
    
        matrix = malloc(sizeof(float *) * n); // int* number x primary structure
        if (matrix == NULL){ printf("malloc failed\n"); exit(1); }
        for (i = 0; i<n; i++)
        {
            matrix[i] = malloc(sizeof(float) * n);
            if (matrix[i] == NULL){ printf("malloc failed\n"); exit(1); }
        } //build matrix[x][y(size of x)] structure
    
        L = malloc(sizeof(float *) * n); // int* number x primary structure
        if (L == NULL){ printf("malloc failed\n"); exit(1); }
        for (i = 0; i<n; i++)
        {
            L[i] = malloc(sizeof(float) * n);
            if (L[i] == NULL){ printf("malloc failed\n"); exit(1); }
        } //build L[x][y(size of x)] structure
    
        U = malloc(sizeof(float *) * n); // int* number x primary structure
       if (U == NULL){ printf("malloc failed\n"); exit(1); }
       for (i = 0; i<n; i++)
       {
           U[i] = malloc(sizeof(float) * n);
           if (U[i] == NULL){ printf("malloc failed\n"); exit(1); }
    
       } //build U[x][y(size of x)] structure
    
       ans = malloc(sizeof(float *) * n); // int* number x primary structure
       if (ans == NULL){ printf("malloc failed\n"); exit(1); }
       for (i = 0; i<n; i++)
       {
           ans[i] = malloc(sizeof(float) * n);
           if (ans[i] == NULL){ printf("malloc failed\n"); exit(1); }
    
       } //build ans[x][y(size of x)] structure
    
        printf("type the number of matrix \n");
        for (x = 0; x < n; x++){
            for (y = 0; y < n; y++){
                printf("line %d  x%d number : ", x + 1, y + 1);
                scanf("%f", &matrix[x][y]);
            }
        }
    
        gauss(n,matrix,L,U,ans);
    
        return 0;
    }
    
    void gauss(int n, float **matrix, float **L, float **U, float **ans) {
        int x,y;
        for(x=0;x<n;x++){
            if(matrix[x][0]!=0){
                for(y=0;y<n;y++){
                    matrix[x][y]=matrix[x][y]/matrix[x][0];
                    L[x][0]=matrix[x][0];
                }
            }
        }
    }
    

答案 1 :(得分:1)

有关完整分析,请参阅NicolayKondratyev的回答。一个小优化:

应该避免打电话给&#34; malloc&#34;通常,nxn矩阵的最小变量是

+ (instancetype)requestDescriptorWithMapping:(RKMapping *)mapping objectClass:(Class)objectClass rootKeyPath:(NSString *)rootKeyPath method:(RKRequestMethod)method

从Lundin的评论链接到问题https://stackoverflow.com/a/12462760/3088138,因为C99是最短且最经济的矩阵分配

matrix = malloc( n*sizeof(*matrix)*n + n*n*sizeof(**matrix) ); 
if (matrix == NULL){ printf("malloc failed\n"); exit(1); }
matrix[0] = (float*)(matrix + n);
for(k=1; k<n; k++) matrix[k] = matrix[k-1] + n

避免了分层指针到指针的结构。