Hoán vị hai cột trên ma trận

5 1.8K 6
Hoán vị hai cột trên ma trận

Đang tải... (xem toàn văn)

Thông tin tài liệu

Hoán vị hai cột ma trận #include #include #include #define MAX 100 void NhapMang(int a[][MAX], int &dong, int &cot) { //Nhập số dòng { printf("\nNhap vao so dong: "); // Cách tà đạo: scanf("dong =%d",&dong); // Lúc nhập phải viết thêm scanf("%d",&dong); if(dong < || dong > MAX) { printf("\nSo dong khong hop le Xin kiem tra lai!"); } }while(dong < || dong > MAX); //Nhập số cột { printf("\nNhap vao so cot: "); chữ ( dong = ) khung console scanf("%d",&cot); if(cot < || cot > MAX) { printf("\nSo cot khong hop le Xin kiem tra lai!"); } }while(cot < || cot > MAX); for(int i = 0; i < dong; i++) { for(int j = 0; j < cot; j++) { printf("\nNhap a[%d][%d] = ", i, j); scanf("%d", &a[i][j]); } } } void XuatMang(int a[][MAX], int dong, int cot) { for(int i = 0; i < dong; i++) { for(int j = 0; j < cot; j++) { printf("%4d", a[i][j]); } printf("\n\n"); } } void HoanVi(int &a, int &b) { int temp = a; a = b; b = temp; } void HoanVi2Cot(int a[][MAX], int dong, int cot, int cot1, int cot2) { if((cot1 >= && cot1 < cot) && (cot2 >= && cot2 < cot)) { for(int i = 0; i < dong; i++) { HoanVi(a[i][cot1], a[i][cot2]); } } } int main() { int a[MAX][MAX], dong, cot; int cot1, cot2; NhapMang(a, dong, cot); XuatMang(a, dong, cot); { printf("\nNhap cot 1: "); scanf("%d", &cot1); if(cot1 < || cot1 >= cot) { printf("\nCot khong hop le Xin Nhap lai!"); } }while(cot1 < || cot1 >= cot); { printf("\nNhap cot 2: "); scanf("%d", &cot2); if(cot2 < || cot2 >= cot) { printf("\nCot khong hop le Xin Nhap lai!"); } }while(cot2 < || cot2 >= cot); HoanVi2Cot(a, dong, cot, cot1, cot2); printf("\nMa tran sau hoan vi cot %d va cot %d \n", cot1, cot2); XuatMang(a, dong, cot); getch(); return 0; }

Ngày đăng: 22/10/2016, 19:58

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan