Hi, i have a function which is supposed to compare two matrices, however i does not work properly. I get this compile warning.

warning: control reaches end of non-void function

Here is the function.

bool matrix::isequal(const matrix& ob3)
{
for (int i = 0; i < rows; i++)			
	{
		for (int j = 0; j < ob3.cols; j++)		
		{
			
			for (int k = 0; k < ob3.rows; k++)
			{
			
				if (data[i][k] == ob3.data[k][j])
				{
					cout << "matricies are equal " << endl;
					return true;
				}
				else
				{
					cout << "matricies are not equal " << endl;
					return false;
				}
			} 
		}
		
	}
}

You function doesn't really make sense.

- Are you comparing 2d or 3d arrays?
- What is "data" and where did it come from.
- Are the two matrices the same size?

If I wanted to make something that 2 matrices of equal size, I would do something like:

int main()
{
	const int rows=2;
	const int cols=2;
	int matrix1[rows][cols] = {{1,2},{3,4}};
	int matrix2[rows][cols] = {{1,2},{3,4}}; 
	bool AreTheSame = true;
    
	for (int x = 0; x< rows; x++)
	    for (int y = 0; y < cols; y++)
		if (matrix1[x][y] != matrix2[x][y]) AreTheSame = false;
	
	if (AreTheSame) std::cout << "are the same\n";
	else std::cout << "are not the same\n";
	std::cin.get();
}

Hi, i have a function which is supposed to compare two matrices, however i does not work properly. I get this compile warning.

warning: control reaches end of non-void function

Here is the function.

bool matrix::isequal(const matrix& ob3)
{
for (int i = 0; i < rows; i++)			
	{
		for (int j = 0; j < ob3.cols; j++)		
		{
			
			for (int k = 0; k < ob3.rows; k++)
			{
			
				if (data[i][k] == ob3.data[k][j])
				{
					cout << "matricies are equal " << endl;
					return true;
				}
				else
				{
					cout << "matricies are not equal " << endl;
					return false;
				}
			} 
		}
		
	}
}

It's a warning, not an error. Java would give you an error. C++ is a little more trusting, but is warning you that it may be possible to go through this function without returning a value. You declare it a boolean function. If rows equals 0, you won't return anything and it's a function that should return something.

At the very least, the k loop can be deleted. You'll go through it once or not at all, so are you sure you want it to be a loop.

The matricies are of different size, data is from a private member from a class whcih stores a matrix, ob3.data also stores a matrix, however it is just an object of the class.

The loops are designed in that way since thats the rules of matrices, if you cannot multiply them, then you cannot compare them. So from what i understand is that the number of rows in the first matrix would equal the number of columns in the second matrix, hence 3 loops so all rows and columns are traversed

The loops are designed in that way since thats the rules of matrices, if you cannot multiply them, then you cannot compare them. So from what i understand is that the number of rows in the first matrix would equal the number of columns in the second matrix, hence 3 loops so all rows and columns are traversed

All rows and columns are NOT traversed. I don't know what your intent is here as far as comparisons go, but you are going to make one or no comparisons in this function. You are returning after the first comparison. There are a lot of ways to "compare" matrices and depending on your definition of "compare" and your application, you may not have to be able to multiply them to compare them. What do you mean by "compare" and what you trying to do in this algorithm?

I want to compare as in see if the values stored in the matrices are equal, the function is required to return true if they are equal and false if they are not.

For two matrices to be equal, the dimensions of both matrices have to be equal and the corresponding values should also be equal. I would follow niek_e's method to compare two matrices.

How about posting the two matrices that you are trying to compare? Perhaps your intentions will become clearer

commented: Thanx for the help +1

I solved it! thanx for the help guys.
niek_e your example cleared things up for me :)

Glad to hear it!

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.