零基础学c++(矩阵相乘)

<span style="font-size:18px;">
#include<iostream>
#include<iomanip>
using namespace std;
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];
int MultiMatrix(int a[][4], int arow, int acol,
	int b[][5], int brow, int bcol,
	int c[][5], int crow, int ccol);     //函数声明
void main()
{
	if (MultiMatrix(a, 3, 4, b, 4, 5, c, 3, 5))
	{
		cout << "illegal matrix multipy.\n";
		return;
	}
	for (int i = 0; i < 3; i++)          //输出矩阵乘法的结果
	{
		for (int j = 0; j < 5; j++)
			cout << setw(5) << c[i][j];
		cout << endl;
	}
}


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

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

	return 0;
}
</span>

      该程序先定义两个全局数组,然后调用矩阵乘法函数,对应的,矩阵乘法函数的参数是三个二维数组表示的矩阵,分别有两个行列式。

      矩阵乘法函数中,先对行列值进行校验,不符要求返回出错信息。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值