u012343958 于 2013.10.22 19:40 提问

#include"stdio.h"
#include"stdlib.h" //动态分配的函数需要包含该头文件
//void Matrix(double ,double **,double **,int ,int ,int );
int main()
{
int i,j,k,mWidth,mHeight,nWidth,nHeight;
double **m,
n,**p; //定义指向指针的指针，即表示二维数组名，只定义了分配相关变量，其他变量自己定义
printf("Please input width and height of matrix m\n");
scanf("%d%d",&mWidth,&mHeight);
printf("Please input width and height of matrix n\n");
scanf("%d%d",&nWidth,&nHeight);
m=(double**)calloc(mWidth,sizeof(double*));
n=(double**)calloc(nWidth,sizeof(double*));
p=(double**)calloc(mWidth,sizeof(double*)); //分配w个指针型的空间，即第一维
if(mHeight==nWidth)
{
for(i=0;i<mWidth;i++)
{
m[i]=(double*)calloc(mHeight,sizeof(double));
p[i]=(double*)calloc(nHeight,sizeof(double));//分配h个int型的空间，即第二维
}

for(i=0;i<nWidth;i++)
n[i]=(double*)calloc(nHeight,sizeof(double));
printf("输入m的矩阵\n");
for(i=0;i<mWidth;i++)
{
for(j=0;j<mHeight;j++)
scanf("%ld",&m[i][j]);
}
printf("%f%f",m[0][0],m[0][1]);
printf("输入n的矩阵\n");
for(i=0;i<nWidth;i++)
for(j=0;j<nHeight;j++)
scanf("%ld",&n[i][j]);
}
for(i=0;i<mWidth;i++)
for(j=0;j<nHeight;j++)
{
double sum=0;
for(k=0;k<mHeight;k++)
{ double a=m[i][k];//*(p[i]+j)
double b=n[k][j];
sum+=a*b;
printf("%f\n",sum);
}
p[i][j]=sum;
printf("%f\n",sum);
}
for(i=0;i<mWidth;i++)
for(j=0;j<nHeight;j++)
printf("%f\n",&p[i][j]);
for(i=0;i<mWidth;i++)
{
free(m[i]);//释放第一维指针
free(p[i]);
}
for(i=0;i<nWidth;i++)
{
free(n[i]);
}
free(m);//释放第二维指针
free(n);
free(p);
return 0;
}
//矩阵相乘函数
/*void Matrix(double **M,double **N,double **P,int mwidth,int mheight,int nheight)
{ int i;
int j;
int k;
for(i=0;i<mwidth;i++)
for(j=0;j<nheight;j++)
{
double sum=0;
for(k=0;k<mheight;k++)
{ double a=M[i][k];//*(p[i]+j)
double b=N[k][j];
sum+=a*b;
}
P[i][j]=sum;
}
}*/