我是一名计算机工程专业的第一年学生,试图使用Gaussian-Jordan消除计算器解决具有多个未知变量的方程组
我的算法是将方程放在二维数组(矩阵)中,并使计算机应用基本操作行 1-重组方程式,使以零开头的方程式位于底部 2-通过划分其对角元素element11 element22的值上的每一行,使所有对角元素= 1,等等 3-消除对角线上下的元素,形成梯形 答案应该是最后一栏。 这是我的代码
#include<stdio.h>
int input;
int rows;
int columns;
float matrix[100][100];
int true =1;
void interchange(curr,alter){
float row_X[columns];
float row_Y[columns];
int k;
for(k=0;k<columns;k++){
row_X[k]=matrix[curr][k];
}
for(k=0;k<columns;k++){
row_Y[k]=matrix[alter][k];
}
for(k=0;k<columns;k++){
matrix[curr][k]=row_Y[k];
}
for(k=0;k<columns;k++){
matrix[alter][k]=row_X[k];
}
}
void order(){
int e;
int count;
count = 1;
for(e=0;e<rows;e++){
if(matrix[e][e]==0){
while(true==1){
if(matrix[e+count][e]!=0){
interchange(e,e+count);
}
count+=1;
if(e+count==rows){
break;
}
}
}
}
}
void diagonals_to_ones(){
int g,h;
for(g=0;g<rows;g++){
for(h=0;h<columns;h++){
matrix[g][h]/=matrix[g][g];
}
}
}
void zeros(){
int inverse;
int diag;
int i,j;
for(diag=0;diag<rows;diag++){
order();
diagonals_to_ones();
for(i=0;i<rows;i++){
if(i!=diag){
inverse=-1*matrix[i][diag];
for(j=0;j<columns;j++){
matrix[i][j]+=matrix[diag][j]*inverse;
}
}
}
}
}
void main(){
printf("Welcome to the awesome system of equations calculator,\n no need for
tedious elementary row operations any more,\n we do it for you, it's
computer era");
printf("\n\n\nEnter the number of equations you want to solve (number of
variables) :");
scanf("%d",&input);
rows=input;
columns=input+1;
printf("\n\nNow enter the cofactor of the variables\nfollowed by the free
nonvariable numbers for each equation\nin the same order");
int a,b;
for(a=0;a<rows;a++){
printf("\n\n\n\nNow equation number %d",a+1);
for(b=0;b<columns-1;b++){
printf("\n\nEnter The Cofactor of Variable %d :",b+1);
scanf("%f",&matrix[a][b]);
}
printf("\nEnter The free non Variable number :");
scanf("%f",&matrix[a][b]);
}
printf("\n\n\nYour System of Equations is :\n\n");
int c,d;
for( c=0;c<rows;c++){
for( d=0;d<columns;d++){
printf("%.2f ",matrix[c][d]);
}
printf("\n");
}
zeros();
for( c=0;c<rows;c++){
for( d=0;d<columns;d++){
printf("%.2f ",matrix[c][d]);
}
printf("\n");
}
}
这是另一个版本,矩阵准备好测试答案应为1,-1,2
#include<stdio.h>
int input;
int rows=3;
int columns=4;
float matrix[3][4]={
{1,-2,3,9},
{-1,3,1,-2},
{2,-5,5,17}
};
int true =1;
void interchange(curr,alter){
float row_X[columns];
float row_Y[columns];
int k;
for(k=0;k<columns;k++){
row_X[k]=matrix[curr][k];
}
for(k=0;k<columns;k++){
row_Y[k]=matrix[alter][k];
}
for(k=0;k<columns;k++){
matrix[curr][k]=row_Y[k];
}
for(k=0;k<columns;k++){
matrix[alter][k]=row_X[k];
}
}
void order(){
int e;
int count;
count = 1;
for(e=0;e<rows;e++){
if(matrix[e][e]==0){
while(true==1){
if(matrix[e+count][e]!=0){
interchange(e,e+count);
}
count+=1;
if(e+count==rows){
break;
}
}
}
}
}
void diagonals_to_ones(){
int g,h;
for(g=0;g<rows;g++){
for(h=0;h<columns;h++){
matrix[g][h]/=matrix[g][g];
}
}
}
void zeros(){
int inverse;
int diag;
int i,j;
for(diag=0;diag<rows;diag++){
order();
diagonals_to_ones();
for(i=0;i<rows;i++){
if(i!=diag){
inverse=-1*matrix[i][diag];
for(j=0;j<columns;j++){
matrix[i][j]+=matrix[diag][j]*inverse;
}
}
}
}
}
void main(){
printf("\n\n\nYour System of Equations is :\n\n");
int c,d;
for( c=0;c<rows;c++){
for( d=0;d<columns;d++){
printf("%.2f ",matrix[c][d]);
}
printf("\n");
}
zeros();
printf("\n\nthe answer is\n");
for( c=0;c<rows;c++){
for( d=0;d<columns;d++){
printf("%.2f ",matrix[c][d]);
}
printf("\n");
}
printf("\n\nthe answer should have been\n");
float ansmatrix[3][4]={
{
1,0,0,1
},
{
0,1,0,-1
},
{
0,0,1,2
}
};
for( c=0;c<rows;c++){
for( d=0;d<columns;d++){
printf("%.2f ",ansmatrix[c][d]);
}
printf("\n");
}
}