People who have studied mathematics know that Gaussian elimination method is used to solve linear equations. The algorithm is very simple, but the process is very complicated, this is why I cannot find a free and correct Gaussian elimination method on the Internet. So I made up my mind to compile it myself.
Gaussian elimination method is widely used. It is one of the most important ways to solve mathematical problems. In the first chapter of this book, Gaussian elimination method is used, many problems are ultimately caused by linear method groups.
I am a beginner in programming, so this program may feel funny to the experts. But I don't mind. Please give me more advice.
My program is compiled in C language and has never been learned in other languages. Non-computer students in college generally only learn C language, so this program is suitable for college students.
I hope that the university can point out the shortcomings of my program and give me more comments. Thank you.
Copy codeThe Code is as follows: # include "Stdio. h"
# Include "Conio. h"
/* L is the number of rows in the matrix minus 1, which is the number of outermost loops in the program.
N corresponds to the number of rows in the matrix, and M corresponds to the number of columns in the matrix.
You can change L, N, and M to control the order of moment */
# Define L 3
# Define N 4
# Define M 5
Void gauss (double a [N] [M], double x [N])
{Int I, j, l, n, m, k = 0;
Double temp [N];
/* The first do-while is to remove the Augmented Matrix into the upper triangle form */
Do {n = 0;
For (l = k; l <L; l ++) temp [n ++] = a [l + 1] [k]/a [k] [k];
For (m = 0, I = k; I <N; I ++, m ++)
For (j = k; j <M; j ++) a [I + 1] [j]-= temp [m] * a [k] [j];
K ++;
} While (k <N );
/* The second do-while is to remove the matrix from the pair angle form and assign a value to k again */
K = L-1;
Do {n = 0;
For (l = k; l> = 0; l --) temp [n ++] = a [k-l] [k + 1]/a [k + 1] [k + 1];
For (m = 0, I = k; I> = 0; I --, m ++)
For (j = k; j <M; j ++) a [k-I] [j]-= temp [m] * a [k + 1] [j];
K --;
} While (k> = 0 );
/* The next for is the equation group */
For (I = 0; I <N; I ++) x [I] = a [I] [N]/a [I] [I];
}
Void menu ()
{Printf ("\ n _ \ n ");
Printf ("1. operation \ n ");
Printf ("2. exit ");
Printf ("\ n _ \ n ");
}
Main ()
{Int I, j, choose;
Double a [N] [M] = {0}, answer [N];
Clrscr ();
While (1 ){
Leep: menu ();
Scanf ("% d", & choose );
Switch (choose ){
Case 1:
Printf ("!! The size of Maxrix is % d * % d, each line enter % d element: \ n ", N, M, M );
For (I = 0; I <N; I ++)
{Printf ("Enter the Matrix's % d line: \ n", I );
For (j = 0; j <N + 1; j ++)
Scanf ("% lf", & a [I] [j]);
}
Printf ("\ nthe corss matrix is: \ n _ \ n ");
Gauss (a, answer );
For (I = 0; I <N; I ++)
{For (j = 0; j <M; j ++)
Printf ("%-2lf", a [I] [j]);
Putchar ('\ n ');
}
Printf ("_ \ nthe solve is: \ n ");
For (I = 0; I <N; I ++) printf ("x % d = % lf \ n", I + 1, answer [I]);
Case 2:
Exit (0 );
Default: printf ("input error: \ n"); goto leep;
}
}
Getch ();
}
/* Test:
Example 2.1 on the 28 pages of the computing method book published by Xi'an Jiaotong University Press:
2 3-4-2
_-3-4-12 13 5
A = 2 10 0-3 10
14 9-13 7
Test results: x1 = 1, x2 = 2, x3 = 3, x4 = 4 */