Write a program that checks whether two matrices are equal or not.


Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
#define SIZE 50
void main()
{
	int A[SIZE][SIZE];
	int B[SIZE][SIZE];
	int R, C;        /* dimensions */
	int I, J;             
			
	do {
		printf("Number of rows   (max.%d) : ", SIZE);
		scanf("%d", &R);
	} while (R <= 0 || R > SIZE);
	do {
		printf("Number of columns (max.%d) : ", SIZE);
		scanf("%d", &C);
	} while (C <= 0 || C > SIZE);

	printf("*** Matrix A ***\n");
	for (I = 0; I<R; I++)
		for (J = 0; J<C; J++)
		{
			printf("A[%d][%d] : ", I, J);
			scanf("%d", &A[I][J]);
		}

	printf("*** Matrix B ***\n");
	for (I = 0; I<R; I++)
		for (J = 0; J<C; J++)
		{
			printf("B[%d][%d] : ", I, J);
			scanf("%d", &B[I][J]);
		}


	printf("Matrix A :\n");
	for (I = 0; I<R; I++)
	{
		for (J = 0; J<C; J++)
			printf("%6d", A[I][J]);
		printf("\n");
	}

	printf("Matrix B :\n");
	for (I = 0; I<R; I++)
	{
		for (J = 0; J<C; J++)
			printf("%6d", B[I][J]);
		printf("\n");
	}

	
	for (I = 0; I<R; I++)
	{
		for (J = 0; J<C; J++)
			// if the corresponding entries of matrices are not equal, break
		 	if(A[I][J]!=B[I][J])
				break;

		//once here, 2 cases:
		// 1. J==C, we have looped over J and we exit normally, nothing to do
		// 2. J<C, we exit from break, so we need to exit the outer loop too
		if(J<C)
			break;
	}

	
	if(J<C) 
		printf("A is not equal to B");
    	else
		printf("A is equal to B");

	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Iterative function to convert a decimal integer to binary