0% found this document useful (0 votes)
724 views3 pages

C++ Program To Find The Rank of A Matrix PDF

This C++ program contains functions to display a matrix, calculate the rank of a matrix using row reduction, and a main function to test the rank calculation. The rank_matrix function performs row reduction on the input matrix A and returns its rank. It first checks if any elements are zero, and if so returns a rank of 0. Otherwise it performs row operations to reduce the matrix to row echelon form and counts the number of nonzero rows on the diagonal to determine the rank.

Uploaded by

Rohan Rajagopal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
724 views3 pages

C++ Program To Find The Rank of A Matrix PDF

This C++ program contains functions to display a matrix, calculate the rank of a matrix using row reduction, and a main function to test the rank calculation. The rank_matrix function performs row reduction on the input matrix A and returns its rank. It first checks if any elements are zero, and if so returns a rank of 0. Otherwise it performs row operations to reduce the matrix to row echelon form and counts the number of nonzero rows on the diagonal to determine the rank.

Uploaded by

Rohan Rajagopal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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