Computer Science‎ > ‎

## C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra

`#include<stdio.h>`
`double **matrix;`
`double *temp;`
`double **coeff;`
`double *unknownvar;`
`int variables;`
`int row,column,inner;`
`void setdiagonal(int row)`
`{//this function exchanges rows in case we have the diagonal entries while tranforming as zero`
`int ctr=0;`
`while(matrix[row][row]==0)`
`{`
`if(row+1!=variables)`
`{//exchange with the next row until we are not on the last row`
`temp=matrix[row];`
`    matrix[row]=matrix[row+1];`
`    matrix[row+1]=temp;`
`    temp=coeff[row];`
`    coeff[row]=coeff[row+1];`
`    coeff[row+1]=temp;`
`}`
`else`
`{//exchange the last row with thr first one`
`temp=matrix[row];`
`    matrix[row]=matrix;`
`    matrix=temp;`
`    temp=matrix[row];`
`    coeff[row]=coeff;`
`    coeff=temp;`
`}`
`ctr++;`
`if(ctr==variables)`
`{//exchanging the row with all others rows doesnot make diagonal entry non-zero then we do not have a solution`
`printf("The system of equations can not be solved by gauss elimination method");`
`getch();`
`exit(0);`
`}`
`}`
`}`
`void reduceMatrices()`
`{//this function reduces the matrix in upper triangular form`
`    double multfactor;`
`    for(row=0;row<variables;row++)`
`    {`
`if(matrix[row][row]==0)`
`setdiagonal(row);//set the diagonal if it's entry is 0`
`for(inner=row+1;inner<variables;inner++)`
`{`
`multfactor=matrix[inner][row]/matrix[row][row];//the multfactor is a factor which is used to multiply with a row to make the next row's some entry 0`
`coeff[inner]=coeff[inner]-multfactor*coeff[row];`
`for(column=0;column<variables;column++)`
`matrix[inner][column]=matrix[inner][column]-multfactor*matrix[row][column];`
`}`
`}`
`}`
`int main()`
`{`
`printf("Enter the number of variables\n");`
`scanf("%d",&variables);`
`matrix=(double**)malloc(variables*sizeof(double));//declare the matrix to hold the coefficents`
`coeff=(double **)malloc(variables*sizeof(double));//declare the matrix to hold the rhs coefficients`
`unknownvar=(double*)malloc(variables*sizeof(double));//declare the matrix of the unknown variables`
`int i,j;`
`for(row=0;row<variables;row++)`
`{`
`matrix[row]=(double*)malloc(variables*sizeof(double));`
`coeff[row]=(double*)malloc(variables*sizeof(double));`
`}`
`temp=(double*)malloc(variables*sizeof(double));//temporary array used while swapping rows`
`    printf("Enter the coefficent matrix of the unknown variables\n");`
`for(row=0;row<variables;row++)`
`for(column=0;column<variables;column++)`
`scanf("%lf",&matrix[row][column]);`
`    printf("Enter the coefficent matrix of the RHS\n");`
`    for(row=0;row<variables;row++)`
`    scanf("%lf",&coeff[row]);`
`reduceMatrices();//We call the function to reduce both coefficent matrices to a form to allow apllication of backward substitution`
`double sum;`
`for(row=variables-1;row>=0;row--)`
`{//This is the coding for the backward substitution`
`    sum=coeff[row];`
`for(column=row+1;column<variables;column++)`
`sum=sum-matrix[row][column]*unknownvar[column];`
`if(matrix[row][row]==0)`
`{//If any diagonal entry in the reduced matrix is 0 we can not find solution`
`printf("The system of equations can not be solved by gauss elimination method");`
`getch();`
`exit(0);`
`}`
`unknownvar[row]=sum/matrix[row][row];`
`}`
`printf("The unknowns are\n");//display the unknowns`
`    for(row=0;row<variables;row++)`
`printf("%lf  ",unknownvar[row]);`
`for(row=0;row<variables;row++)`
`{`
`free(matrix[row]);`
`free(coeff[row]);`
`}`
`free(matrix);`
`free(temp);`
`free(coeff);`
`free(unknownvar);`
`getch();`
`}`
`/*A sample run of the program was carried and the resulta were found as:-`
`Enter the number of variables`
`3`
`Enter the coefficent matrix of the unknown variables`
`0 1 1`
`2 0 3`
`1 1 1`
`Enter the coefficent matrix of the RHS`
`2`
`5`
`3`
`The unknowns are`
`1.000000 1.000000 1.000000`
`*/`

 C Program to Reverse A StringC Program: Building an Expression EvaluatorC Program: Check for Armstrong NumbersC Program: Check whether a string is a Palindrome or notC Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal ProductC Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansionsC Program: Computing the Area of a CircleC Program: Computing the Upper Triangular Matrix and Lower Triangular MatrixC Program: Demonstrating File Handling FunctionsC Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding DeterminantsC Program: Demonstrating the use of Bitwise OperatorsC Program: Displaying a Histogram of word frequencies (unigram)C Program: Distance Vector Routing Algorithm using Bellman Ford's AlgorithmC Program: Numerical Computing - The Gaussian Elimination MethodC Program: Numerical Computing - Implementing the Newton Raphson MethodC Program: Numerical Computing - the Bisection MethodC Program: Numerical Computing - The Gaussian Elimination Technique from Linear AlgebraC Program: Numerical Computing - the Jacobi MethodC Program: Printing the Pascal TriangleC Program: Reversing the order of words in a sentenceC Program: Solving Simultaneous Equations in Two VariablesC Program: Source Code for computing the GCD(HFC) of two numbersC Program: Source Code for Solving Quadratic EquationsC Program: Source code to solve the Josephus ProblemC Program: Sudoku SolverC Program: The Usage of Command Line ArgumentsC Program: Using the Sieve of Eratosthenes to print Prime Numbers