-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion10.cpp
More file actions
117 lines (117 loc) · 2.1 KB
/
question10.cpp
File metadata and controls
117 lines (117 loc) · 2.1 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
#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;
}
int main()
{
int n;char ch;
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 binary operation:\n";
cin>>ch;
if(isGroup(s,n,ch)) cout<<"It's a group!"<<endl;
else cout<<"It's not a group!";
return 0;
}