//I have a problem with my program,
// the function rank to find rank of a matrix 
//cannot return the result
//somebody help me to correct it!

#include<iostream.h>
#include<math.h>
#include<iomanip.h>

class matrix
{
	int n,m;
	double A[6][6];
	public:
	//.....
	friend int rank(matrix);
};
int rank(matrix B)
{
	int i, j, r, c;
	int rankmt=B.m;
	i=0;
	j=0;
	while(i<B.m&&j<B.n)
	{
			if(B.A[i][j]==0)
			{
				r=i+1;
				while(r<B.m&&B.A[r][j]==0)
					r++;
					if(r==B.m)
					{i++; j++; continue;}
		
				for(c=0; c<B.n; c++)
				{
					double temp= B.A[r][c];
					B.A[r][c]=B.A[i][c];
					B.A[i][c]=temp;
				}
			}
	}
	while(i<B.m&&j<B.n)
	{
			if(B.A[i][j]!=0)
			{
				r=i+1;
				while(r<B.m&&B.A[r][j]!=0)
				{
					for(c=0;c<B.n;c++)
					B.A[r][c]-=B.A[i][c]*B.A[r][j]/B.A[i][j];
					r++;
					if(r==B.m)
					break;
				}
			}
				i++;
				j++;
				continue;
	}
				
		for(i=0;i<B.m;i++)
		for(j=0;j<B.n;j++)
		if(B.A[i][j]!=0)
		break;
		else
		if(j=B.n)
		rankmt--;	
	
	return rankmt;
}	
void main()
{
	matrix m1;
	//...
	cout<<rankmt(m1);
}

This is quite a complicated function. It is almost impossible to follow - you have multiple nested loops, one letter variable names, and NO comments!! Just because it is called "code" doesn't mean it has to look like an encrypted document! Every one of those nested loops could probably be broken out into function. I bet you that code contains something like a SumRow(int) function and SumDiagonal(), etc. If you write it modularly like this, then you can test each function individually and see exactly which piece is going wrong.

This is quite a complicated function. It is almost impossible to follow - you have multiple nested loops, one letter variable names, and NO comments!! Just because it is called "code" doesn't mean it has to look like an encrypted document! Every one of those nested loops could probably be broken out into function. I bet you that code contains something like a SumRow(int) function and SumDiagonal(), etc. If you write it modularly like this, then you can test each function individually and see exactly which piece is going wrong.

thank daviddoria!!!
I've corected my program after test each function individually. :icon_cheesygrin:

Great! Please mark the thread as solved.

I'm not good at English
I post my progam after I corect

int rank(matran B)
{
	int rankmt,i,j,r,c;
	rankmt=B.m;
	//**************************
	for(i=1; i<=B.m; i++)
	for(j=1; j<=B.n; j++)
	{
	if(B.A[i][j]==0&&i==j)
	{
		for(r=i+1; r<=B.m; r++)
		if(B.A[r][j]!=0)
		{
			for(c=1; c<=B.m; c++)
			{
				double temp= B.A[r][c];
				B.A[r][c]=B.A[i][c];
				B.A[i][c]=temp;
			}
			break;
		}
	}
	else
	break;
	}
	//**************************
	for(i=1; i<=B.m; i++)
	for(j=1; j<=B.n; j++)
	{
		if(B.A[i][j]!=0&&i==j)
		r=i+1;
		while(r<=B.m)
		{
			double k=B.A[r][j]/B.A[i][j];
			for(c=1; c<=B.n; c++)
			B.A[r][c]=B.A[r][c]-k*B.A[i][c];
			r++;
		}
	}
	//***************************
	
	for(i=1; i<=B.m; i++)
	{
		for(j=1; j<=B.n; j++)
		if(B.A[i][j]!=0)
		break;
		else
		if(j==B.n)
		rankmt--;		
	}
	return rankmt;	
}

Thanks, that is a very important step, so others can see the solution as well.

There should be a section at the bottom of the thread that says "Has this thread been answered?" with a link that says "Mark this Thread as Solved". The idea is that people won't waste time reading your question and trying to answer it because then they can see that it has already been taken care of.

Just a quick note on your code: you should use

#include <iostream>
#include <cmath>
#include <iomanip>

instead of

#include <iostream.h>
#include <math.h>
#include <iomanip.h>

These are the old, C-style way of doing it :o)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.