-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ4.cpp
170 lines (165 loc) · 2.78 KB
/
Q4.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<iostream>
#include<stdio.h>
using namespace std;
class RELATION
{
int matrx[3][3];
int a = 3;
int b = 3;
public:
void input()
{
/*cout<<"Enter no. of elememts in Set A.: ";
cin>>a;
cout<<"Enter no. of elememts in Set B.: ";
cin>>b;*/
for (int i = 0; i<a ; i++)
for (int j = 0; j<b; j++)
{
cout<<"Enter 0 or 1 for set A element "<<(i+1)<<" with set B element "<<(j+1)<<": ";
cin>>matrx[i][j];
}
}
int reflexive()
{
int f = 0;
for (int i=0; i<a; i++)
for (int j=0; j<b; j++)
{
if ((i==j)&&(matrx[i][j]==1))
f++;
}
if (f == a)
{
cout<<"Relation is Reflexive."<<endl;
return 1;
}
else
{
cout<<"Relation is Not Reflexive."<<endl;
return 0;
}
}
int sym()
{
int matrxt[3][3];
int f = 0;
for (int i = 0; i<b ; i++)
for (int j = 0; j<a; j++)
{
matrxt[i][j] = matrx[j][i];
}
for (int i = 0; i<b ; i++)
for (int j = 0; j<a; j++)
{
if (matrxt[i][j] == matrx[i][j])
f++;
}
if (f == (a*b))
{
cout<<"Relations is Symmetric."<<endl;
return 1;
}
else
{
cout<<"Relation is not symmetric."<<endl;
return 0;
}
}
int antisym()
{
int f = 0;
for (int i = 0; i<a; i++)
for (int j = 0; j<b; j++)
{
if (((matrx[i][j]==0)||(matrx[j][i]==0))&&(i!=j))
f++;
}
if (f == ((a*b)-a))
{
cout<<"Relation is Anti-Symmetric."<<endl;
return 1;
}
else
{
cout<<"Relation is not Anti-Symmetric."<<endl;
return 0;
}
}
int trans()
{
int f = 0;
for (int i = 0; i<a; i++)
for (int j = 0; j<b; j++)
for (int k = 0; k<a ; k++)
{
if (matrx[i][j]==1&&matrx[j][k]==1&&matrx[i][k]==1)
f++;
}
if (f > 1)
{
cout<<"Relation is Transitive"<<endl;
return 1;
}
else
{
cout<<"Relation is Not Transitive"<<endl;
return 0;
}
}
void r_check()
{
if (reflexive()==1&&sym()==1&&trans()==1)
cout<<"This is an Equivalence Relation."<<endl;
else if (reflexive()==1&&antisym()==1&&trans()==1)
cout<<"This is a Partial Ordered Relation."<<endl;
else
cout<<"Neither an Equivalent nor PO."<<endl;
}
};
int main()
{
int choice;
RELATION ans;
ans.input();
do
{
cout<<"Choose one of the following."<<endl;
cout<<"1. Check for Reflexive."<<endl;
cout<<"2. Check for Symmetric."<<endl;
cout<<"3. Check for Anti-Symmetric."<<endl;
cout<<"4. Check for Transitive."<<endl;
cout<<"5. Check for type of Relation (Equivalance/Partial Ordered/None)"<<endl;
cout<<"0. Exit"<<endl;
cin>>choice;
switch (choice)
{
case 1:
{
ans.reflexive();
break;
}
case 2:
{
ans.sym();
break;
}
case 3:
{
ans.antisym();
break;
}
case 4:
{
ans.trans();
break;
}
case 5:
{
ans.r_check();
break;
}
}
}while (choice != 0);
return 0;
}