| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1323 人关注过本帖
标题:[原创]两矩阵相乘,希望对大家有用
取消只看楼主 加入收藏
hlstone
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2005-12-13
收藏
 问题点数:0 回复次数:0 
[原创]两矩阵相乘,希望对大家有用


#include <iostream.h>
#include <iomanip.h>

int a[3][4]={{ 5, 7, 8, 2},
{-2, 4, 1, 1},
{ 1, 2, 3, 4}};

int b[4][5]={{4,-2, 3, 3, 9},
{4, 3, 8,-1, 2},
{2, 3, 5, 2, 7},
{1, 0, 6, 3, 4}};

int c[3][5];

bool MultiMatrix(int a[3][4], int arow, int acol,
int b[4][5], int brow, int bcol,
int c[3][5], int crow, int ccol); //函数声明

void main()
{
if(MultiMatrix(a,3,4, b,4,5, c,3,5)){
cout <<"illegal matrix multiply.\n";
return;
}

for(int i=0; i<3; i++){ //输出矩阵乘法的结果
for(int j=0; j<5; j++)
cout <<setw(5) <<c[i][j];
cout <<endl;
}
}

bool MultiMatrix(int a[3][4], int arow, int acol,
int b[4][5], int brow, int bcol,
int c[3][5], int crow, int ccol)
{
if(!((acol==brow)&&(crow==arow)&&(ccol==bcol))) //正确性检查
return 1;

for(int i=0; i<crow; i++) //行
for(int j=0; j<ccol; j++){ //列
c[i][j]=0; //此句可以省略,因为c是全局数组
for(int n=0; n<acol; n++) //求一个元素
c[i][j]+= a[i][n] * b[n][j];
}

return 0;
}

搜索更多相关主题的帖子: int 矩阵 相乘 include bool 
2005-12-13 23:50
快速回复:[原创]两矩阵相乘,希望对大家有用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028598 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved