-
Notifications
You must be signed in to change notification settings - Fork 17
/
ds question 3.cpp
168 lines (157 loc) · 3.41 KB
/
ds question 3.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
#include<iostream>
using namespace std;
class relation
{
public:
int n;
int** m;
void getdata();
void printdata();
int reflexive();
int symmetric();
int assymmetric();
int transitive();
};
void relation::getdata()
{
cout<<"Enter order of matrix"<<endl;
cin>>n;
m=new int* [n];
for(int i=0;i<n;i++)
m[i]=new int[n];
cout<<"Enter matrix of order "<<n<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cin>>m[i][j];
}
}
void relation::printdata()
{
cout<<endl<<"The matrix is "<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<m[i][j]<<" ";
cout<<endl;
}
}
int relation::reflexive()
{
int c=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
{
if(m[i][j]==1)
c++;
}
}
}
if(c==n)
return 1;
else
return 0;
}
int relation::symmetric()
{
int t=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i!=j)
{
if((m[i][j]==1)&&(m[j][i]!=1))
t=0;
}
}
}
return t;
}
int relation::assymmetric()
{
int t=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i!=j)
{
if((m[i][j]==1)&&(m[j][i]==1))
t=0;
}
}
}
return t;
}
int relation::transitive()
{
int t=1;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
if((m[i][j]==1)&&(m[j][k]==1)&&(m[i][k]!=1))
t=0;
return t;
}
int main()
{
relation a;
a.getdata();
a.printdata();
int ch;
cout<<" Menu "<<endl;
cout<<"1. Reflexive relation "<<endl;
cout<<"2. Symmetric relation "<<endl;
cout<<"3. Assymmetric relation "<<endl;
cout<<"4. Transitive relation "<<endl;
cout<<"Enter choice "<<endl;
cin>>ch;
switch(ch)
{
case 1:
{
int r=a.reflexive();
if(r==1)
cout<<"The relation is reflexive"<<endl;
else
cout<<"The relation is not reflexive"<<endl;
break;
}
case 2:
{
int s=a.symmetric();
if(s==1)
cout<<"The relation is symmetric"<<endl;
else
cout<<"The relation is not symmetric"<<endl;
break;
}
case 3:
{
int as=a.assymmetric();
if(as==1)
cout<<"The relation is assymmetric"<<endl;
else
cout<<"The relation is not assymmetric"<<endl;
break;
}
case 4:
{
int t=a.transitive();
if(t==1)
cout<<"The relation is transitive"<<endl;
else
cout<<"The relation is not transitive"<<endl;
break;
}
default:
{
cout<<"Wrong choice "<<endl;
break;
}
}
}