#include<stdio.h> #include<stdlib.h> main() { int n, i, j, dem, tam, x; int *a; //nhap n: do { printf("\nNhap so phan tu cua mang: "); scanf("%d",&n); } while(n<=0); //cap phat dong n o nho, con tro a tro den n o nho nay a = (int *) malloc(n+1); //nhap n gia tri tu ban phim va luu vao n o nho printf("\nNhap cac phan tu cua mang:\n"); for(i=0;i<n;i++) { printf("\nPhan tu thu %d: ",i+1); scanf("%d",a+i); } //in mang vua nhap printf("\nMang vua nhap:\n"); for(i=0;i<n;i++) { printf("%d ",*(a+i)); } //dem cac phan tu le trong mang dem = 0; for(i=0;i<n;i++) { if(*(a+i)%2) { dem++; } } printf("\n So phan tu le trong mang la: %d",dem); //sap xep mang for(i=0; i <= n -2; i++) for(j=i+1; j<=n-1; j++) if(*(a+i)>*(a+j)) { tam = *(a+i); *(a+i) = *(a+j); *(a+j) = tam; } //in mang vua sap xep printf("\nMang vua sap xep:\n"); for(i=0;i<n;i++) { printf("%d ",*(a+i)); } //chen phan tu vao mang da sap xep printf("\nNhap phan tu can chen vao mang: "); scanf("%d",&x); i = 0; //duyet mang tu dau den phan tu dau tien co gia tri lon hon hoac bang x while(*(a+i)<x) i++; //i la vi tri cua phan tu co gia tri nho nhat ma lon hon x //di chuyen cac phan tu lon hon x sang phai 1 vi tri for(j=n-1; j>=i; j--) { *(a+j+1) = *(a+j); } //gan x vao vi tri thu i *(a+i) = x; //in mang sau khi chen printf("\nMang vua chen:\n"); for(i=0;i<=n;i++) { printf("%d ",*(a+i)); } }
Tags:
lập trình c