c语言二维数组输出的一个小程序,打印不对呢,求大侠指导啊~~先谢过啦
#include <stdio.h>#include <stdlib.h>
#include <math.h>
#include <time.h>
#define pi 3.1415926
void cholesky (double **a,int n,double **l){
int i,j,c,d,k;
double s,g;
for (c=0;c<n;c++) {
for (d=0;d<n;d++){
if (c==d){s=0.;
for(k=0;k<c;k++){s+=l[c][k]*l[c][k];}
l[c][d]=sqrt(a[c][c]-s);}
if (c>d){s=0.;
for(k=0;k<d;k++){s+=l[c][k]*l[d][k];}
g=a[c][d]-s;
l[c][d]=g/l[d][d];}
if (c<d)l[c][d]=0. ;};}
;}
double mult_mat_vec(double **l,double *u,int n){
double *v;
int i,j;
for (i=0;i<n;i++){for (j=0;j<n;j++)v[i]+=l[i][j]*u[j];}
;}
double gussien(){
double u,v,s,z;
u=rand()/(RAND_MAX+1.0);
v=rand()/(RAND_MAX+1.0);
s=-2*log(u);
z=sqrt(s)*cos(2*pi*v);
return z;
}
int main (void){
double **a,**l,*u,*v;
int n,i,j,c,d,e,h,min;
srand(time(NULL));
printf ("donner la taille de matrice,SVP:\n");
scanf("%d",&n);
u=malloc(n*sizeof(double));
for (e=0;e<n;e++)u[e]=gussien();
for (e=0;e<n;e++)printf("%lf ",u[e]);
printf ("\n\n");
a=malloc(n*sizeof(double*));
for (i=0;i<n;i++){a[i]=malloc(n*sizeof(double));}
for (i=0;i<n;i++){for (j=0;j<n;j++){min=i+1;if (j<i)min=j+1;a[i][j]=2.*((double)min/(double)(i+j+2.));}}
for (i=0;i<n;i++){for (j=0;j<n;j++){printf ("%lf ",a[i][j]);if((j+1)==n){printf("\n");}}}/*if 里面的判断语句一定要是==才能判断*/
printf ("\n\n");
l=malloc(n*sizeof(double*));
for (c=0;c<n;c++){l[c]=malloc(n*sizeof(double));}
cholesky (a,n,l);
for (c=0;c<n;c++){for (d=0;d<n;d++){printf ("%lf ",l[c][d]);if((d+1)==n){printf("\n");};};}
v=malloc(n*sizeof(double));
mult_mat_vec(l,u,n);
for (h=0;h<n;h++)printf ("%lf ",v[h]);
return 0;
}
这是个算矩阵乘法生成以a为相关矩阵的正态分布的程序;
开始都对,就在最后一步的程序上出了一个问题:
double mult_mat_vec(double **l,double *u,int n){
double *v;
int i,j;
for (i=0;i<n;i++){for (j=0;j<n;j++)v[i]+=l[i][j]*u[j];}
;}
对应的输出程序是这个样子的
v=malloc(n*sizeof(double));
mult_mat_vec(l,u,n);
for (h=0;h<n;h++)printf ("%lf ",v[h]);
开始的时候都能输出就在这里输出不出来了,程序还不纠错,直接就结束任务了,呜呜,急死我了,求帮忙指导啊,谢谢啦,刚开始学,代码实在比较恶心,多包涵哈