-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN_lab.cpp
More file actions
68 lines (58 loc) · 1.42 KB
/
N_lab.cpp
File metadata and controls
68 lines (58 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i, j, k, n,col;
float A[20][20], c, x[10], sum = 0.0;
cout << "Enter the order of matrix: ";
cin >> n>>col;
cout << "\nEnter the elements of augmented matrix row-wise:\n\n";
for (i = 0; i < n; i++)
{
for (j = 0; j <col; j++)
{
cin >> A[i][j];
}
}
for (j = 0; j < col; j++) /* loop for the generation of upper triangular matrix */
{
for (i = 0; i < n; i++)
{
if (i > j)
{
c = A[i][j] / A[j][j];
for (k = 0; k <= n; k++)
{
A[i][k] = A[i][k] - c * A[j][k];
}
}
}
}
x[n - 1] = A[n - 1][n] / A[n - 1][n - 1];
/* this loop is for backward substitution */
for (i = n - 2; i >= 0; i--)
{
sum = 0;
for (j = i + 1; j < n; j++)
{
sum = sum + A[i][j] * x[j];
}
x[i] = (A[i][n] - sum) / A[i][i];
}
cout << "\nThe solution is:\n";
for (i = 0; i < n; i++)
{
cout << "x" << i + 1 << " = " << x[i] << "\t"; /* x1, x2, x3 are the required solutions */
}
cout<<"\n";
for (i = 0; i < n; i++)
{
for (j = 0; j <col; j++)
{
cout<< A[i][j]<<"\t";
}
cout<<"\n";
}
cout <<endl;
return 0;
}