Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,868 questions

51,791 answers

573 users

How to check whether two matrices are equal or not in C

3 Answers

0 votes
#include <stdio.h>

#define LEN 4

int matrices_equal(int matrix1[][LEN], int matrix2[][LEN], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            if (matrix1[i][j] != matrix2[i][j]) 
                return 0;
	}

	return 1;
}
   
int main()
{
    int matrix1[][LEN] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 7, 6, 3 } };
    int matrix2[][LEN] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 7, 6, 3 } };
	int matrix3[][LEN] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 } };
	
	int rows = sizeof(matrix1) / sizeof(matrix1[0]);
    int cols = sizeof(matrix1[0]) / sizeof(matrix1[0][0]);
	
    if (matrices_equal(matrix1, matrix2, rows, cols))
		printf("Equal\n");
	else
		printf("Not Equal\n");

    if (matrices_equal(matrix2, matrix3, rows, cols))
		printf("Equal\n");
	else
		printf("Not Equal\n");
        
    return 0;
}
   
   
   
/*
run:
   
Equal
Not Equal
  
*/

 



answered Jul 7, 2020 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
 
#define LEN 4
 
int main(void) {
    int matrix1[][LEN] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 7, 6, 3 } };
    int matrix2[][LEN] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 7, 6, 3 } };
     
    if (memcmp(matrix1, matrix2, 12 * sizeof(int)) == 0) {
        puts("Equal");
    } else {
        puts("Not Equal");
    }
    
    return 0;
}
 
 
   
   
/*
run:
   
Equal
   
*/

 



answered Dec 24, 2020 by avibootz
edited Dec 25, 2020 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
 
#define LEN 4
 
int main(void) {
    int matrix1[][LEN] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 7, 6, 3 } };
    int matrix2[][LEN] = { { 1, 1, 1, 4 }, { 5, 6, 7, 8 }, { 9, 7, 6, 3 } };
     
    if (memcmp(matrix1, matrix2, 12 * sizeof(int)) == 0) {
        puts("Equal");
    } else {
        puts("Not Equal");
    }
    
    return 0;
}
 
 
   
   
/*
run:
   
Not Equal
   
*/

 



answered Dec 24, 2020 by avibootz
edited Dec 25, 2020 by avibootz

Related questions

1 answer 159 views
1 answer 148 views
1 answer 99 views
1 answer 128 views
1 answer 81 views
...