现在的位置: 首页 > 综合 > 正文

第四章作业(二)

2014年11月11日 ⁄ 综合 ⁄ 共 783字 ⁄ 字号 评论关闭

3. 编程定义一个4X5矩阵和5X3矩阵,并计算它们的积。

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
	
		int f[4][5];
		int s[5][3];
		int i,j,k,l;
		cout<<"请输入第一个矩阵:"<<endl;
		for(i=0;i<4;i++)
		for(j=0;j<5;j++)
		{
			cout<<"a["<<i<<"]["<<j<<"]=";
			cin>>f[i][j];
		}
		cout<<"请输入第二个矩阵:"<<endl;
		for(k=0;k<5;k++)
		for(l=0;l<3;l++)
		{
			cout<<"b["<<k<<"]["<<l<<"]=";
			cin>>s[k][l];
		}
	    cout<<"\n第一个矩阵为:"<<endl;
		for(i=0;i<4;i++)
		{
			for(j=0;j<5;j++)
				cout<<setw(6)<<f[i][j];
			cout<<endl;
		}
		cout<<"\n第二个矩阵为:"<<endl;
		for(k=0;k<5;k++)
		{
			for(l=0;l<3;l++)
				cout<<setw(6)<<s[k][l];
			cout<<endl;
		}
		cout<<"这两个矩阵的积为:"<<endl;
		int z=0,x,y,sum;
		int t[4][3];
		for(x=0;x<4;x++)
		{
			for(y=0;y<3;y++)
			{
				t[x][y]=0;			
				for(z=0;z<5;z++)
				{
				 sum=f[x][z]*s[z][y];
				 t[x][y]=sum+t[x][y];
				}
			     cout<<setw(6)<<t[x][y];
			}
			cout<<endl;
		}
		return 0;
}

运行的结果为




老师这些作业都是我自己做的,对不起 我那么迟才交作业,但是这些都是我花费好几个小时才完成的。绝对没有抄别人。后面那题关于魔方的想了很久也想不出来。

【上篇】
【下篇】

抱歉!评论已关闭.