Tboy Cá voi
Tổng số bài gửi : 178 Cảm ơn : 413 Danh vọng : 4 Join date : 03/01/2011 Age : 32 Đến từ : TPHCM
| Tiêu đề: Chèn vào mảng- (Mảng 2 chiều- Biến đổi trên mảng) Sat Feb 12, 2011 5:31 pm | |
| | Sat Feb 12, 2011 5:31 pm Chèn vào mảng- (Mảng 2 chiều- Biến đổi trên mảng) | | | - Code:
-
//CHEN DONG, COT VAO MA TRAN #include <stdio.h> #include <conio.h> #include <ctype.h> #define MAX 101 void NhapMT(int MT[][MAX],int Dong,int Cot); void XuatMT(int MT[][MAX],int Dong,int Cot); void Nhapmang(int A[],int N); void Xuatmang(int A[],int N); void Chendong(int MT[][MAX],int &Dong,int Cot,int A[],int K); void Chencot(int MT[][MAX],int Dong,int &Cot,int A[],int K); void main() { int matran[MAX][MAX],a[MAX],dong,cot,n,k; char m; //*****************************NHAP MA TRAN************************************* printf("\tNHAP MA TRAN\n"); do { printf("\nNhap vao so dong va cot ma tran: "); scanf("%d%d",&dong,&cot); }while(dong<1||cot<1||dong>MAX-1||cot>MAX-1); NhapMT(matran,dong,cot); printf("Ma tran vua nhap:\n"); XuatMT(matran,dong,cot); //*****************************CHEN DONG**************************************** printf("\n****\tCHEN THEM MOT DONG VAO MA TRAN\t****\n\n"); printf("Nhap cac phan tu cua dong can chen:\n"); Nhapmang(a,cot); printf("Dong vua nhap: "); Xuatmang(a,cot); do { printf("Chen vao dong thu: "); scanf("%d",&k); }while(k<1||k>dong+1); Chendong(matran,dong,cot,a,k-1); printf("Sau khi chen\n"); XuatMT(matran,dong,cot); getch(); //***************************CHEN COT******************************************* printf("\n****\tCHEN THEM MOT COT VAO MA TRAN\t****\n\n"); //=====CHON MA TRAN do { printf("Ban muon su dung lai ma tran tren hay khong? (y/n) "); m=getchar(); m=toupper(m); }while((m!='Y')&&(m!='N')); if(m=='N') { printf("\n\tNHAP MA TRAN\n"); do { printf("\nNhap vao so dong va cot ma tran: "); scanf("%d%d",&dong,&cot); }while(dong<1||cot<1||dong>MAX-1||cot>MAX-1); NhapMT(matran,dong,cot); printf("Ma tran vua nhap:\n"); XuatMT(matran,dong,cot); } //======THUC HIEN CHEN printf("\nNhap cac phan tu cua cot can chen:\n"); Nhapmang(a,dong); printf("Cot vua nhap: "); Xuatmang(a,dong); do { printf("Chen vao cot thu: "); scanf("%d",&k); }while(k<1||k>cot+1); Chencot(matran,dong,cot,a,k-1); printf("Sau khi chen\n"); XuatMT(matran,dong,cot); getch(); } void NhapMT(int MT[][MAX],int Dong,int Cot) { for(int i=0;i<Dong;i++) for(int j=0;j<Cot;j++) { printf("A[%d][%d]= ",i+1,j+1); scanf("%d",&MT[i][j]); } } void XuatMT(int MT[][MAX],int Dong,int Cot) { for(int i=0;i<Dong;i++) { for(int j=0;j<Cot;j++) printf("%5d",MT[i][j]); printf("\n"); } } void Nhapmang(int A[],int N) { for(int i=0;i<N;i++) { printf("A[%d]= ",i+1); scanf("%d",&A[i]); } } void Xuatmang(int A[],int N) { for(int i=0;i<N;i++) printf("%d ",A[i]); printf("\n"); } void Chendong(int MT[][MAX],int &Dong,int Cot,int A[],int K) { for(int i=Dong-1;i>=K;i--) for(int j=0;j<Cot;j++) MT[i+1][j]=MT[i][j]; for(int d=0;d<Cot;d++) MT[K][d]=A[d]; Dong++; } void Chencot(int MT[][MAX],int Dong,int &Cot,int A[],int K) { for(int i=Cot-1;i>=K;i--) for(int j=0;j<Dong;j++) MT[j][i+1]=MT[j][i]; for(int d=0;d<Dong;d++) MT[d][K]=A[d]; Cot++; } Tboy
| | | | | |
|