注册 登录
编程论坛 C++教室

求助c++4*4矩阵积!还是不行。。

jinking618 发布于 2013-03-30 16:42, 488 次点击
#include <iostream>
#include <iomanip>
//#include <malloc.h>
using namespace std;
#define N 3

void R(int M[4][4])
{
int i,j;
for(i=0;i<4;i++)
 for(j=0;j<4;j++)cin>>M[i][j];
}

void W(int M[4][4])
{int i,j;for(i=0;i<4;i++)
{
for(j=0;j<4;j++)cout<<setw(4)<<M[i][j];
cout<<endl;
}
}

void Add(int M1[4][4],int M2[4][4],int M3[4][4])//,int M4[4][4]
{int i,j;
for(i=0;i<4;i++)
 for(j=0;j<4;j++)
M3[i][j]=M1[i][j]*M2[i][j];
}



void main()
{
 int M1[4][4],M2[4][4],M3[4][4];//M4[4][4];
 //cin>>
 //cout<<"Case 1"<<
 R(M1);R(M2);Add(M1,M2,M3);W(M3);//输出矩阵
}

/*
1 0 0 0
1 0 1 0
0 0 0 1
0 1 0 1

0 1 0 1
1 0 1 1
1 1 0 0
0 0 0 1





#include <iostream>
#include <iomanip>
#include <malloc.h>
using namespace std;
#define N 3

void R(int M[4][4])
{
int i,j;
for(i=0;i<4;i++)
 for(j=0;j<4;j++)
cin>>M[i][j];
}


void W(int M[4][4])
{int i,j;for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
cout<<setw(4)<<M[i][j];
cout<<endl;
}
}


void malloc(int M1[4][4],int M2[4][4],int M3[4][4])
{int i,j;
for(i=0;i<4;i++)
 for(j=0;j<4;j++)
M3[i][j]=M1[i][j] * M2[i][j];
}



void main()
{
 int M1[4][4],M2[4][4],M3[4][4];

int T,i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
M3[i][j]=M1[i][j] * M2[i][j];
cin>>T;
 cin>>M1[i][j]>>M2[i][j];
 cout<<"Case 1"<<M3[i][j];
 //R(M1);R(M2);Add(M1,M2,M3);W(M3);//输出矩阵
}

/*
1 0 0 0
1 0 1 0
0 0 0 1
0 1 0 1

0 1 0 1
1 0 1 1
1 1 0 0
0 0 0 1
*/
*/
2 回复
#2
不要脸的猫2013-03-30 21:05
void Add(int M1[4][4],int M2[4][4],int M3[4][4])
{
int i,j,k;
for(i=0;i<4;i++)
for(k=0;k<4;k++)
for(j=0;j<4;j++)
M3[i][k]+=M1[i][j]*M2[j][k];
for(i=0;i<4;i++)
for(j=0;j<4;j++)//输出M3[][]
{
printf("%d ",M3[i][j]);
if(!((j+1)%4))
putchar('\n');
}
}
#3
jinking6182013-03-30 21:51
回复 2楼 不要脸的猫
这不是求和的?大神有求积的不
1