#include #include // Hàm để đọc ma trận từ tệp int** readMatrixFromFile(const char* filename, int* m, int* n) { FILE* file = fopen(filename, "r"); if (file == NULL) { printf("Không thể mở tệp %s\n", filename); return NULL; } fscanf(file, "%d %d", m, n); int** matrix = (int**)malloc((*m) * sizeof(int*)); for (int i = 0; i < *m; i++) { matrix[i] = (int*)malloc((*n) * sizeof(int)); for (int j = 0; j < *n; j++) { fscanf(file, "%d", &matrix[i][j]); } } fclose(file); return matrix; } // Hàm để tính tích của từng cột của ma trận void calculateColumnProducts(int** matrix, int m, int n, int* products) { for (int j = 0; j < n; j++) { int product = 1; for (int i = 0; i < m; i++) { product *= matrix[i][j]; } products[j] = product; } } // Hàm để tìm cột có tích các phần tử nhỏ nhất int findColumnWithMinimumProduct(int* products, int n) { int minProduct = products[0]; int minColumn = 0; for (int j = 1; j < n; j++) { if (products[j] < minProduct) { minProduct = products[j]; minColumn = j; } } return minColumn; } // Hàm để giải phóng bộ nhớ đã cấp phát cho ma trận void freeMatrix(int** matrix, int m) { for (int i = 0; i < m; i++) { free(matrix[i]); } free(matrix); } int main() { char filename[100]; printf("Nhập tên tệp: "); scanf("%s", filename); int m, n; int** matrix = readMatrixFromFile(filename, &m, &n); if (matrix == NULL) { return 0; } // Tính tích của từng cột int* columnProducts = (int*)malloc(n * sizeof(int)); calculateColumnProducts(matrix, m, n, columnProducts); // Tìm cột có tích các phần tử nhỏ nhất int minColumn = findColumnWithMinimumProduct(columnProducts, n); printf("Cột có tích các phần tử nhỏ nhất: %d\n", minColumn + 1); // Giải phóng bộ nhớ free(columnProducts); freeMatrix(matrix, m); return 0; }