Nhập,xuất mảng 2 chiều; max; max trên từng hàng, từng cột; max trên đường chéo chính, chéo phụ code: Mã: #include <iostream> using namespace std; void main() { //Nhap int a[10][10]; int m,n,i,j; cout<<"Nhap so hang, so cot"<<endl; cin>>m>>n; cout<<"Nhap gia tri phan tu"<<endl; for(i=0;i<m;i++) for(j=0;j<n;j++) { cout<<"a["<<i+1<<"]["<<j+i<<"]="; cin>>a[i][j]; } //Xuat for(i=0;i<m;i++) { cout<<endl; for(j=0;j<n;j++) { cout<<a[i][j]<<"\t"; } } cout<<endl; //Phan tu lon nhat int max=a[0][0];int min=a[0][0]; for(i=0;i<m;i++) for(j=0;j<n;j++) { if(max<a[i][j]) max=a[i][j]; if(min>a[i][j]) min=a[i][j]; } cout<<"Max="<<max<<endl; cout<<"Min="<<min<<endl; //Phan tu lon nhat tren tung hang int maxhang[10]; for(i=0;i<m;i++) { maxhang[i]=a[i][0]; for(j=0;j<n;j++) { if(maxhang[i]<a[i][j]) maxhang[i]=a[i][j]; } } for(i=0;i<m;i++) { cout<<"Max hang "<<i+1<<" = "<<maxhang[i]<<endl; } //Phan tu lon nhat tren tung cot int maxcot[10]; for(j=0;j<n;j++) { maxcot[j]=a[0][j]; for(i=0;i<m;i++) { if(maxcot[j]<a[i][j]) maxcot[j]=a[i][j]; } } for(j=0;j<n;j++) { cout<<"Max cot "<<j+1<<" = "<<maxcot[j]<<endl; } //Phan tu lon nhat tren duong cheo chinh if(m==n) { int maxcheo=a[0][0]; for(i=0;i<m;i++) for(j=0;j<n;j++) { if(i==j) { if(maxcheo<a[i][j]) maxcheo=a[i][j]; } } cout<<"Max cheo = "<<maxcheo<<endl; } else cout<<"Khong ton tai duong cheo chinh"<<endl; //Phan tu lon nhat tren duong cheo phu if(m==n) { int maxphu=a[0][0]; for(i=0;i<m;i++) for(j=0;j<n;j++) { if(i==n-1-j) { if(maxphu<a[i][j]) maxphu=a[i][j]; } } cout<<"Max phu = "<<maxphu<<endl; } else cout<<"Khong ton tai duong cheo phu"<<endl; }
Mã: #include <iostream> #define max 100 using namespace std; //Ham nhap void nhap(int a[max][max],int m, int n) { for(int i=0;i<m;i++) for(int j=0;j<n;j++) { cout<<"a["<<i+1<<"]["<<j+1<<"]="; cin>>a[i][j]; } } //Ham xuat void xuat(int a[max][max], int m, int n) { for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cout<<a[i][j]<<"\t"; } cout<<endl; } } //Ham tong void tong(int a[max][max], int b[max][max], int c[max][max], int m, int n) { int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j]+b[i][j]; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<c[i][j]<<"\t"; } cout<<endl; } } //Ham tich void tich(int a[max][max], int b[max][max], int c[max][max], int da,int ca,int cb) { int i,j,k; for(i=0; i<da; i++) for(j=0; j<cb; j++) { c[i][j]=0; for(k=0;k<ca;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } cout<<"Ma tran tich 2 ma tran vua nhap..."<<endl; for(i=0;i<da;i++) { cout<<endl; for(j=0;j<cb;j++) cout<<c[i][j]<<"\t"; } } } //Ham main void main() { int a[max][max],b[max][max],c[max][max],d[max][max]; int da,ca,db,cb; cout<<"Nhap ma tran A"<<endl; cout<<"Nhap so dong, so cot"<<endl; cin>>da>>ca; nhap(a,da,ca); cout<<endl; cout<<"Nhap ma tran B"<<endl; cout<<"Nhap so dong, so cot"<<endl; cin>>db>>cb; nhap(b,db,cb); cout<<endl; cout<<"Hai ma tran vua nhap"<<endl; cout<<"Ma tran A"<<endl; xuat(a,da,ca); cout<<endl; cout<<"Ma tran B"<<endl; xuat(b,db,cb); cout<<endl; //Kiem tra dieu kien tinh tong if(da==db && ca==cb) { cout<<"Tong 2 ma tran"<<endl; cout<<"Ma tran C"<<endl; tong(a,b,c,da,ca); } else cout<<"Khong the cong 2 ma tran"; //Ham tich 2 man tran if(ca==db) { tich(a,b,d, da,ca,db); } else cout<<"Khong ton tai tich 2 ma tran"; }