forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankMatrix.cpp
114 lines (100 loc) · 2.14 KB
/
rankMatrix.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
109
110
111
112
113
114
// C++ program to find rank of a matrix
#include <bits/stdc++.h>
using namespace std;
#define R 3
#define C 3
/* function for exchanging two rows of
a matrix */
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;
}
}
// Function to display a matrix
void display(int mat[R][C], int row, int col);
/* function for finding rank of matrix */
int rankOfMatrix(int mat[R][C])
{
int rank = C;
for (int row = 0; row < rank; row++)
{
// Before we visit current row 'row', we make
// sure that mat[row][0],....mat[row][row-1]
// are 0.
// Diagonal element is not zero
if (mat[row][row])
{
for (int col = 0; col < R; col++)
{
if (col != row)
{
// This makes all entries of current
// column as 0 except entry 'mat[row][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;
/* Find the non-zero element in current
column */
for (int i = row + 1; i < R; i++)
{
// Swap the row with non-zero element
// with this row.
if (mat[i][row])
{
swap(mat, row, i, rank);
reduce = false;
break ;
}
}
// If we did not find any row with non-zero
// element in current columnm, then all
// values in this column are 0.
if (reduce)
{
// Reduce number of columns
rank--;
// Copy the last column here
for (int i = 0; i < R; i ++)
mat[i][row] = mat[i][rank];
}
// Process this row again
row--;
}
// Uncomment these lines to see intermediate results
// display(mat, R, C);
// printf("\n");
}
return rank;
}
/* function for displaying the matrix */
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");
}
}
// Driver program to test above functions
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;
}