阵列由malloc函数上的LU分解 [英] array by malloc on function lu decomposition
问题描述
我尝试使用由scanf函数输入n×n的矩阵,但其使用的n×n矩阵,高斯函数我怎么知道由malloc函数上使用数组方式错误做LU分解。
的方式来使用矩阵[X] [Y]高斯温控功能是点
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#pragma警告(禁用:4996)无效高斯(INT N,双**矩阵,双** L,双** U,双** ANS);INT主要(无效){ INT I,N; //
INT X,Y; //行x,y行
双**矩阵; //定义矩阵[X] [Y]
双** L,
双** U;
双**答; 的printf(n×n的矩阵键入n \\ n); scanf函数(%d个,&安培; N); 矩阵=的malloc(sizeof的(浮动*)* N); //为int *数x基本结构
如果(矩阵== NULL){printf的(malloc的失败\\ n);出口(1); }
对于(i = 0; I< N;我++)
{
矩阵[I] =的malloc(sizeof的(浮动)* N);
如果(矩阵[I] == NULL){printf的(malloc的失败\\ n);出口(1); }
} //建立矩阵[X] [Y(x的大小)结构 L =的malloc(sizeof的(浮动*)* N); //为int *数x基本结构
如果(L == NULL){printf的(malloc的失败\\ n);出口(1); }
对于(i = 0; I< N;我++)
{
L [i] =的malloc(sizeof的(浮动)* N);
如果(L [我] == NULL){printf的(malloc的失败\\ n);出口(1); }
} //建L [X] [Y(x的大小)结构 U =的malloc(sizeof的(浮动*)* N); //为int *数x基本结构
如果(U == NULL){printf的(malloc的失败\\ n);出口(1); }
对于(i = 0; I< N;我++)
{
U [i] =的malloc(sizeof的(浮动)* N);
如果(U [I] == NULL){printf的(malloc的失败\\ n);出口(1); } } //打造U [X] [Y(x的大小)结构 ANS =的malloc(sizeof的(浮动*)* N); //为int *数x基本结构
如果(ANS == NULL){printf的(malloc的失败\\ n);出口(1); }
对于(i = 0; I< N;我++)
{
答[我] =的malloc(sizeof的(浮动)* N);
如果(ANS [I] == NULL){printf的(malloc的失败\\ n);出口(1); } } //建立ANS [X] [Y(x的大小)结构 的printf(类型的矩阵\\ n的数量);
为(X = 0; X&下; N; X ++){
对于(Y = 0; Y< N; Y ++){
的printf(%d行X%D号码:X + 1,Y + 1);
scanf函数(%LF,&安培;矩阵[X] [Y]);
}
}
/ *
为(X = 0; X&下; N; X ++){
对于(Y = 0; Y< N; Y ++){
的printf(%d行X%D号码:%.2lf \\ n,X + 1,Y + 1,矩阵[X] [Y]); }
}
* /
高斯(N,矩阵,L,U,ANS);/ *对于(i = 0; I< N;我++)
{
免费(矩阵[I]);
}
免费(矩阵); //自由矩阵对于(i = 0; I< N;我++)
{
免费(L [我]);
}
免费(L); //游离L-对于(i = 0; I< N;我++)
{
免费(U [I]);
}
免费的(U); //免费ü
* /返回0;}无效高斯(INT N,双**矩阵,双** L,双** U,双** ANS){
INT X,Y;
为(X = 0; X&下; = N; X ++){
如果(矩阵[X] [0]!= 0){
对于(Y = 0; Y< = N; Y ++){
矩阵[X] [Y] =矩阵[X] [Y] /矩阵[X] [0];
L [X] [0] =矩阵[X] [0];
}
}
}
}
-
您的
浮动
矩阵分配内存,但用它作为双击
,由于尺寸浮动
和双击
是不同的,你得到的错误。 -
要经过矩阵的行和colums正确的做法应该是这样的。
为(X = 0; X< N,X ++){
...
}但你写
为(X = 0; X< = N,X ++){
....
}所以你试图访问不存在的一行指数
N
(但最后一行指数N-1
)。
修正code:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;无效高斯(INT N,浮法**矩阵,浮** L,浮** U,浮** ANS);INT主要(无效)
{ INT I,N; //
INT X,Y; //行x,y行
浮**矩阵; //定义矩阵[X] [Y]
浮** L,
浮** U;
浮**答; 的printf(n×n的矩阵键入n \\ n); scanf函数(%d个,&安培; N); 矩阵=的malloc(sizeof的(浮动*)* N); //为int *数x基本结构
如果(矩阵== NULL){printf的(malloc的失败\\ n);出口(1); }
对于(i = 0; I< N;我++)
{
矩阵[I] =的malloc(sizeof的(浮动)* N);
如果(矩阵[I] == NULL){printf的(malloc的失败\\ n);出口(1); }
} //建立矩阵[X] [Y(x的大小)结构 L =的malloc(sizeof的(浮动*)* N); //为int *数x基本结构
如果(L == NULL){printf的(malloc的失败\\ n);出口(1); }
对于(i = 0; I< N;我++)
{
L [i] =的malloc(sizeof的(浮动)* N);
如果(L [我] == NULL){printf的(malloc的失败\\ n);出口(1); }
} //建L [X] [Y(x的大小)结构 U =的malloc(sizeof的(浮动*)* N); //为int *数x基本结构
如果(U == NULL){printf的(malloc的失败\\ n);出口(1); }
对于(i = 0; I< N;我++)
{
U [i] =的malloc(sizeof的(浮动)* N);
如果(U [I] == NULL){printf的(malloc的失败\\ n);出口(1); } } //打造U [X] [Y(x的大小)结构 ANS =的malloc(sizeof的(浮动*)* N); //为int *数x基本结构
如果(ANS == NULL){printf的(malloc的失败\\ n);出口(1); }
对于(i = 0; I< N;我++)
{
答[我] =的malloc(sizeof的(浮动)* N);
如果(ANS [I] == NULL){printf的(malloc的失败\\ n);出口(1); } } //建立ANS [X] [Y(x的大小)结构 的printf(类型的矩阵\\ n的数量);
为(X = 0; X&下; N; X ++){
对于(Y = 0; Y< N; Y ++){
的printf(%d行X%D号码:X + 1,Y + 1);
scanf函数(%F,&安培;矩阵[X] [Y]);
}
} 高斯(N,矩阵,L,U,ANS); 返回0;
}无效高斯(INT N,浮法**矩阵,浮** L,浮** U,浮** ANS){
INT X,Y;
为(X = 0; X&下; N; X ++){
如果(矩阵[X] [0]!= 0){
对于(Y = 0; Y< N; Y ++){
矩阵[X] [Y] =矩阵[X] [Y] /矩阵[X] [0];
L [X] [0] =矩阵[X] [0];
}
}
}
}
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];
}
}
}
}
You allocate memory for
float
matrix, but then use it asdouble
, since sizes offloat
anddouble
are different you got errors.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 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); }
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];
}
}
}
}
这篇关于阵列由malloc函数上的LU分解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!