-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest10.c
More file actions
72 lines (65 loc) · 1.78 KB
/
Copy pathtest10.c
File metadata and controls
72 lines (65 loc) · 1.78 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
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLS 4
void print_matrix(float matrix[ROWS][COLS], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%.2f ", matrix[i][j]);
}
printf("\n");
}
}
void swap_rows(float matrix[ROWS][COLS], int row1, int row2) {
for (int j = 0; j < COLS; j++) {
float temp = matrix[row1][j];
matrix[row1][j] = matrix[row2][j];
matrix[row2][j] = temp;
}
}
void scale_row(float matrix[ROWS][COLS], int row, float scalar) {
for (int j = 0; j < COLS; j++) {
matrix[row][j] *= scalar;
}
}
void add_scaled_row(float matrix[ROWS][COLS], int dest_row, int src_row, float scalar) {
for (int j = 0; j < COLS; j++) {
matrix[dest_row][j] += scalar * matrix[src_row][j];
}
}
void rref(float matrix[ROWS][COLS], int rows, int cols) {
int lead = 0;
for (int r = 0; r < rows; r++) {
if (lead >= cols) {
return;
}
int i = r;
while (matrix[i][lead] == 0) {
i++;
if (i == rows) {
i = r;
lead++;
if (lead == cols) {
return;
}
}
}
swap_rows(matrix, i, r);
scale_row(matrix, r, 1.0 / matrix[r][lead]);
for (i = 0; i < rows; i++) {
if (i != r) {
add_scaled_row(matrix, i, r, -matrix[i][lead]);
}
}
lead++;
}
}
int main() {
float matrix[ROWS][COLS] = {{2, 1, -1, 8}, {-3, -1, 2, -11}, {-2, 1, 2, -3}};
printf("Original matrix:\n");
print_matrix(matrix, ROWS, COLS);
rref(matrix, ROWS, COLS);
printf("RREF:\n");
print_matrix(matrix, ROWS, COLS);
return 0;
}