#include #include #include void** create_matrix(long long rows, long long cols, long long element_size); void delete_matrix(void** matrix, long long rows); int read_matrix(long double** matrix, long long equations); void print_matrix(long double** matrix, long long equations); bool gauss_jordan(long double** matrix, long long equations); bool make1(long double** matrix, long long equations, int row); void divide_row(long double** matrix, long long equations, long long row, long double divisor); void make0(long double** matrix, long long equations, long long row, long long reference_row); int main() { long long equations = 0; if ( scanf("%lld\n", &equations) != 1 ) return feof(stdin) ? 0 : 1; if ( equations <= 0 ) return printf("invalid data\n"); long double** matrix = (long double**)create_matrix(equations, equations + 1, sizeof(long double)); if ( matrix == NULL ) return 3; if ( read_matrix(matrix, equations) != 0 ) return 4; print_matrix(matrix, equations); gauss_jordan(matrix, equations); delete_matrix( (void**)matrix, equations ); return 0; } void** create_matrix(long long rows, long long cols, long long element_size) { void** matrix = calloc(rows, sizeof(void*)); if ( matrix == NULL ) return NULL; for (long long row_index = 0; row_index < rows; ++row_index ) if ( ( matrix[row_index] = calloc(cols, element_size) ) == NULL ) return NULL; return matrix; } void delete_matrix(void** matrix, long long rows) { for (long long row_index = 0; row_index < rows; ++row_index ) free( matrix[row_index] ); free(matrix); } int read_matrix(long double** matrix, long long equations) { for (long long row_index = 0; row_index < equations; ++row_index ) for (long long col_index = 0; col_index < equations + 1; ++col_index ) if ( scanf("%Lg%*[ \n]", &matrix[row_index][col_index]) != 1 ) return 4; return 0; } void print_matrix(long double** matrix, long long equations) { for (long long row_index = 0; row_index < equations; ++row_index ) { for (long long col_index = 0; col_index < equations; ++col_index ) printf(" %5.2Lf", matrix[row_index][col_index]); printf(" | %5.2Lf\n", matrix[row_index][equations]); } putchar('\n'); } bool gauss_jordan(long double** matrix, long long equations) { for ( long long active_row = 0; active_row < equations; ++active_row ) { if ( ! make1(matrix, equations, active_row) ) return false; print_matrix(matrix, equations); for ( int other_row = 0; other_row < equations; ++other_row ) if ( active_row != other_row ) make0(matrix, equations, other_row, active_row); print_matrix(matrix, equations); } return true; } bool make1(long double** matrix, long long equations, int row) { if ( matrix[row][row] == 0.0 ) return false; printf("f%d /= %5.2Lf:\n", row + 1, matrix[row][row]); divide_row(matrix, equations, row, matrix[row][row]); return true; } void divide_row(long double** matrix, long long equations, long long row, long double divisor) { for( int column = 0; column < equations + 1; ++column ) matrix[row][column] /= divisor; } void make0(long double** matrix, long long equations, long long row, long long reference_row) { long double inverse = - matrix[row][reference_row]; printf("f%lld += %5.2Lf f%lld:\n", row + 1, inverse, reference_row + 1); for ( int col = 0; col < equations + 1; ++col ) matrix[row][col] += inverse * matrix[reference_row][col]; }