Skip to content

Instantly share code, notes, and snippets.

@ManiruzzamanAkash
Created March 7, 2017 16:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ManiruzzamanAkash/df4db3869224c621c1327b2eb8e0de72 to your computer and use it in GitHub Desktop.
Save ManiruzzamanAkash/df4db3869224c621c1327b2eb8e0de72 to your computer and use it in GitHub Desktop.
Gauss Elimination problem solution in C language simple
/**
Gauss Elimination problem solution in C language [Basic]
@author : Maniruzzaman Akash
**/
#include<stdio.h>
int main(){
int i, j, k, n;
float matrix[100][100];
float sum = 0.0;
float x[100]; //Keep the values in it
float temp = 0.0;
printf("Please enter the order of the matrix : ");
scanf("%d", &n);
//Take the matrix value from the scan
for(i = 1; i <= n; i++){
for(j = 1; j <= n+1; j++){
printf("Enter Matrix value for Mat[%d][%d] :", i, j);
scanf("%f", &matrix[i][j]);
}
}
printf("\n");
//Show the matrix entered by the user
for(i = 1; i <= n; i++){
for(j = 1; j <= n; j++){
printf("%f ", matrix[i][j]);
}
printf("\n");
}
//Make an upper triangular matrix first
for(j = 1; j <= n; j++){
for(i = 1; i <= n; i++){
if(i > j){
temp = matrix[i][j] / matrix[j][j];
for(k = 1; k <= n + 1; k++){
matrix[i][k] = matrix[i][k] - temp * matrix[j][k];
}
}
}
}
x[n]= matrix[n][n+1] / matrix[n][n];
//Backward substitution
for(i=n-1; i>=1; i--){
sum = 0;
for(j = i+1; j <= n; j++)
{
sum = sum+matrix[i][j]*x[j];
}
x[i]=(matrix[i][n+1]-sum) / matrix[i][i];
}
printf("The values are : \n");
for(i=1; i<=n; i++)
{
printf("\nx%d=%f\t",i,x[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment