2

im trying to do lu decomposition using nxn matrix typed by scanf but having errors on using nxn matrix for gauss function i what to know the ways to use array by malloc on function. the way to use matrix[x][y] on gauss fucntion is the point

#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];
        }
    }
}
}
Himanshu
  • 4,327
  • 16
  • 31
  • 39
Kim Iku
  • 21
  • 2
  • What is the error you get ? That apart, you alloc `sizeof(float)` when your variables are declared `double`, there's probably a mismatch somewhere. – Joël Hecht Apr 28 '15 at 06:10
  • i can't call out matrix on gauss function it stops when gauss is called :/ – Kim Iku Apr 28 '15 at 07:19
  • http://stackoverflow.com/questions/12462615/how-do-i-correctly-set-up-access-and-free-a-multidimensional-array-in-c – Lundin Apr 28 '15 at 10:42

2 Answers2

2
  1. You allocate memory for float matrix, but then use it as double, since sizes of float and double are different you got errors.

  2. The correct way to go through matrix rows and colums is like this

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

    but you wrote

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

    so you tried to access non-existing row with index n (but last row has index n-1).

Corrected code:

#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];
            }
        }
    }
}
Nikolay K
  • 3,770
  • 3
  • 25
  • 37
  • "throw" is a verb, did you mean, "go trough" or "go through"? (+1 anyway) – Lutz Lehmann Apr 28 '15 at 10:24
  • I wonder if someone has thought about an editor mode "english for foreigners and Americans" that automatically highlights problematic words like "tail <-> tale", "passed <-> past", "there <-> their <-> they're", "your <-> you're" etc. – Lutz Lehmann Apr 28 '15 at 10:31
1

For complete analysis, see answer of NicolayKondratyev. One small optimization:

One should avoid to call "malloc" too often, the minimal variant for a nxn matrix is

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

From Lundin's link in comment to question https://stackoverflow.com/a/12462760/3088138 , since C99 the shortest and most economic matrix allocation is

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

which avoids the layered pointer-to-pointer structure.

Community
  • 1
  • 1
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
  • Please don't recommend to cast the result of malloc in C. It wasn't there in the original code. – Lundin Apr 28 '15 at 10:43
  • @Lundin: Is there a deeper reason? It may be obvious and automatic in many cases, here however a memory area is spliced into two arrays of different type, the explicit casts at least increase readability. – Lutz Lehmann Apr 28 '15 at 10:50
  • See the SO [FAQ](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Lundin Apr 28 '15 at 10:53
  • Ok. Casting malloc results may hide programming errors. Does this also apply to the second cast? – Lutz Lehmann Apr 28 '15 at 11:04
  • Yes. There is never a reason to cast the result of malloc in C. – Lundin Apr 28 '15 at 11:06