#include<conio.h> #include<iostream.h> #include<stdlib.h> #include<time.h> #define M 30 #define N 50 void nhapmang (int a[][N],int d,int c) { srand(time(NULL)); for(int i=0;i<d;i++) for(int j=0;j<c;j++) a[i][j]=rand()%100; } void xuatmang(int a[][N],int d,int c) { for(int i=0;i<d;i++){ for(int j=0;j<c;j++) cout<<a[i][j]<<"\t"; cout<<endl;} } void tongcl(int a[][N],int d,int c) //Tong cac so chan le { float tc,tl,t,k; t=k=0; for(int i=0;i<d;i++) for(int j=0;j<c;j++) if(a[i][j]%2==0) {tc+=a[i][j]; t++;} else {tl+=a[i][j]; k++;} cout<<"Tong cac so chan la: "<<tc<<" Co "<<t<<" so chan."; cout<<"\nTong cac so le la: "<<tl<<" Co "<<k<<" so le."; } void timmax(int a[][N],int d,int c) //Tim so lon nhat { int max; max=a[0][0]; for(int i=0;i<d;i++) for(int j=0;j<c;j++) if(a[i][j]>max) max=a[i][j]; cout<<"So lon nhat la: "<<max; } void timmin(int a[][N],int d,int c) //Tim so nho nhat { int min; min=a[0][0]; for(int i=0;i<d;i++) for(int j=0;j<c;j++) if(a[i][j]<min) min=a[i][j]; cout<<"So nho nhat la: "<<min; }
void duongcp(int a[][N],int d,int c) //Tinh tong duong cheo phu { int s; s=0; for(int i=0;i<d;i++) for(int j=0;j<c;j++) if(j==d-1-i) s+=a[i][j]; if(c==d) cout<<"Tong duong cheo phu la: "<<s; else cout<<"Khong co DCP vi day ko la MT vuong."; } void tongduongcheo(int a[][N],int d,int c) //Tinh tong duong cheo chinh { float k; for(int i=0;i<c;i++) k+=a[i][i]; if(d==c) cout<<"Tong duong cheo chinh la: "<<k; else cout<<"Khong co DCC vi so dong # so cot."; } void timxtrongmang(int a[][N],int d,int c,int x) //Tim so lon nhat { int k,l,f; f=0; for(int i=0;i<d;i++) for(int j=0;j<c;j++) if(a[i][j]==x) { k=i+1; l=j+1; f=1; } if(f==1) cout<<"So co trong mang; Vi tri cua so do la: Dong "<<k<<" Cot "<<l; else cout<<"So ko co trong mang."; } void trichcacsochan(int a[][N],int d,int c) // lay so chan ra { cout<<"Trong mang co cac so chan: "; for(int i=0;i<d;i++) for(int j=0;j<c;j++) if(a[i][j]%2==0) cout<<a[i][j]<<"\t"; cout<<endl; } void trichcacsole(int a[][N],int d,int c) // lay so le ra { cout<<"Trong mang co cac so chan: "; for(int i=0;i<d;i++) for(int j=0;j<c;j++) if(a[i][j]%2!=0) cout<<a[i][j]<<"\t"; cout<<endl; } void main() { int a[M][N],d,c,x,m,n; cout<<"Nhap so dong: "; cin>>d; cout<<"Nhap so cot: "; cin>>c; nhapmang(a,d,c); cout<<"Mang da nhap la: \n"; xuatmang(a,d,c); cout<<"\nNhan 1 de tim so trong mang.\n"; cout<<"Nhan 2 de tim so lon nhat.\n"; cout<<"Nhan 3 de tinh tong duong cheo.\n"; cout<<"Nhan 4 de tong cac so chan le.\n"; cout<<"Nhan 5 de tim so nho nhat.\n"; cout<<"Nhan 6 de trich cac so chan.\n"; cout<<"Nhan 7 de trich cac so le.\n"; cin>>m; switch(m) { case 1: cout<<"\nNhap so can tim: "; cin>>x; timxtrongmang(a,d,c,x); break; case 2: timmax(a,d,c); break; case 3: cout<<"Nhan 1 tinh duong cheo chinh\n"; cout<<"Nhan 2 de tinh duong cheo phu."; cin>>n; switch(n) { case 1: tongduongcheo(a,d,c); break; case 2: duongcp(a,d,c); break; } break; case 4: tongcl(a,d,c); break; case 5: timmin(a,d,c); break; case 6: trichcacsochan(a,d,c); break; case 7: trichcacsole(a,d,c); break; default: break; } getche(); }