#include #include // Hàm để đọc ma trận từ tệp int** readMatrixFromFile(const char* filename, int* rows, int* columns) { FILE* file = fopen(filename, "r"); if (file == NULL) { printf("Không thể mở tệp %s\n", filename); return NULL; } fscanf(file, "%d %d", rows, columns); int** matrix = (int**)malloc(*rows * sizeof(int*)); for (int i = 0; i < *rows; i++) { matrix[i] = (int*)malloc(*columns * sizeof(int)); for (int j = 0; j < *columns; 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 calculateColumnProduct(int** matrix, int rows, int columns) { for (int j = 0; j < columns; j++) { int product = 1; for (int i = 0; i < rows; i++) { product *= matrix[i][j]; } printf("Tích của cột %d: %d\n", j + 1, product); } } // Hàm để tìm cột có tích các phần tử nhỏ nhất int findColumnWithSmallestProduct(int** matrix, int rows, int columns) { int minProduct = 1; int minColumn = -1; for (int j = 0; j < columns; j++) { int product = 1; for (int i = 0; i < rows; i++) { product *= matrix[i][j]; } if (minColumn == -1 || product < minProduct) { minProduct = product; 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 rows) { for (int i = 0; i < rows; i++) { free(matrix[i]); } free(matrix); } int main() { int rows, columns; int** matrix = readMatrixFromFile("matrix.txt", &rows, &columns); if (matrix == NULL) { return 0; } calculateColumnProduct(matrix, rows, columns); int smallestProductColumn = findColumnWithSmallestProduct(matrix, rows, columns); printf("Cột có tích các phần tử nhỏ nhất: %d\n", smallestProductColumn + 1); freeMatrix(matrix, rows); return 0; }