急急!!求助 C语言矩阵相乘

帮忙写一个程序
要求:利用动态分配数组方式输入并存储A、B两矩阵,并求出两矩阵相乘结果。
谢谢!!!

/* Matrix_main.cpp */
/*********************************************************************************
* 与矩阵乘法相关的一个小程序,用于离散中的二元关系求解
* AUTHOR : 2009-11-14 Created by Yajiang
* REFER :
* NOTE : 不完善的地方在于过多的for()结构,程序执行效率过低。此外,使用了二重指针
* 来创建2D数组,用malloc()来动态分配的内存。
* 编译过程中,发现时间消耗过长(I don't know why!Maybe too many for()...)
*********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
/* #include <process.h> */

void main(void)
{
int col, row, row_s; /* the column & row of the matrix */
int **pM_f = NULL, **pM_s = NULL; /* point to two matrix,they will be multiplied */
int **pM_r = NULL; /* the matrix will store the result */
int i, j, k;

printf("Input column & row of the first matrix:\n(depart with blank): ");
scanf("%d %d", &col, &row);
printf("Input row of the second one:\n(column needn't): ");
scanf("%d", &row_s);

/* ---------------Request storage for process--------------- */
pM_f = (int**)malloc(col * sizeof(int*));
pM_s = (int**)malloc(row * sizeof(int*));
pM_r = (int**)malloc(col * sizeof(int*));
for (i=0; i<col; i++)
{
pM_f[i] = (int*)malloc(row * sizeof(int));
pM_r[i] = (int*)malloc(row_s * sizeof(int));
}
for (i=0; i<row; i++)
{
pM_s[i] = (int*)malloc(row_s * sizeof(int));
}

/* ---------------now store the number to the matrix(a 2D array)--------------- */
srand((unsigned)time(NULL)); /* for creating random number(here is 0 & 1) */
for (i=0; i<col; i++)
{
for (j=0; j<row; j++)
{
*(*(pM_f + i) + j) = rand() % 2; /* evaluate */
}
}
for (i=0; i<row; i++)
{
for (j=0; j<row_s; j++)
{
*(*(pM_s + i) + j) = rand() % 2; /* evaluate */
}
}
for (i=0; i<col; i++) /* here i use this matrix to store the result */
{
for (j=0; j<row_s; j++)
{
*(*(pM_r + i) + j) = 0; /* evaluate & initialize */
}
}

/* ---------------multiply two matrix--------------- */
for (i=0; i<col; i++)
{
for (k=0; k<row_s; k++)
{
for (j=0; j<row; j++) /* attention */
{
*(*(pM_r+i)+k) += (*(*(pM_f+i)+j)) * (*(*(pM_s+j)+k)); /* calculate */
if (*(*(pM_r+i)+k) != 0)
{
*(*(pM_r+i)+k) = 1; /* if the value is more than 1,just let equal to 1 */
}
}
}
}

/* ---------------Result--------------- */
printf("\nThe first Matrix:\n");
for (i=0; i<col; i++)
{
for (j=0; j<row; j++)
{
printf("%d ", *(*(pM_f + i) + j));
}
printf("\n");
}
printf("\nThe second Matrix:\n");
for (i=0; i<row; i++)
{
for (j=0; j<row_s; j++)
{
printf("%d ", *(*(pM_s + i) + j));
}
printf("\n");
}
printf("\nThe result is:\n");
for (i=0; i<col; i++) /* main */
{
for (j=0; j<row_s; j++)
{
printf("%d ", *(*(pM_r + i) + j));
}
printf("\n");
}

/* ---------------Give the storage back to system--------------- */
for (i=0; i<col; i++)
{
if (pM_f[i] != NULL)
{
free(pM_f[i]);
pM_f[i] = NULL;
}
if (pM_r[i] != NULL)
{
free(pM_r[i]);
pM_r[i] = NULL;
}
}
for (i=0; i<row; i++)
{
if (pM_s[i] != NULL)
{
free(pM_s[i]);
pM_s[i] = NULL;
}
}
if (pM_f != NULL)
{
free(pM_f);
pM_f = NULL;
}
if (pM_s != NULL)
{
free(pM_s);
pM_s = NULL;
}
if (pM_r != NULL)
{
free(pM_r);
pM_r = NULL;
}

/* ---------------Exit(it's needn't)--------------- */
/* exit(0); */
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-19
下面这个是我以前写过的,功能有点多了,我就不改了哈
矩阵A是m*n的,矩阵B是n*k的,矩阵C是结果矩阵m*k, C=x*A*B+y*C,可以带系数的。
下面是程序代码,如有不懂,再提问吧。

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
void bmpmatmul(double *a, int lda, double *b, int ldb,
double *c, int ldc, double x, double y, int m,
int k, int n)
{

int i, j, ij;
double temp;
int jldc,jldb,ijlda;
if(y!=1.0)
{
for(i=0; i<m; ++i)
{
for(j=0; j<n; ++j)
{
c[i+j*ldc]*=y;
}
}
}
for(j=0; j<n; ++j)
{
jldc=j*ldc;
jldb=j*ldb;
for(ij=0; ij<k; ++ij)
{
ijlda=ij*lda;
temp=b[ij+jldb]*x;
for(i=0; i<m; ++i)
{
c[i+jldc]+=a[i+ijlda]*temp;
}
}
}
return;
}
int main()
{
double *a,*b,*c;
int m,n,k;
int i,j;
while(scanf("%d%d%d",&m,&n,&k)!=EOF)
{
a=NULL;
b=NULL;
c=NULL;
a=(double*)malloc((m*n)*sizeof(double));
b=(double*)malloc((n*k)*sizeof(double));
c=(double*)malloc((m*k)*sizeof(double));
for(i=0; i<m; ++i)
{
for(j=0; j<n; ++j)
{
scanf("%lf",&a[j*m+i]);
}
}
for(i=0; i<n; ++i)
{
for(j=0; j<k; ++j)
{
scanf("%lf",&b[j*n+i]);
}
}
for(i=0; i<m; ++i)
{
for(j=0; j<k; ++j)
{
c[j*m+i]=0.0;
}
}
bmpmatmul(a,m,b,n,c,m,1,1,m,n,k);
free(a);
free(b);
free(c);
}
return 0;
}
第2个回答  2011-03-20
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
main()
{
int **a,**b,**c;
int am=0,an=0,bm=1,bn=1,i,j,k,cm,cn;
while(an!=bm)
{
printf("ÊäÈëa¾ØÕó³ß´ç\n");
scanf("%d%d",&am,&an);
printf("ÊäÈëb¾ØÕó³ß´ç\n");
scanf("%d%d",&bm,&bn);
if(an!=bm)printf("ÊäÈë²»ºÏ·¨!\n");
}
a=(int**)malloc(am*sizeof(int*));
for(i=0;i<am;i++)a[i]=(int*)malloc(an*sizeof(int));
b=(int**)malloc(bm*sizeof(int*));
for(i=0;i<bm;i++)b[i]=(int*)malloc(bn*sizeof(int));
printf("ÊäÈëaÔªËØ\n");
for(i=0;i<am;i++)
{
printf("µÚ%dÐÐ:",i+1);
for(j=0;j<an;j++)scanf("%d",*(a+i)+j);
}
printf("ÊäÈëbÔªËØ\n");
for(i=0;i<bm;i++)
{
printf("µÚ%dÐÐ:",i+1);
for(j=0;j<bn;j++)scanf("%d",*(b+i)+j);
}
cm=am;
cn=bn;
c=(int**)malloc(cm*sizeof(int*));
for(i=0;i<cm;i++)c[i]=(int*)malloc(cn*sizeof(int));
for(i=0;i<cm;i++)
for(j=0;j<cn;j++)
c[i][j]=0;
for(i=0;i<cm;i++)
{
for(j=0;j<cn;j++)printf(" %d ",c[i][j]);
printf("\n");
}
getch();
for(i=0;i<cm;i++)
for(j=0;j<cn;j++)
for(k=0;k<an;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
for(i=0;i<cm;i++)
{
for(j=0;j<cn;j++)printf(" %d ",c[i][j]);
printf("\n");
}

return 0;
}
相似回答