C++ 自由定义矩阵相乘

自由定义矩阵相乘问题
可以解决大部分问题
代码存在问题:定义了二维数组的最大规模,并不是可变数组

#include<iostream>
using namespace std;
void main()
{
	int a[10][10]={0},b[10][10]={0},c[10][10]={0};
	int i,j,m,n,k,x,y;
	cout<<"please input the matrex_a that is m*n and matrex_b that is n*m"<<endl;
	cout<<"please input m and n"<<endl;
	cout<<"m=";
	cin>>m;
	cout<<"n=";
	cin>>n;
	cout<<"please input "<<m*n<<" numbers as matrix_a"<<endl;
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			cin>>a[i][j];
    cout<<"the matrix_a is :"<<endl;
	for(i=0;i<m;i++){
		for(j=0;j<n;j++)
			cout<<a[i][j]<<" ";
		cout<<endl;
	}

    cout<<"please input "<<n*m<<" numbers as matrix_b"<<endl;
    for(i=0;i<n;i++)
		for(j=0;j<m;j++)
			cin>>b[i][j];
    cout<<"the matrix_b is :"<<endl;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
			cout<<b[i][j]<<" ";
		cout<<endl;
	}


    for(x=0;x<m;x++)
	{
		for(y=0;y<m;y++)
		{
			c[x][y]=0;
			for(k=0;k<n;k++)
			{
				c[x][y]+=a[x][k]*b[k][y];
			}
		}

	}
	cout<<"the result of a*b is "<<endl;
    for(x=0;x<m;x++)
	{
		for(y=0;y<m;y++)
			cout<<c[x][y]<<" ";
		cout<<endl;
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值