-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crout.cpp
108 lines (106 loc) · 2.56 KB
/
Crout.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>//Crout Method
using namespace std;
void initializer(int A[][3], int B[], int rows, int columns)
{
cout << "Enter Values of A:" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << "Enter a[" << i << "][" << j << "]=";
cin >> A[i][j];
}
}
cout << endl << "Enter values of B:" << endl;
for (int i = 0; i < rows; i++)
{
cout << "Enter b[" << i << "]=";
cin >> B[i];
}
}
void display(int A[][3], int B[], int rows, int columns)
{
for (int i = 0; i < rows; i++)
{
cout << "|";
for (int j = 0; j < columns; j++)
{
cout << A[i][j] << " ";
}
cout << "| |x" << i + 1 << "| |" << B[i] << "|";
cout << endl;
}
cout << "------------------------------------------------------------------------------- " << endl;
}
void displayLU(float L[][3], float U[][3], int rows, int columns)
{
cout << "L Matrix:" << endl;
for (int i = 0; i < rows; i++)
{
cout << "|";
for (int j = 0; j < columns; j++)
{
cout << L[i][j] << " ";
}
cout << "|";
cout << endl;
}
cout << "------------------------------------------------------------------------------- " << endl;
cout << "U Matrix:" << endl;
for (int i = 0; i < rows; i++)
{
cout << "|";
for (int j = 0; j < columns; j++)
{
if (i == j)
{
U[i][j] = 1;
}
cout << U[i][j] << " ";
}
cout << "|";
cout << endl;
}
}
void display_arr(float X[], int rows)
{
cout << endl;
for (int i = 0; i < rows; i++)
{
cout << "|" << X[i] << "|" << endl;
}
}
int main()
{
int rows = 3, columns = 3;
int A[3][3], B[3];
float L[3][3] = { 0 }, U[3][3] = { 0 }, Y[3], X[3];
initializer(A, B, rows, columns);
display(A, B, rows, columns);
//calculating L and U
L[0][0] = A[0][0];
U[0][1] = A[0][1] / L[0][0];
U[0][2] = A[0][2] / L[0][0];
L[1][0] = A[1][0];
L[1][1] = A[1][1] - (L[1][0] * U[0][1]);
U[1][2] = (A[1][2] - (L[1][0] * U[0][2])) / L[1][1];
L[2][0] = A[2][0];
L[2][1] = A[2][1] - (L[2][0] * U[0][1]);
L[2][2] = A[2][2] - (L[2][0] * U[0][2]) - (L[2][1] * U[1][2]);
displayLU(L, U, rows, columns);
//calculating Y
Y[0] = B[0]/L[0][0];
Y[1] = (B[1] - Y[0])/L[1][1];
Y[2] = (B[2] - (L[2][0]*Y[0])-(L[2][1]*Y[1]))/L[2][2];
cout << "-------------------------------------------------------------------------------" << endl;
cout << "Y Matrix:";
display_arr(Y, rows);
//calculating x
X[2] = Y[2];
X[1] = Y[1] - (U[1][2] * X[2]);
X[0] = Y[0] - (U[0][1] * X[1]) - (U[0][2] * X[2]);
cout << "-------------------------------------------------------------------------------- " << endl;
cout << "X Matrix:";
display_arr(X, rows);
return 0;
}