"stdio.h"
#include
"stdlib.h"
void
main
()
{
int
m,l,p,q
scanf("%d%d",&m,&l)
printf("输入第二个矩阵的维数p和q:\n")
scanf("%d%d",&p,&q)
while
(l!=p)
{
printf("两个矩阵不匹配,不能进行相乘运算,请重新输入...\n\n")
printf("输入第一个矩阵的维数m和l:\n")
scanf("%d%d",&m,&l)
printf("输入第二个矩阵的维数p和q:\n")
scanf("%d%d",&p,&q)
}
double
a[200][200]={0},
b[200][200]={0},sum=0
int
i,j,k=0
for
(i=0i<mi++)
{
for
(j=0j<lj++)
{
a[i][j]=(double)rand()/RAND_MAX
}
}
printf("随机矩阵a是:\n")
for
(i=0i<mi++)
{
for
(j=0j<lj++)
{
printf("%f
",a[i][j])
}
printf("\n")
}
//cout<<"输入一个3*2的矩阵:"<<endl
for
(i=0i<pi++)
{
for
(j=0j<qj++)
{
b[i][j]=(double)rand()/RAND_MAX
}
}
printf("随机矩阵b是:\n")
for
(i=0i<pi++)
{
for
(j=0j<qj++)
{
printf("%f
",b[i][j])
}
printf("\n")
}
printf("ab相乘结果为:\n")
for
(i=0i<mi++)
{
while
(k<q)
{
for
(j=0j<qj++)
{
sum
=
a[i][j]*b[j][k]+sum
}
printf("%f
",sum)
k++
sum=0
}
k=0
printf("\n")
}
}
如果不满足你要求,自己再改一下吧
#define MAX 50#define M MAX
#define N MAX
#define T MAX
#define S MAX
int Mult(double a[][N],int m,int n,double b[][T]int s,int t,double c[][T]) {
int i,j,k
if(n != s) {
printf("两矩阵相乘,左矩阵的列数与右矩阵的行数必须相等。\n")
return 0
}
int c
tmp.m_Mat = new double *[tmp.m_Rows]
for(int i = 0i < tmp.m_Rowsi++) tmp.m_Mat[i] = new double[tmp.m_Cols]
for(i = 0 i < m ++i) {
for(j = 0 j < n ++j) {
c[i][j] = 0
for(k = 0 k < t ++k)
c[i][j] += c[i][k] * c[k][j]
}
}
return 1
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)