Back
KT3.cpp
Save
// KT3.cpp #include
using namespace std; void nhapMang(int *a, int n) { for (int i = 0; i < n; i++) { cout<<"Nhap phan tu thu "<
>*(a+i); } } void giaoHaiMang(int *a, int *b, int *c, int m, int n, int &o) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (*(a+i) == *(b+j)) { c = (int*)realloc(c, (o + 1) * sizeof(int)); *(c+o) = *(a+i); o++; } } } } void ghiTapTin(int *a, int n, string fileName) { ofstream ofs(fileName); for (int i = 0; i < n; i++) { ofs<<*(a+i)<<" "; } ofs.close(); } int main() { int *a, *b, *c; a = (int*)malloc(0 * sizeof(int)); b = (int*)malloc(0 * sizeof(int)); c = (int*)malloc(0 * sizeof(int)); int m, n, o = 0; cout<<"Nhap m la so phan tu cua mang 1: "; cin>>m; a = (int*)realloc(a, m * sizeof(int)); nhapMang(a, m); cout<<"Nhap n la so phan tu cua mang 2: "; cin>>n; b = (int*)realloc(b, n * sizeof(int)); nhapMang(b, n); giaoHaiMang(a, b, c, m, n, o); ghiTapTin(a, m, "Mang1.txt"); ghiTapTin(b, n, "Mang2.txt"); ghiTapTin(c, o, "Mang3.txt"); return 0; }