【问题标题】:array by malloc on function lu decompositionmalloc 对函数 lu 分解的数组
【发布时间】:2015-04-28 05:14:57
【问题描述】:

我尝试使用由 scanf 键入的 nxn 矩阵进行 lu 分解,但在将 nxn 矩阵用于高斯函数时出错我知道如何通过 malloc 函数使用数组的方法。 在高斯函数上使用matrix[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];
        }
    }
}
}

【问题讨论】:

  • 你得到什么错误?除此之外,当你的变量被声明为double 时,你分配了sizeof(float),可能某处不匹配。
  • 我无法在高斯函数上调用矩阵,它在调用高斯时停止:/

标签: c matrix


【解决方案1】:
  1. 您为float 矩阵分配内存,但随后将其用作double,因为floatdouble 的大小不同,您会出错。

  2. 矩阵行列的正确遍历方式是这样的

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

    但你写了

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

    所以您尝试使用索引n 访问不存在的行(但最后一行的索引为n-1)。

更正的代码:

#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];
            }
        }
    }
}

【讨论】:

  • "throw" 是动词,你的意思是“go trough”还是“go through”? (无论如何+1)
  • 我想知道是否有人考虑过一种编辑模式“外国人和美国人的英语”,它会自动突出显示有问题的单词,如“tail tale”、“passed past”、“there 他们的 他们是”、“你的 你是”等
【解决方案2】:

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

应该避免过于频繁地调用“malloc”,nxn 矩阵的最小变体是

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

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

float (*matrix)[n] = malloc(sizeof(float[n][n]));

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

【讨论】:

  • 请不要推荐将 malloc 的结果转换成 C 语言。原始代码中没有。
  • @Lundin:还有更深层的原因吗?在许多情况下它可能是显而易见的和自动的,但是这里的内存区域被拼接成两个不同类型的数组,显式转换至少增加了可读性。
  • 查看 SO FAQ
  • 好的。强制转换 malloc 结果可能会隐藏编程错误。这是否也适用于第二次演员表?
  • 是的。从来没有理由在 C 中强制转换 malloc 的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多