【算法】 C/C++实现直接法解线性方程组

该博客提供了一段C/C++代码,使用高斯消元法来求解线性方程组。用户可以输入矩阵系数和常数向量,程序将通过高斯消元将矩阵转换为上三角形并计算唯一解或无限解。代码已针对原始优快云文章中的问题进行了修正。

这里就不解释线性代数的数学解法原理什么的了,可以参考下我看过的这篇优快云文章:

http://blog.youkuaiyun.com/kang___xi/article/details/51475268

这篇文章的代码部分好像有点问题,我在其基础之上做了一些修改,并且可以把最终结果算出输出在控制台上。


【1】代码

/*======================================================

 # Author:      Sugary
 # Filetype:    C/C++ source code
 # Environment: Window 8.1
 # Tool:        CodeBlocks
 # Date:        2017/3/7
 # Descprition: Solve the AX=b by the Guass Elimination!

========================================================*/

#include<stdio.h>
#include<math.h>

#define ROW 100
#define COL 100

void get_coefficient(double[][COL], int, int);
void get_vector(double[], int);
void create_Ab(double[][COL], double[], int, int);
void show_matrix(double[][COL], int, int);
void guass_elimination(double *[ROW], int, int);
void exchange_row(double *[ROW], int, int, int);
void show_solution(double *[ROW], int, int);
void back_substitution(double *[ROW], int, int);
void back_substitution(double *[ROW], int, int, int);

int main()
{
    double Receptacle[ROW][COL];    // store the matrix A
    double Vector[ROW];             // store the vector b
    double *Ab_pointer[ROW];        // store every row of augment matrix (A,b)
    int row, col;                   // store the row/col of the matrix;
    int i;

    printf("Enter the coefficient matrix's size (less than %d * %d): ", ROW, COL - 1);
    while (scanf("%d%d", &row, &col) == 2)
    {
        get_coefficient(Receptacle, row, col);  // get the value of matrix A
        get_vector(Vector, row);                // get the value of vector b
        create_Ab(Receptacle, Vector, row, col);// create the augment matrix (A,b)

        printf("\nThe linear equations in the form of augmented matrix as follow:\n");
        show_matrix(Receptacle, row, col + 1);  // output the augment matrix (A,b)

        for (i = 0; i < ROW; i++)               // make every Ab_pointer points every row of the augment matrix (A,b)
            Ab_pointer[i] = Receptacle[i];

        guass_elimination(Ab_poin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值