You are on page 1of 3

/*

Name - N.R. Rohan


Roll No. - M19MA010

Project - Assuming the none of the Pivot columns is zero

*/

#include<iostream>

using namespace std;

struct fmatrix
{
int r;
int c;
double *M;
};

void disp_matrix(fmatrix A)
{
cout<<endl;
for(int i=0; i<A.r; i++)
{
for(int j=0; j<A.c; j++)
{
cout<<*(A.M + i*A.c + j)<<"\t";
}
cout<<endl;
}
cout<<endl;
}
int rank_matrix(fmatrix A)
{
int rank=0, flag=0;

for(int i=0; i<A.r; i++)


{
for(int j=0; j<A.c; j++)
{
if(*(A.M + i*A.c + j) != 0)
{
flag = 1;
break;
}
}
}

if(flag==0)
rank=0;
else
{
for(int ip=0; ip<A.r-1; ip++)
{
double p=*(A.M + ip*A.c + ip);
if(p!=0)
{
for(int ir = ip+1; ir<A.r; ir++)
{
double fact = *(A.M + ir*A.c +ip)/p;
for(int ic=0; ic<A.c; ic++)
{
*(A.M + ir*A.c +ic) = *(A.M + ir*A.c +ic) -
fact*(*(A.M + ip*A.c +ic));
}
}
}
}
}

for(int i=0; i<A.r; i++)


{
if(*(A.M + i*A.c + i) != 0)
rank++;
}

cout<<"Displaying the Matrix after row reduction"<<endl;


disp_matrix(A);

return rank;
}

int main()
{
fmatrix A;

cout<<"Enter the number of rows"<<endl;


cin>>A.r;

cout<<"Enter the number of columns"<<endl;


cin>>A.c;

A.M = (double *)malloc(A.r*A.c*sizeof(double));

cout<<"Enter the matrix"<<endl;

for(int i=0; i<A.r; i++)


{
for(int j=0; j<A.c; j++)
{
cout<<"A["<<i<<"]["<<j<<"] = ";
cin>>*(A.M + i*A.c + j);
}
}

cout<<endl<<"Displaying the matrix"<<endl;


disp_matrix(A);

int rank = rank_matrix(A);


cout<<"The rank of the matrix is "<<rank<<endl;

delete (A.M);
return 0;
}

You might also like