Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<conio.h>
- #include<stdio.h>
- #include<stdlib.h>
- #define Max 100
- #define TRUE 1
- #define FALSE 0
- typedef unsigned char Boolean;
- void ReadMat(float A[Max][Max+1],int n)
- {
- int i,j;
- for(i=0;i<n;i++)
- for(j=0;j<(n+1);j++)
- scanf("%f",&A[i][j]);
- }
- void PrintMat(float A[Max][Max+1], int n)
- {
- int i,j;
- for(i=0;i<n;i++)
- {
- for(j=0;j<(n+1);j++)
- printf("%10.3f ",A[i][j]);
- printf("\n");
- }
- }
- Boolean GaussJordanElimination(float A[Max][Max+1],int n)
- {
- float C1,Det=1;
- int i,j,k;
- for(i=0;i<n;i++)
- {
- if(A[i][i]==0) //checking if zero or not
- {
- PrintMat(A,n);
- printf("\n");
- for(k=i+1;k<n;k++)//search for non zero row of matrix
- if(A[k][i]!=0)break;
- if(k>=n)return FALSE;
- for(j=i;j<(n+1);j++)
- {
- C1=A[i][j];
- A[i][j]=A[k][j];
- A[k][j]=C1;
- }
- PrintMat(A,n);
- printf("\n");
- }
- C1=A[i][i];
- Det=Det*C1;
- for(j=i;j<(n+1);j++)//pivoting the ith row
- A[i][j]=A[i][j]/C1;
- PrintMat(A,n);
- printf("\n");
- for(k=0;k<n;k++)//eleminate all rows from ith to nth row
- {
- if(k==i)
- continue;
- C1=A[k][i];//take a[k][i] as multliplication constant
- for(j=i;j<(n+1);j++)//elemminate
- A[k][j]=A[k][j]-(A[i][j]*C1);
- PrintMat(A,n);
- printf("\n");
- }
- }
- printf("\nDet=%f",Det);
- return TRUE;
- }
- int main()
- {
- float A[Max][Max+1],X[Max];
- int i,n;
- printf("enter the number of rows : ");
- scanf("%d",&n);
- ReadMat(A,n);
- PrintMat(A,n);
- printf("\n");
- if(GaussJordanElimination(A,n))
- {
- for(i=0;i<n;i++)
- X[i]=A[i][n];
- printf("\nSolution");
- for(i=0;i<n;i++)
- printf("\nX[%d]=%f",i,X[i]);
- }
- else
- printf("\nno solution present");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement