-
Notifications
You must be signed in to change notification settings - Fork 11
/
matrix_rank.cpp
87 lines (74 loc) · 1.31 KB
/
matrix_rank.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
#include <bits/stdc++.h>
using namespace std;
#define R 3
#define C 3
void swap(int mat[R][C], int row1, int row2,
int col)
{
for (int i = 0; i < col; i++)
{
int temp = mat[row1][i];
mat[row1][i] = mat[row2][i];
mat[row2][i] = temp;
}
}
void display(int mat[R][C], int row, int col);
int rankOfMatrix(int mat[R][C])
{
int rank = C;
for (int row = 0; row < rank; row++)
{
if (mat[row][row])
{
for (int col = 0; col < R; col++)
{
if (col != row)
{
double mult = (double)mat[col][row] /
mat[row][row];
for (int i = 0; i < rank; i++)
mat[col][i] -= mult * mat[row][i];
}
}
}
else
{
bool reduce = true;
for (int i = row + 1; i < R; i++)
{
if (mat[i][row])
{
swap(mat, row, i, rank);
reduce = false;
break ;
}
}
if (reduce)
{
rank--;
for (int i = 0; i < R; i ++)
mat[i][row] = mat[i][rank];
}
row--;
}
}
return rank;
}
void display(int mat[R][C], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
printf(" %d", mat[i][j]);
printf("\n");
}
}
int main()
{
int mat[][3] = {{10, 20, 10},
{-20, -30, 10},
{30, 50, 0}};
printf("Rank of the matrix is : %d",
rankOfMatrix(mat));
return 0;
}