-
Notifications
You must be signed in to change notification settings - Fork 0
/
lu.c
75 lines (61 loc) · 1.83 KB
/
lu.c
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
#include <stdio.h>
#define MAX_SIZE 10
void printMatrix(double A[MAX_SIZE][MAX_SIZE], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%.2f\t", A[i][j]);
}
printf("\n");
}
}
void luFactorization(double A[MAX_SIZE][MAX_SIZE], double L[MAX_SIZE][MAX_SIZE], double U[MAX_SIZE][MAX_SIZE], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j)
L[i][j] = 1.0;
else
L[i][j] = 0.0;
U[i][j] = 0.0;
}
}
for (int k = 0; k < n; k++) {
for (int j = k; j < n; j++) {
U[k][j] = A[k][j];
for (int p = 0; p < k; p++) {
U[k][j] -= L[k][p] * U[p][j];
}
}
for (int i = k + 1; i < n; i++) {
L[i][k] = A[i][k];
for (int p = 0; p < k; p++) {
L[i][k] -= L[i][p] * U[p][k];
}
L[i][k] /= U[k][k];
}
}
}
int main() {
int n;
printf("Enter the size of the square matrix (max %d): ", MAX_SIZE);
scanf("%d", &n);
if (n <= 0 || n > MAX_SIZE) {
printf("Invalid matrix size. Please enter a value between 1 and %d.\n", MAX_SIZE);
return 1;
}
double A[MAX_SIZE][MAX_SIZE], L[MAX_SIZE][MAX_SIZE], U[MAX_SIZE][MAX_SIZE];
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < n; i++) {
printf("Row %d: ", i + 1);
for (int j = 0; j < n; j++) {
scanf("%lf", &A[i][j]);
}
}
luFactorization(A, L, U, n);
printf("\nMatrix A:\n");
printMatrix(A, n);
printf("\nMatrix L:\n");
printMatrix(L, n);
printf("\nMatrix U:\n");
printMatrix(U, n);
return 0;
}