Skip to content

Commit 3b1937f

Browse files
Merge pull request #4 from farhadurFahim/patch-1
added gaussian elimination method
2 parents d5f1da3 + 754bd4e commit 3b1937f

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int mat_size,i,j,step;
7+
8+
cout<<"Matrix size: ";
9+
cin>>mat_size;
10+
11+
double mat[mat_size+1][mat_size+1], x[mat_size][mat_size+1];
12+
13+
cout<<endl<<"Enter value of the matrix: "<<endl;
14+
for(i=0;i<mat_size;i++)
15+
{
16+
for(j=0;j<=mat_size;j++)
17+
{
18+
cin>>mat[i][j]; //Enter (mat_size*mat_size) value of the matrix.
19+
}
20+
}
21+
22+
for(step=0;step<mat_size-1;step++){
23+
for(i=step;i<mat_size-1;i++)
24+
{
25+
double a = (mat[i+1][step]/mat[step][step]);
26+
27+
for(j=step;j<=mat_size;j++)
28+
mat[i+1][j] = mat[i+1][j] - (a * mat[step][j]);
29+
}
30+
}
31+
32+
cout<<endl<<"Matrix using Gaussian Elimination method: "<<endl;
33+
for(i=0;i<mat_size;i++)
34+
{
35+
for(j=0;j<=mat_size;j++)
36+
{
37+
x[i][j] = mat[i][j];
38+
cout<<mat[i][j]<<" ";
39+
}
40+
cout<<endl;
41+
}
42+
cout<<endl<<"Value of the Gaussian Elimination method: "<<endl;
43+
for(i=mat_size-1;i>=0;i--)
44+
{
45+
double sum = 0;
46+
for(j=mat_size-1;j>i;j--)
47+
{
48+
x[i][j] = x[j][j] * x[i][j];
49+
sum = x[i][j] + sum;
50+
}
51+
if(x[i][i]==0)
52+
x[i][i] = 0;
53+
else
54+
x[i][i] = (x[i][mat_size] - sum)/(x[i][i]);
55+
56+
cout<<"x"<<i<<"= "<<x[i][i]<<endl;
57+
}
58+
return 0;
59+
}

0 commit comments

Comments
 (0)