im trying lu decomposition using nxn matrix typed scanf having errors on using nxn matrix gauss function know ways use array malloc on function. way use matrix[x][y] on gauss fucntion 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); } (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); } (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); } (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); } (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 number of matrix \n"); (x = 0; x < n; x++){ (y = 0; y < n; y++){ printf("line %d x%d number : ", x + 1, y + 1); scanf("%lf", &matrix[x][y]); } } /* (x = 0; x < n; x++){ (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); /* (i = 0; i<n; i++) { free(matrix[i]); } free(matrix);//free matrix (i = 0; i<n; i++) { free(l[i]); } free(l);//free l (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]; } } } }
you allocate memory
float
matrix, usedouble
, since sizes offloat
,double
different got errors.the correct way go through matrix rows , colums this
for(x=0; x<n; x++){ ... }
but wrote
for(x=0;x<=n;x++){ .... }
so tried access non-existing row index
n
(but last row has indexn-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); } (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); } (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); } (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); } (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 number of matrix \n"); (x = 0; x < n; x++){ (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]; } } } }