#include "iostream.h"
#include "stdlib.h"
#include "time.h"
#include "iomanip.h"
void main()
{
int a[4][4],b[4][4],c[4][4],i,j,max,imax(0),jmax(0),t,s(0);
srand(time(NULL)) ;
cout<<"矩阵A内容如下:"<<endl;
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
{a[i][j]=rand()%41+30;
cout<<a[i][j]<<" ";}
cout<<endl;}
cout<<"矩阵B内容如下:"<<endl;
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
{b[i][j]=rand()%41+30;
cout<<b[i][j]<<" ";}
cout<<endl;}
cout<<"矩阵C内容如下:"<<endl;
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
{c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<" ";}
cout<<endl;}
max=c[0][0];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(c[i][j]>max)
{max=c[i][j];
imax=i;
jmax=j;}
cout<<"max="<<max<<" "<<"imax="<<imax<<" "<<"jmax="<<jmax<<endl;
cout<<"矩阵A主对角线以下元素内容如下:"<<'\n';
for(i=0;i<4;i++)
{for(j=0;j<=i;j++)
cout<<a[i][j]<<" ";
cout<<endl;}
cout<<"矩阵B主对角线以上元素内容如下:"<<'\n';
for(i=0;i<4;i++)
{for(j=i;j<4;j++)
cout<<b[i][j]<<" ";
cout<<endl<<setw(3*i+5);}
for(j=0;j<4;j++)
{t=a[0][j];
a[0][j]=a[2][j];
a[2][j]=t;}
cout<<"改变后的矩阵A内容如下:"<<'\n';
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
cout<<a[i][j]<<" ";
cout<<endl;}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i==j||i+j==3)
s=s+a[i][j];
cout<<"对角线元素之和="<<s<<endl;
}
评论0