-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion11.cpp
More file actions
156 lines (155 loc) · 2.97 KB
/
question11.cpp
File metadata and controls
156 lines (155 loc) · 2.97 KB
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
#include<iostream>
using namespace std;
int identity;
bool linearSearch(int*a,int n,int key)
{
for(int i=0;i<n;i++)
{
if(a[i]==key)
{
return true;
}
}
return false;
}
int operation(int p,int q,char ch)
{
switch (ch)
{
case '+': return p+q;
break;
case '*': return p*q;
break;
case '-': return p-q;
break;
default: return p/q;
}
}
bool isClosure(int*s,int n,char ch)
{
int result=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
result=operation(s[i],s[j],ch);
if(!linearSearch(s,n,result)) return false;
}
}
return true;
}
bool isAssociative(int*s,int n,char ch)
{
for(int i=0;i<n-2;i++)
{
for(int j=i+1;j<n-1;j++)
{
for(int k=j+1;k<n;k++)
{
if(operation(operation(s[i],s[j],ch),s[k],ch)!=operation(operation(s[j],s[k],ch),s[i],ch))
{
return false;
}
}
}
}
return true;
}
bool isIdentity(int*s,int n,char ch)
{
int j;
for(int i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(operation(s[i],s[j],ch)!=s[j])
{
break;
}
}
if(j==n)
{
identity=s[i];
return true;
}
}
return false;
}
bool isInverse(int*s,int n,char ch)
{
int j;
for(int i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(operation(s[i],s[j],ch)==identity)
{
break;
}
}
if(j==n) return false;
}
return true;
}
bool isGroup(int*s,int n,char ch)
{
if(isClosure(s,n,ch)&&isAssociative(s,n,ch)&&isIdentity(s,n,ch)&&isInverse(s,n,ch)) return true;
else return false;
}
bool isCommutative(int*s,int n,char ch)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(operation(s[i],s[j],ch)!=operation(s[j],s[i],ch))
{
return false;
}
}
}
return true;
}
bool isDistributive(int*s,int n,char ch1,char ch2)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if(operation(s[i],operation(s[j],s[k],ch1),ch2)!=operation(operation(s[i],s[j],ch2),operation(s[i],s[k],ch2),ch1))
{
return false;
}
}
}
}
return true;
}
bool isRing(int*s,int n,char ch1,char ch2)
{
if(isGroup(s,n,ch1)&&isCommutative(s,n,ch1)&&isClosure(s,n,ch2)&&isAssociative(s,n,ch2)&&isDistributive(s,n,ch1,ch2)) return true;
else return false;
}
int main()
{
int n;char ch1,ch2;
cout<<endl<<"Name - Daksh Gupta"<<endl;
cout<<"Roll no. - 2019UCO1669"<<endl<<endl;
cout<<"Enter the number of elements in the set:\n";
cin>>n;
cout<<"Enter the elements of the set:\n";
int s[1000];
for(int i=0;i<n;i++)
{
cin>>s[i];
}
cout<<"Enter the first binary operation:\n";
cin>>ch1;
cout<<"Enter the second binary operation:\n";
cin>>ch2;
if(isRing(s,n,ch1,ch2)) cout<<"It's a ring!"<<endl;
else cout<<"It's not a ring!";
return 0;
}