-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checking Equality of Two Matrices
91 lines (73 loc) · 1.45 KB
/
Checking Equality of Two Matrices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Humza KHawar
Lab Task
Comparing Matrices
*/
#include<stdio.h>
void main()
{
//initialization of array
int matrix1[10][10] = { 0 }, matrix2[10][10] = { 0 }, size, flag = 0;
//inputing size
printf("\n Enter he size of the Square Matrices: ");
scanf("%d", &size);
//inputing elements of matrix
printf("\nMatrix 1");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
printf("\nEnter Number [%d] [%d]: ", i+1, j+1);
scanf("%d", &matrix1[i][j]);
}
}
printf("\nMatrix 2");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
printf("\nEnter Number [%d] [%d]: ", i+1, j+1);
scanf("%d", &matrix2[i][j]);
}
}
//printing the matrix
printf("\nYour Matrix 1:\n");
for (int i = 0; i < size; i++)
{
printf("\n |");
for (int j = 0; j < size; j++)
{
printf(" %3d ", matrix1[i][j]);
}
printf(" |");
}
//printing the matrix
printf("\nYour Matrix 2:\n");
for (int i = 0; i < size; i++)
{
printf("\n |");
for (int j = 0; j < size; j++)
{
printf(" %3d ", matrix2[i][j]);
}
printf(" |");
}
//comparing each element of matrix
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (matrix1[i][j] != matrix2[i][j]) {
flag = 1; //flag is increased to 1 if any element is unequal
}
}
}
if (flag ==1)
{
printf("\n\nMatrices are NOT equal\n\n");
}
else
{
printf("\n\nMatrices are equal\n\n");
}
}