Last active
August 29, 2015 14:14
-
-
Save tandat2209/756cd774d644aa2c758c to your computer and use it in GitHub Desktop.
Mảng xoắn ốc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <conio.h> | |
const int max=50; | |
//ham nhap mang | |
void input(int *a, int *b, int &hang, int &cot){ | |
int dem=0; | |
printf("Nhap so hang: "); scanf("%d",&hang); | |
printf("Nhap so cot: "); scanf("%d",&cot); | |
for(int i=0;i<hang;i++){ | |
for(int j=0;j<cot;j++) { | |
printf("a[%d,%d]: ",i+1,j+1); | |
scanf("%d",a+max*i+j); | |
*(b+(dem++))=*(a+max*i+j); //dang dung con tro tuong duong voi a[i][j] | |
} | |
} | |
} | |
//ham hien thi mang | |
void display(int a[][max],int hang,int cot){ | |
for(int i=0;i<hang;i++){ | |
for(int j=0;j<cot;j++){ | |
printf("%3d",a[i][j]); | |
} | |
printf("\n"); | |
} | |
} | |
//ham hoan doi 2 so | |
void hoandoi(int &a,int &b){ | |
int t=a; | |
a=b; | |
b=t; | |
} | |
//ham sap xep mang 1 chieu | |
void sxmang1(int *a,int n){ | |
for(int i=0;i<n;i++){ | |
for(int j=i+1;j<n;j++){ | |
if(*(a+i)>*(a+j)) hoandoi(*(a+i),*(a+j)); | |
} | |
} | |
} | |
//ham bien mang 1 chieu thanh mang xoan oc | |
void sxmang2(int *a, int *b, int hang, int cot){ | |
int tren=0, trai=0, phai=cot-1, duoi=hang-1,dem=0; | |
while(dem<hang*cot){ | |
for(int i=trai; i<phai && dem<hang*cot; i++) *(a+tren*max+i)=b[dem++]; | |
for(int i=tren; i<duoi && dem<hang*cot; i++) *(a+i*max+phai)=b[dem++]; | |
for(int i=phai; i>trai && dem<hang*cot; i--) *(a+duoi*max+i)=b[dem++]; | |
for(int i=duoi; i>tren && dem<hang*cot; i--) *(a+i*max+trai)=b[dem++]; | |
trai++; tren++; phai--; duoi--; | |
} | |
} | |
main(){ | |
int hang, cot, dem=0; | |
int a[max][max], b[max]; | |
printf("Nhap mang: \n"); | |
input(*a,b,hang,cot); //ban than mang 1 chieu da la mot con tro | |
int n=hang*cot; | |
printf("\nHien thi mang: \n"); | |
display(a,hang,cot); | |
printf("\nLay mang 1 chieu mang 2 chieu\n"); | |
for(int i=0;i<hang*cot;i++) printf("%3d",b[i]); | |
printf("\nSap xep mang 1 chieu tang dan: \n"); | |
//sap xep b theo chieu tang dan | |
sxmang1(b,n); | |
for(int i=0;i<hang*cot;i++) printf("%3d",b[i]); | |
printf("\nMang xoan oc: \n"); | |
/*int tren=0, trai=0, phai=cot-1, duoi=hang-1; dem=0; | |
while(dem<hang*cot){ | |
for(int i=trai; i<phai && dem<hang*cot; i++) a[tren][i]=b[dem++]; | |
for(int i=tren; i<duoi && dem<hang*cot; i++) a[i][phai]=b[dem++]; | |
for(int i=phai; i>trai && dem<hang*cot; i--) a[duoi][i]=b[dem++]; | |
for(int i=duoi; i>tren && dem<hang*cot; i--) a[i][trai]=b[dem++]; | |
trai++; tren++; phai--; duoi--; | |
} */ | |
sxmang2(*a,b,hang,cot); | |
display(a,hang,cot); | |
getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment