求助C语言编程求解矩阵的根
用C语言编程求解一个3*4矩阵的根,只用最基础的就可以。由于初学C语言,根本不懂如何编程,希望有人帮帮我。
x+y+z=7
2x+y-z=5
x-y-2z=4
#include <stdio.h> int value(int sa[3][4], int a, int b, int c) { int t[3][3] = {{sa[0][a], sa[0][b], sa[0][c]}, {sa[1][a], sa[1][b], sa[1][c]}, {sa[2][a], sa[2][b], sa[2][c]}}; return t[0][0] * t[1][1] * t[2][2] + t[0][1] * t[1][2] * t[2][0] + t[0][2] * t[1][0] * t[2][1] - t[0][0] * t[1][2] * t[2][1] - t[0][1] * t[1][0] * t[2][2] - t[0][2] * t[1][1] * t[2][0]; } int main(void) { int a[3][4] = {{1, 1, 1, 7}, {2, 1, -1, 5}, {1, -1, -2, 4}}; int d = value(a, 0, 1, 2); printf("%d\n", d); if (!d) { printf("No Answer\n"); return -1; } int dx = value(a, 3, 1, 2); int dy = value(a, 0, 3, 2); int dz = value(a, 0, 1, 3); printf("x = %lf, y = %lf, z = %lf\n", dx * 1.0 / d, dy * 1.0 / d, dz * 1.0 / d); return 0; }
#include <stdio.h> #define ROW 3 #define COL (ROW + 1) int print(int (*sa)[COL]) { printf("Array:\n"); for (int i = 0; i < ROW; ++i) for (int j = 0; j < COL; ++j) { printf("%d%c", sa[i][j], j + 1 - COL ? '\t' : '\n'); } printf("\n"); } int getGcd(int a, int b) { int tmp; while (b) { tmp = b; b = a % b; a = tmp; } return a; } int getLcm(int a, int b) { if (!a) return b; if (!b) return a; return a / getGcd(a, b) * b; } int main(void) { int a[ROW][COL] = {{1, 1, 1, 7}, {2, 1, -1, 5}, {1, -1, -2, 4}}; for (int line = 0; line < ROW; ++line) { int row = line; // 跳过已是0的行 while (a[line][row] == 0 && row++ < ROW); if (row >= ROW) { printf("无解或无数解\n"); return -1; } // 当前列剩余行转化为 0 for (int i = 0; i < ROW; ++i) { if (i == row || !a[i][line]) continue; int lcm = getLcm(a[row][line], a[i][line]); int plus = lcm / a[i][line]; for (int j = 0; j < COL; ++j) { a[i][j] *= plus; a[i][j] -= lcm / a[row][line] * a[row][j]; } } print(a); } for (int i = 0; i < ROW; ++i) { printf("r%d = %lf\n", i + 1, a[i][3] * 1.0 / a[i][i]); } return 0; }
[此贴子已经被作者于2016-11-20 21:48编辑过]
#include <stdio.h> #define ROW 3 #define COL (ROW + 1) int print(int (*sa)[COL]) { printf("Array:\n"); for (int i = 0; i < ROW; ++i) for (int j = 0; j < COL; ++j) { printf("%d%c", sa[i][j], j + 1 - COL ? '\t' : '\n'); } printf("\n"); } int getGcd(int a, int b) { int tmp; while (b) { tmp = b; b = a % b; a = tmp; } return a; } int getLcm(int a, int b) { if (!a) return b; if (!b) return a; return a / getGcd(a, b) * b; } int main(void) { int a[ROW][COL] = {{0, -1, -3, -9}, {1, 1, 1, 7}, {0, -2, -3, -3}}; for (int row = 0; row < ROW; ++row) { int tmp = row; // 跳过已是0的行 while (a[row][tmp] == 0 && tmp++ < ROW); if (tmp >= ROW) { printf("无解或无数解\n"); return -1; } // 交换row和line for (int col = 0; col < COL; ++col) { int tmpp = a[tmp][col]; a[tmp][col] = a[row][col]; a[row][col] = tmpp; } // 当前列剩余行转化为 0 for (int i = 0; i < ROW; ++i) { if (i == row || !a[i][row]) continue; int lcm = getLcm(a[row][row], a[i][row]); int plus = lcm / a[i][row]; for (int j = 0; j < COL; ++j) { a[i][j] *= plus; a[i][j] -= lcm / a[row][row] * a[row][j]; } } print(a); } for (int i = 0; i < ROW; ++i) { printf("r%d = %lf\n", i + 1, a[i][3] * 1.0 / a[i][i]); } return 0; }
[此贴子已经被作者于2016-11-20 21:50编辑过]