用c语言写Guass-Jordan消元代码
时间: 2023-06-09 18:07:57 浏览: 108
下面是一个用C语言实现Guass-Jordan消元算法的代码:
```c
#include <stdio.h>
void printMatrix(float matrix[][4], int rows, int cols) {
printf("\nMatrix:\n");
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
printf("%f ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
float matrix[3][4] = {{3.0, 2.0, -4.0, 3.0}, {2.0, 3.0, 3.0, 15.0}, {5.0, -3.0, 1.0, 14.0}};
int rows = 3, cols = 4;
float pivot, multiplier;
printMatrix(matrix, rows, cols);
for(int i=0; i<rows; i++) {
pivot = matrix[i][i];
if(pivot != 0) {
for(int j=0; j<cols; j++) {
matrix[i][j] /= pivot;
}
for(int k=0; k<rows; k++) {
if(k != i) {
multiplier = matrix[k][i];
for(int j=0; j<cols; j++) {
matrix[k][j] -= multiplier * matrix[i][j];
}
}
}
}
}
printMatrix(matrix, rows, cols);
return 0;
}
```
这个代码实现了Guass-Jordan消元算法,其中输入矩阵定义在`matrix`数组中,`rows`和`cols`分别表示行数和列数。在算法的执行过程中,程序对于每一行,首先找到本行的主元元素,将这个元素除以主元元素的值,然后用这个主元元素将本行中其他元素消成0。接着,程序会进入下一行。当所有行都处理完之后,矩阵的结果将存储在`matrix`数组中,并被输出到屏幕上。
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)